adds support for comppleted files

This commit is contained in:
Wolfang Torres
2026-06-29 00:05:46 +08:00
parent 5c78c2dad6
commit 9299020738
6 changed files with 216 additions and 42 deletions

View File

@@ -9,13 +9,21 @@ from .api import (
is_file, is_file,
list_input_files, list_input_files,
pre_process_a_dictionary_file, pre_process_a_dictionary_file,
proccess_a_completed_file,
process_a_dictation_file, process_a_dictation_file,
process_a_dictionary_file, process_a_dictionary_file,
process_a_phrases_file, process_a_phrases_file,
select_file, select_file,
select_folder, select_folder,
) )
from .constants import DICTATION_TYPE, DICT_TYPE, INPUT, LANGUAGES, PHRASES_TYPE from .constants import (
COMPLETED_TYPE,
DICTATION_TYPE,
DICT_TYPE,
INPUT,
LANGUAGES,
PHRASES_TYPE,
)
from .utility import ProcessFile, ProcessFolder from .utility import ProcessFile, ProcessFolder
@@ -24,7 +32,7 @@ def cli_choose_work_type() -> str:
option = None option = None
while not option in ["s", "f", "e"]: while not option in ["s", "f", "e"]:
option = input( option = input(
"Please select the work option:\n" "s: single file, f:folder, e:exit\n" "Please select the work option:\n" "s:single file, f:folder, e:exit\n"
) )
return option return option
@@ -66,7 +74,7 @@ def cli_select_folder() -> ProcessFolder:
print(f"Selected {selected}") print(f"Selected {selected}")
s = None s = None
while s not in ("yes", "y", "no", "n"): while s not in ("yes", "y", "no", "n"):
s = input("if this the folder? (yes/no, go inside)") s = input("if this the folder? (yes / no, go inside) : ")
if s in ("yes", "y"): if s in ("yes", "y"):
in_folder = selected in_folder = selected
else: else:
@@ -100,9 +108,10 @@ def cli_select_language(languages: list = None) -> str:
for language_id, language in avaliable_languages: for language_id, language in avaliable_languages:
if languages and language_id in languages: if languages and language_id in languages:
print(f"{language_id} - {language}") print(f"{language_id} - {language}")
print("all - All languages")
s = None s = None
while not s or s not in LANGUAGES.AvailableLanguages: while not s or s not in LANGUAGES.AvailableLanguages + ("all",):
lan_codes = [lan_id for lan_id, lan in avaliable_languages] lan_codes = [lan_id for lan_id, lan in avaliable_languages] + ["all"]
s = input(f"Please select the language {', '.join(lan_codes)}: ") s = input(f"Please select the language {', '.join(lan_codes)}: ")
return s return s
@@ -137,8 +146,16 @@ def main():
language_id = cli_select_language() language_id = cli_select_language()
print(f"processing file {input_file} with language {language_id}") print(f"processing file {input_file} with language {language_id}")
process_a_dictation_file(input_file, language_id) process_a_dictation_file(input_file, language_id)
elif COMPLETED_TYPE in input_file.input_file.suffixes:
language_id = input_file.input_file.suffixes[1][1:]
print(f"processing file {input_file} with language {language_id}")
proccess_a_completed_file(input_file, language_id)
elif option == "f": elif option == "f":
for language_id in LANGUAGES.AvailableLanguages: language_id = cli_select_language()
langs = (
LANGUAGES.AvailableLanguages if language_id == "all" else [language_id]
)
for language_id in langs:
in_folder = cli_select_folder() in_folder = cli_select_folder()
print(f"Selected: {in_folder} with language {language_id}") print(f"Selected: {in_folder} with language {language_id}")
for dirpath, dirnames, filenames in tuple( for dirpath, dirnames, filenames in tuple(

View File

@@ -13,7 +13,13 @@ from pinyin_tone_converter.pinyin_tone_converter import PinyinToneConverter
# Local # Local
from .constants import DICTATION_TYPE, DICT_TYPE, PHRASES_TYPE from .constants import DICTATION_TYPE, DICT_TYPE, PHRASES_TYPE
from .utility import DictionaryResult, ProcessFile, ProcessFolder, TranslationResult from .utility import (
DictionaryResult,
ProcessFile,
ProcessFolder,
SimpleResult,
TranslationResult,
)
# Constants # Constants
@@ -137,9 +143,86 @@ HSK_MODEL = Model(
css=CSS, css=CSS,
) )
SIMPLE_HSK_MODEL = Model(
2819647620,
"Simple HSK Model",
fields=[
{"name": "Meaning"},
{"name": "Pinyin"},
{"name": "Simplified"},
{"name": "Audio"},
],
templates=[
{
"name": "Card 1",
"qfmt": (
"<strong>{{Pinyin}}</strong>"
"<br>{{Meaning}}"
"<br>{{Audio}}"
"<br>Simplified: {{type:Simplified}}"
),
"afmt": (
"{{FrontSide}}<hr id='answer''><div class='simple'>{{Simplified}}</div>"
),
},
{
"name": "Card 2",
"qfmt": (
"<div class='simple'>{{Simplified}}</div>" "<br>Pinyin: {{type:Pinyin}}"
),
"afmt": (
"{{FrontSide}}<hr id='answer'><strong>{{Pinyin}}</strong>"
"<br>{{Meaning}}<br>{{Audio}}"
),
},
{
"name": "Card 3",
"qfmt": ("{{Audio}}" "<br>Simplified: {{type:Simplified}}"),
"afmt": (
"{{FrontSide}}<hr id='answer'><strong>{{Pinyin}}</strong>"
"<br><div class='simple'>{{Simplified}}</div>"
),
},
],
css=CSS,
)
# Proccess # Proccess
def output_anki_completed(
process_file: ProcessFile, results: list[SimpleResult]
) -> Path:
"""Creates an anki file for a completed file"""
final_file = (
process_file.output_name.parent / f"{process_file.input_file.stem}.apkg"
)
deck_name = "::".join(process_file.input_file.parts[:-1] + (final_file.stem,))
deck = Deck(
random.randrange(1 << 30, 1 << 31),
deck_name,
f"Deck for {process_file.input_file.stem}, "
"created in https://www.wolfang.info.ve/hskankicreator/",
)
audios = []
for result in results:
note = Note(
model=SIMPLE_HSK_MODEL,
fields=[
result.meaning,
PinyinToneConverter().convert_text(result.pinyin),
result.character,
f"[sound:{result.audio_path.name}]",
],
)
deck.add_note(note)
audios.append(result.audio_path)
package = Package(deck)
package.media_files = audios
package.write_to_file(final_file)
return final_file
def output_anki_dictation( def output_anki_dictation(
process_file: ProcessFile, results: list[DictionaryResult] process_file: ProcessFile, results: list[DictionaryResult]
) -> Path: ) -> Path:

View File

@@ -9,12 +9,14 @@ from pathlib import Path
# Local # Local
from . import DATA_FOLDER from . import DATA_FOLDER
from .anki_generation import ( from .anki_generation import (
output_anki_completed,
output_anki_dictation, output_anki_dictation,
output_anki_dictionary, output_anki_dictionary,
output_anki_package, output_anki_package,
output_anki_phrase, output_anki_phrase,
) )
from .constants import ( from .constants import (
COMPLETED_TYPE,
DICTATION_TYPE, DICTATION_TYPE,
DICT_TYPE, DICT_TYPE,
INPUT, INPUT,
@@ -24,6 +26,7 @@ from .constants import (
RESOURCES, RESOURCES,
) )
from .proccessor import ( from .proccessor import (
completed_process,
dictation_process, dictation_process,
dictionary_bulk_process, dictionary_bulk_process,
dictionary_pre_process, dictionary_pre_process,
@@ -249,6 +252,18 @@ def process_a_phrases_file(process_file: ProcessFile, language_id: str) -> Path:
return output_anki_phrase(process_file, results) return output_anki_phrase(process_file, results)
def proccess_a_completed_file(process_file: ProcessFile, language_id: str) -> Path:
"""Process a completed file"""
process_file.language_id = language_id
TTS.create_tts()
with process_file.absolute_input_file.open(
"r", encoding="utf8", newline="\n"
) as file:
text_lines = [line.strip() for line in file.readlines() if line.strip()]
results = completed_process(text_lines, process_file)
return output_anki_completed(process_file, results)
def folder_proccess(process_folder: ProcessFolder, language_id: str): def folder_proccess(process_folder: ProcessFolder, language_id: str):
process_folder.language_id = language_id process_folder.language_id = language_id
TTS.create_tts() TTS.create_tts()
@@ -278,4 +293,11 @@ def folder_proccess(process_folder: ProcessFolder, language_id: str):
) as file: ) as file:
text_lines = [line.strip() for line in file.readlines() if line.strip()] text_lines = [line.strip() for line in file.readlines() if line.strip()]
results[process_file] = translator_process(text_lines, process_file) results[process_file] = translator_process(text_lines, process_file)
elif process_file.file_type is COMPLETED_TYPE:
process_file.language_id = process_file.input_file.suffixes[1][1:]
with process_file.absolute_input_file.open(
"r", encoding="utf8", newline="\n"
) as file:
text_lines = [line.strip() for line in file.readlines() if line.strip()]
results[process_file] = completed_process(text_lines, process_file)
return output_anki_package(process_folder, results) return output_anki_package(process_folder, results)

View File

@@ -22,6 +22,7 @@ GWEN_TTS = GWEN_FOLDER / "Qwen3-TTS-12Hz-0.6B-CustomVoice"
PHRASES_TYPE = ".phrases" PHRASES_TYPE = ".phrases"
DICT_TYPE = ".dictionary" DICT_TYPE = ".dictionary"
DICTATION_TYPE = ".dictation" DICTATION_TYPE = ".dictation"
COMPLETED_TYPE = ".completed"
class LANGUAGES: class LANGUAGES:
@@ -35,7 +36,7 @@ class LANGUAGES:
TR = "tr" TR = "tr"
TH = "th" TH = "th"
JP = "jp" JP = "jp"
AvailableLanguages = (EN, ES, FR, RU, TR, TH, JP) AvailableLanguages = (ES, EN, FR, RU, TR, TH, JP)
LanguageNames = { LanguageNames = {
EN: "English", EN: "English",
ES: "Spanish", ES: "Spanish",

View File

@@ -10,7 +10,14 @@ import soundfile as sf
# Local # Local
from .constants import LANGUAGES from .constants import LANGUAGES
from .utility import CCCEDICT, TTS, DictionaryResult, ProcessFile, TranslationResult from .utility import (
CCCEDICT,
TTS,
DictionaryResult,
ProcessFile,
SimpleResult,
TranslationResult,
)
# Constants # Constants
@@ -68,7 +75,26 @@ def translator_process(
return results return results
def completed_process(
text_lines: list[str], process_file: ProcessFile
) -> list[TranslationResult]:
"""Process a complete file, not translation necesary"""
results = []
dictionary = CCCEDICT.create_cedict()
for n, line in enumerate(text_lines):
char, meaning = line.split(maxsplit=1)
entries = dictionary.get(char)
pinying = entries[0].pinyin
audio_path = process_file.resources / f"{char}.wav"
if not audio_path.is_file():
wavs, sr = TTS.generate(f"{char}")
sf.write(audio_path, wavs[0], sr)
results.append(SimpleResult(char, pinying, meaning, audio_path))
return results
def dictionary_bulk_process(words_list: list[str], process_file: ProcessFile): def dictionary_bulk_process(words_list: list[str], process_file: ProcessFile):
dictionary_en = CCCEDICT.create_cedict(LANGUAGES.EN)
dictionary = CCCEDICT.create_cedict(process_file.language_id) dictionary = CCCEDICT.create_cedict(process_file.language_id)
with process_file.dictionary_resource_file.open( with process_file.dictionary_resource_file.open(
"w", encoding="utf8", newline="" "w", encoding="utf8", newline=""
@@ -81,30 +107,25 @@ def dictionary_bulk_process(words_list: list[str], process_file: ProcessFile):
for words in words_list: for words in words_list:
word = words.split()[0] word = words.split()[0]
hint = " ".join(words.split()[1:]) if len(words.split()) > 1 else None hint = " ".join(words.split()[1:]) if len(words.split()) > 1 else None
if entries := dictionary.get(word): entries_en = dictionary_en.get(word)
if len(entries) > 1: entries = dictionary.get(word)
print(f"\nWARNING: {word} has multiple meanings:") if entries:
pos_meanings = ( pos_meanings = (
[ [
entry meaning
for entry in entries for entry, entry_en in zip(entries, entries_en)
for meaning in entry.meanings for meaning, meaning_en in zip(
if hint in meaning entry.meanings, entry_en.meanings
)
if hint in meaning_en or hint in meaning
] ]
if hint if hint
else [] else [meaning for entry in entries for meaning in entry.meanings]
) )
pos_entries = pos_meanings or entries meanings_text = "\n".join(
meanings = [] f"{n}: {meaning}" for n, meaning in enumerate(pos_meanings)
for entry in pos_entries: )
for meaning in entry.meanings: entry = entries[0]
if hint and hint in meaning:
meanings.append(meaning)
if not meanings:
meanings = [
meaning for entry in pos_entries for meaning in entry.meanings
]
meanings_text = meanings[0]
tsv_writer.writerow( tsv_writer.writerow(
{ {
"n": number, "n": number,

View File

@@ -18,6 +18,7 @@ from qwen_tts import Qwen3TTSModel
# Local # Local
from .constants import ( from .constants import (
CCCEDICT_PATH, CCCEDICT_PATH,
COMPLETED_TYPE,
DICTATION_TYPE, DICTATION_TYPE,
DICT_TYPE, DICT_TYPE,
GWEN_TTS, GWEN_TTS,
@@ -55,6 +56,7 @@ class TRANS:
) )
print(f"available packages {packages[:5]}") print(f"available packages {packages[:5]}")
packages_to_install = [] packages_to_install = []
ready = False
for in_package in packages: for in_package in packages:
if in_package.from_code == from_code: if in_package.from_code == from_code:
if in_package.to_code == to_code: if in_package.to_code == to_code:
@@ -64,7 +66,12 @@ class TRANS:
f"->{in_package.to_code}" f"->{in_package.to_code}"
) )
packages_to_install.append(in_package) packages_to_install.append(in_package)
ready = True
break break
for in_package in packages:
if ready:
break
if in_package.from_code == from_code:
for out_package in packages: for out_package in packages:
if out_package.to_code == to_code: if out_package.to_code == to_code:
if in_package.to_code == out_package.from_code: if in_package.to_code == out_package.from_code:
@@ -78,6 +85,7 @@ class TRANS:
) )
packages_to_install.append(in_package) packages_to_install.append(in_package)
packages_to_install.append(out_package) packages_to_install.append(out_package)
ready = True
break break
for package in packages_to_install: for package in packages_to_install:
print(f"instaling package {package}") print(f"instaling package {package}")
@@ -110,16 +118,17 @@ class TranslatedEntry:
@property @property
def meanings(self): def meanings(self):
"""Entry translated meaning list""" """Entry translated meaning list"""
for meaning in self.entry.meanings: if not self._translated_meanings:
if self.language_id != LANGUAGES.EN: for meaning in self.entry.meanings:
print(f"translating from {LANGUAGES.EN} to {self.language_id}") if self.language_id != LANGUAGES.EN:
print(f"-> {meaning}") print(f"translating from {LANGUAGES.EN} to {self.language_id}")
trans_meaning = argostranslate.translate.translate( print(f"-> {meaning}")
meaning, LANGUAGES.EN, self.language_id trans_meaning = argostranslate.translate.translate(
) meaning, LANGUAGES.EN, self.language_id
else: )
trans_meaning = meaning else:
self._translated_meanings.append(trans_meaning) trans_meaning = meaning
self._translated_meanings.append(trans_meaning)
return self._translated_meanings return self._translated_meanings
@@ -227,7 +236,7 @@ class ProcessFolder:
def input_files(self): def input_files(self):
input_files = [] input_files = []
for file in self.absolute_input_folder.glob(f"*.txt"): for file in self.absolute_input_folder.glob(f"*.txt"):
for file_type in (DICT_TYPE, PHRASES_TYPE, DICTATION_TYPE): for file_type in (DICT_TYPE, PHRASES_TYPE, DICTATION_TYPE, COMPLETED_TYPE):
if file_type in file.suffixes: if file_type in file.suffixes:
input_files.append( input_files.append(
ProcessFile( ProcessFile(
@@ -264,9 +273,18 @@ class ProcessFile:
# process file type # process file type
self.out_folder = OUTPUT / input_file.parent self.out_folder = OUTPUT / input_file.parent
self.out_folder.mkdir(parents=True, exist_ok=True) self.out_folder.mkdir(parents=True, exist_ok=True)
resources = RESOURCES / input_file
self.resources = resources.parent / resources.stem @property
self.resources.mkdir(parents=True, exist_ok=True) def resources(self):
resources = RESOURCES / self.input_file
if self.file_type is COMPLETED_TYPE:
resources = (
resources.parent / f"{self.input_file.name.split(".")[0]}.dictionary"
)
else:
resources = resources.parent / resources.stem
resources.mkdir(parents=True, exist_ok=True)
return resources
@property @property
def file_type(self): def file_type(self):
@@ -276,6 +294,8 @@ class ProcessFile:
return DICT_TYPE return DICT_TYPE
elif PHRASES_TYPE in self.input_file.suffixes: elif PHRASES_TYPE in self.input_file.suffixes:
return PHRASES_TYPE return PHRASES_TYPE
elif COMPLETED_TYPE in self.input_file.suffixes:
return COMPLETED_TYPE
else: else:
raise ValueError("File type not recognized") raise ValueError("File type not recognized")
@@ -320,6 +340,16 @@ class ProcessFile:
return f"Proccess:{self.input_file}" return f"Proccess:{self.input_file}"
class SimpleResult:
"""Simplied result class"""
def __init__(self, character: str, pinyin: str, meaning: str, audio_path: Path):
self.character = character
self.pinyin = pinyin
self.meaning = meaning
self.audio_path = audio_path
class TranslationResult: class TranslationResult:
"""Result of a translated process""" """Result of a translated process"""