fix bug with dictionary only translated on demand

This commit is contained in:
Wolfang Torres
2026-06-12 20:58:53 +08:00
parent f9fc887d05
commit dde819f1e6
5 changed files with 26 additions and 20 deletions

View File

@@ -14,6 +14,7 @@ from .utility import CCCEDICT, TTS, DictionaryResult, ProcessFile, TranslationRe
# Constants
FIELDNAMES = ["simplified", "traditional", "pinyin", "meaning"]
DIALECT = "excel-tab"
# Results Classes
@@ -44,8 +45,8 @@ def dictionary_pre_process(words_list: list[str], process_file: ProcessFile):
with process_file.dictionary_resource_file.open(
"w", encoding="utf8", newline=""
) as resource_file:
tsv_writer = csv.writer(
resource_file, dialect="excel-tab", fieldnames=FIELDNAMES
tsv_writer = csv.DictWriter(
resource_file, dialect=DIALECT, fieldnames=FIELDNAMES
)
tsv_writer.writeheader()
for words in words_list:
@@ -84,15 +85,19 @@ def dictionary_process(process_file: ProcessFile) -> list[DictionaryResult]:
"""Process a dictionary_resource_file into a final result"""
results = []
with process_file.dictionary_resource_file.open(
"w", encoding="utf8", newline=""
"r", encoding="utf8", newline=""
) as resource_file:
reader = csv.DictReader(resource_file)
reader = csv.DictReader(resource_file, dialect=DIALECT)
for line in reader:
audio_path = process_file.resources / f"{line['pinyin']}.wav"
if not audio_path.exists():
audio = TTS.MODEL.generate(f"{line['simplified']}", language_id="zh")
audio = TTS.MODEL.generate(
f"{line['simplified']}", language_id=LANGUAGES.CN
)
torchaudio.save(audio_path, audio, TTS.MODEL.sr)
result = DictionaryResult(**line, audio_path=audio_path)
result = DictionaryResult(
**line, audio_path=audio_path, language_id=process_file.language_id
)
results.append(result)
return results