Skip to content

Commit

Permalink
Merge pull request #5 from Kor-SVS/dev
Browse files Browse the repository at this point in the history
버그 수정 및 개선 v0.1.1
  • Loading branch information
Cardroid committed Sep 1, 2022
2 parents bd92c6b + d747672 commit 56e5011
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 52 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pyBuild.bat
dist/
*.spec

logs/
test/
Expand Down
30 changes: 20 additions & 10 deletions src/enunu_kor_tool/analysis4vb/analysis.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from copy import deepcopy
import os
import shutil
from glob import glob

from tqdm import tqdm

from enunu_kor_tool import utils, log
from enunu_kor_tool.analysis4vb.model.config import DB_Config
from enunu_kor_tool.utaupyk._ustx2ust import Ustx2Ust_Converter
from enunu_kor_tool.analysis4vb.runner import analysis_runner
from enunu_kor_tool.analysis4vb.model import DB_Info, DB_Files
from enunu_kor_tool.analysis4vb.config import DEFAULT_CONFIG, DEFAULT_YAML_CONFIG
from enunu_kor_tool.analysis4vb import config as config_module


def cli_ui_main():
Expand Down Expand Up @@ -37,23 +40,23 @@ def main(args=None):

assert os.path.isdir(args["input"]), "입력한 경로에서 DB를 찾을 수 없습니다."

log.DIR_PATH = os.path.join(args["input"], "analysis", "logs")
config_path = os.path.join(args["input"], "analysis", "analysis_config.yaml")
output_path = os.path.join(args["input"], "analysis")
log.DIR_PATH = os.path.join(output_path, "logs")
config_path = os.path.join(output_path, "analysis_config.yaml")

def get_root_module_logger():
loglevel = options.get("log_level", "info") if (options := config.get("options")) != None else "info"
return log.get_logger("analysis4vb", loglevel)

if not os.path.isfile(config_path):
config = DEFAULT_CONFIG
config = deepcopy(config_module.DEFAULT_CONFIG)

with open(config_path, "w", encoding="utf-8") as f:
f.write(DEFAULT_YAML_CONFIG)
config_module.save_default_config2yaml(config_path)

logger = get_root_module_logger()
logger.warning(f"Config 파일이 존재하지 않습니다. (DB 내부에 기본 Config 파일을 생성합니다)\nPath=[{config_path}]")

input("Config 파일을 DB에 맞게 수정하고, 엔터를 눌러주세요...")
input("Config 파일을 DB에 알맞게 수정 후, 엔터를 눌러주세요...")
config = utils.load_yaml(config_path)
else:
config = utils.load_yaml(config_path)
Expand All @@ -67,8 +70,10 @@ def get_root_module_logger():
db_raw_ustx_files = glob(os.path.join(db_path, "**", "*.ustx"), recursive=True)

if len(db_raw_ustx_files) > 0:
logger.info("ustx -> ust 변환 중...")
os.makedirs(db_config.output.temp, exist_ok=True)
for ustx_path in db_raw_ustx_files:
for ustx_path in (db_raw_ustx_files_tqdm := tqdm(db_raw_ustx_files)):
db_raw_ustx_files_tqdm.set_description(f"ustx -> ust Converting... [{os.path.relpath(ustx_path)}]")
if (ustx_path_split := os.path.splitext(ustx_path))[1] == ".ustx":
converter = Ustx2Ust_Converter(ustx_path, encoding="utf-8")
converter.save_ust(os.path.join(db_config.output.temp, os.path.basename(ustx_path_split[0]) + ".ust"))
Expand All @@ -80,8 +85,13 @@ def get_root_module_logger():
glob(os.path.join(db_path, "**", "*.wav"), recursive=True),
)

if not (len(db_files.ustx) == len(db_files.ust) == len(db_files.lab) == len(db_files.wav)):
logger.warning(f"데이터의 개수가 일치하지 않습니다.\nustx=[{len(db_files.ustx)} 개]\nust=[{len(db_files.ust)} 개]\nlab=[{len(db_files.lab)} 개]\nwav=[{len(db_files.wav)} 개]")
ustx_file_count = len(db_files.ustx)
ust_file_count = len(db_files.ust) - ustx_file_count
lab_file_count = len(db_files.lab)
wav_file_count = len(db_files.wav)

if not (ustx_file_count == ust_file_count == lab_file_count == wav_file_count):
logger.warning(f"데이터의 개수가 일치하지 않습니다.\nustx=[{ustx_file_count} 개]\nust=[{ust_file_count} 개]\nlab=[{lab_file_count} 개]\nwav=[{wav_file_count} 개]")

db_info = DB_Info(db_path, db_name, db_files, db_config)

Expand Down
50 changes: 30 additions & 20 deletions src/enunu_kor_tool/analysis4vb/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
from typing import Any, Dict
import yaml

from enunu_kor_tool.analysis4vb.functions import FUNC_LIST
Expand Down Expand Up @@ -26,29 +28,37 @@
}


def __config2yaml(config):
return str(yaml.dump(config, indent=2, sort_keys=False, allow_unicode=True))
def config2yaml(config: Dict[str, Any]):
default_options = {
"indent": 2,
"sort_keys": False,
"allow_unicode": True,
}
result = []

# config_yaml = str(yaml.dump(config, indent=2, sort_keys=False, allow_unicode=True)).split("\n")
for key in config.keys():
if key == "group":
result.append(yaml.dump({key: config[key]}, default_flow_style=None, **default_options))
else:
result.append(yaml.dump({key: config[key]}, **default_options))

# result = []
# count = 0
# is_funcs = False
# for line in config_yaml:
# if is_funcs:
# count += 1
# if count > 1:
# if line.strip().startswith("-"):
# line = "#" + line
# else:
# is_funcs = False
# count -= 1
# elif line == "funcs:":
# is_funcs = True
return "".join(result)

# result.append(line)

# return "\n".join(result)
def save_config2yaml(path: str, config: Dict[str, Any]):
if not path.endswith(".yaml"):
path = os.path.splitext(path)[0] + ".yaml"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(config2yaml(config))


DEFAULT_YAML_CONFIG = __config2yaml(DEFAULT_CONFIG)
def save_default_config2yaml(path: str):
if not path.endswith(".yaml"):
path = os.path.splitext(path)[0] + ".yaml"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(DEFAULT_YAML_CONFIG)


DEFAULT_YAML_CONFIG = config2yaml(DEFAULT_CONFIG)
15 changes: 8 additions & 7 deletions src/enunu_kor_tool/analysis4vb/functions/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def __100ns2s(ns: int):
return ns / 10000000
return ns / (10**7)


def __preprocess(func):
Expand Down Expand Up @@ -45,7 +45,7 @@ def __lab_loader(db_info: DB_Info, logger: logging.Logger) -> bool:
lab_global_error_line_count = 0
for file in (file_tqdm := tqdm(phonemes_files, leave=False)):
file_tqdm.set_description(f"Processing... [{file}]")
logger.info(f"[{file}] 파일 로드 중...")
logger.info(f"[{os.path.relpath(file)}] 파일 로드 중...")

lab = []

Expand All @@ -65,31 +65,32 @@ def __lab_loader(db_info: DB_Info, logger: logging.Logger) -> bool:
length = lab[-1][1]
lab_len = len(lab)
lab_global_line_count += lab_len
logger.info(f"lab 파일을 로드했습니다. [총 라인 수: {line_num_formatter(lab_len)}] [길이: {round(__100ns2s(length), 1)}s ({length} 100ns)] [오류 라인 수: {error_line_count}]")

# TODO: 자음이 혼자 있을 경우 검출
# TODO: 1 Frame 보다 짧은 음소 검출
logger.debug(f"오류 검사 중...")
global_length = 0
for idx, (start, end, phn) in enumerate(lab, 1):
if phn not in db_info.config.group.all:
logger.warning(f"[Line {line_num_formatter(idx)}] [{phn}] Config에 명시되지 않은 음소가 사용되었습니다. ({file})")
logger.warning(f"[Line {line_num_formatter(idx)}] [{phn}] Config에 명시되지 않은 음소가 사용되었습니다.")
error_line_count += 1
if start >= end:
logger.warning(f"[Line {line_num_formatter(idx)}] 종료시점이 시작지점보다 빠릅니다. ({file})")
logger.warning(f"[Line {line_num_formatter(idx)}] 종료시점이 시작지점보다 빠릅니다.")
error_line_count += 1
if global_length != start:
logger.warning(f"[Line {line_num_formatter(idx)}] 시작시점이 이전 종료지점과 다릅니다. ({file})")
logger.warning(f"[Line {line_num_formatter(idx)}] 시작시점이 이전 종료지점과 다릅니다.")
error_line_count += 1

global_length = end

if error_line_count > 0:
logger.warning(f"총 [{line_num_formatter(error_line_count)}] 개의 오류가 발견되었습니다. ({file})")
logger.warning(f"총 [{line_num_formatter(error_line_count)}] 개의 오류가 발견되었습니다.\n({file})")
error_flag = True
lab_global_error_line_count += error_line_count

labs[file] = lab
logger.info(f"lab 파일을 로드했습니다. [총 라인 수: {line_num_formatter(lab_len)}] [길이: {round(__100ns2s(length), 1)}s ({length} 100ns)] [오류 라인 수: {error_line_count}]")

logger.info(f"모든 lab 파일을 로드했습니다. [lab 파일 수: {len(labs)}] [총 라인 수: {lab_global_line_count}] [오류 라인 수: {lab_global_error_line_count}]")
db_info.cache["labs"] = labs

Expand Down
8 changes: 4 additions & 4 deletions src/enunu_kor_tool/analysis4vb/functions/ust.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __ust_loader(db_info: DB_Info, logger: logging.Logger) -> bool:
ust_files = db_info.files.ust
group_config = db_info.config.group
encoding = db_info.config.options.get("encoding", "utf-8")
line_num_formatter = lambda ln: str(ln).rjust(5)
line_num_formatter = lambda ln: str(ln).rjust(4)

error_flag = False
usts = {}
Expand All @@ -48,7 +48,7 @@ def __ust_loader(db_info: DB_Info, logger: logging.Logger) -> bool:

for file in (file_tqdm := tqdm(ust_files, leave=False)):
file_tqdm.set_description(f"Processing... [{file}]")
logger.info(f"[{file}] 파일 로드 중...")
logger.info(f"[{os.path.relpath(file)}] 파일 로드 중...")

ust = up.ust.load(file, encoding=encoding)

Expand All @@ -72,7 +72,7 @@ def __ust_loader(db_info: DB_Info, logger: logging.Logger) -> bool:
logger.info(
f"ust 파일을 로드했습니다. "
f"[총 노트 수: {line_num_formatter(notes_len)} (무음 제외: {line_num_formatter(notes_voiced_len)})] "
f"[총 길이: {round(notes_length_sum, 3)} s (무음 제외: {round(notes_voiced_length_sum, 3)} s)]"
f"[총 길이: {round(notes_length_sum, 3)}s (무음 제외: {round(notes_voiced_length_sum, 3)}s)]"
)

usts[file] = ust
Expand All @@ -81,7 +81,7 @@ def __ust_loader(db_info: DB_Info, logger: logging.Logger) -> bool:
f"모든 ust 파일을 로드했습니다. "
f"[ust 파일 수: {len(usts)}] "
f"[총 노트 수: {line_num_formatter(global_notes_len)} (무음 제외: {line_num_formatter(global_notes_voiced_len)})] "
f"[총 길이: {round(global_notes_length_sum, 3)} s (무음 제외: {round(global_notes_voiced_length_sum, 3)} s)]"
f"[총 길이: {round(global_notes_length_sum, 3)}s (무음 제외: {round(global_notes_voiced_length_sum, 3)}s)]"
)
db_info.cache["usts"] = usts

Expand Down
12 changes: 11 additions & 1 deletion src/enunu_kor_tool/analysis4vb/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ def analysis_runner(db_info: DB_Info):
log_level = db_info.config.options["log_level"]
logger = log.get_logger("analysis_runner", log_level)

for func_name in (func_tqdm := tqdm(db_info.config.funcs, leave=False)):
check_funcs = []
funcs = []
for f in db_info.config.funcs:
f = f.lower()
if f.endswith("check"):
check_funcs.append(f)
else:
funcs.append(f)
funcs = check_funcs + funcs

for func_name in (func_tqdm := tqdm(funcs, leave=False)):
func_info = FUNC_LIST.get(func_name)
if func_info == None:
logger.error("찾을 수 없는 기능을 건너뛰었습니다.")
Expand Down
53 changes: 47 additions & 6 deletions src/enunu_kor_tool/entry/exe_entry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import cli_ui

BASE_MODULE_NAME = "enunu_kor_tool.analysis4vb.functions"
Expand Down Expand Up @@ -35,15 +36,27 @@ def join_module_name(func_name: str):


def cli_ui_main():
from enunu_kor_tool import version

global MODULE_LIST

selected_module = cli_ui.ask_choice("사용할 모듈을 선택하세요.", choices=MODULE_LIST, func_desc=lambda m: MODULE_DESC_LIST[m])
print()
print("".center(40, "#"))
print(f" {version.package_name} ver.{version.version} ".center(40, " "))
print(f" Dev. Cardroid ".center(40, " "))
print("".center(40, "#"))
print()

while True:
selected_module = cli_ui.ask_choice("사용할 모듈을 선택하세요.", choices=MODULE_LIST, func_desc=lambda m: MODULE_DESC_LIST[m])

module_info = MODULE_DICT[selected_module]
module = __import__(module_info["module"], fromlist=[module_info["module"]])
func = getattr(module, module_info["func"])

module_info = MODULE_DICT[selected_module]
module = __import__(module_info["module"], fromlist=[module_info["module"]])
func = getattr(module, module_info["func"])
func()

func()
print()


def main():
Expand All @@ -63,9 +76,37 @@ def main():
"pause"
)

with open(os.path.join("dist", "Start.bat"), "w", encoding="utf-8") as f:
with open(os.path.join("dist", "enunu_kor_tool", "Start.bat"), "w", encoding="utf-8") as f:
f.write(batch_file)

os.system(
(
"pyinstaller "
'--add-data="dep_package\mecab;mecab" '
'--add-data="enunu_kor_tool_python\Lib\site-packages\konlpy;konlpy" '
'--add-data="dep_package\g2pK\g2pk;g2pk" '
'--add-data="enunu_kor_tool_python\Lib\site-packages\jamo\data;jamo\data" '
'--hidden-import="konlpy" '
'--hidden-import="matplotlib" '
'--hidden-import="matplotlib.backends.backend_tkagg" '
'--hidden-import="enunu_kor_tool.analysis4vb" '
'--hidden-import="enunu_kor_tool.analysis4vb.functions.lab" '
'--hidden-import="enunu_kor_tool.analysis4vb.functions.ust" '
'--hidden-import="enunu_kor_tool.g2pk4utau.g2pk4utau" '
'--hidden-import="enunu_kor_tool.entry.ustx2lab" '
'--hidden-import="enunu_kor_tool.utaupyk._ustx2ust" '
'--hidden-import="enunu_kor_tool.utaupyk._ust2hts" '
'--hidden-import="enunu_kor_tool.entry.lab2ntlab" '
"--clean "
"--distpath dist\enunu_kor_tool "
'-n "enunu_kor_tool" '
"--noconfirm "
"src\enunu_kor_tool\entry\exe_entry.py"
)
)
shutil.rmtree("build")
os.remove("enunu_kor_tool.spec")

return

cli_ui_main()
Expand Down
4 changes: 3 additions & 1 deletion src/enunu_kor_tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def save_json(path: str, obj: Any, indent: int = 2):
if not path.endswith(".json"):
path = os.path.splitext(path)[0] + ".json"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(obj, f, indent=indent)

Expand All @@ -25,8 +26,9 @@ def load_json(path: str) -> Any:
def save_yaml(path: str, obj: Any, indent: int = 2):
if not path.endswith(".yaml"):
path = os.path.splitext(path)[0] + ".yaml"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
yaml.dump(obj, f, indent=indent, sort_keys=False, allow_unicode=True)
yaml.dump(obj, f, indent=indent, sort_keys=False, allow_unicode=True, default_flow_style=True)


def load_yaml(path: str) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion src/enunu_kor_tool/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
package_name = "enunu_kor_tool"
version = "0.1.0"
version = "0.1.1"

0 comments on commit 56e5011

Please sign in to comment.