solve problerms with menaing trasnlations,
mightneed to change trasnlation engine
This commit is contained in:
@@ -124,39 +124,49 @@ def main():
|
||||
break
|
||||
elif option == "s":
|
||||
input_file = cli_select_files()
|
||||
if DICT_TYPE in input_file.input_file.suffixes:
|
||||
if DICT_TYPE is input_file.file_type:
|
||||
dict_selected = cli_select_dictionay_tsv()
|
||||
if dict_selected:
|
||||
language_id = cli_select_language()
|
||||
print(
|
||||
f"pre-processing file {input_file} with language {language_id}"
|
||||
f"pre-processing {DICT_TYPE} file {input_file} with language {language_id}"
|
||||
)
|
||||
pre_process_a_dictionary_file(input_file, language_id)
|
||||
else:
|
||||
print(f"Processing file {input_file} with language {language_id}")
|
||||
print(
|
||||
f"Processing {DICT_TYPE} file {input_file} with language {language_id}"
|
||||
)
|
||||
language_id = cli_select_language(
|
||||
input_file.available_dictionary_languages
|
||||
)
|
||||
process_a_dictionary_file(input_file, language_id)
|
||||
elif PHRASES_TYPE in input_file.input_file.suffixes:
|
||||
elif PHRASES_TYPE is input_file.file_type:
|
||||
language_id = cli_select_language()
|
||||
print(f"processing file {input_file} with language {language_id}")
|
||||
print(
|
||||
f"processing {PHRASES_TYPE} file {input_file} with language {language_id}"
|
||||
)
|
||||
process_a_phrases_file(input_file, language_id)
|
||||
elif DICTATION_TYPE in input_file.input_file.suffixes:
|
||||
elif DICTATION_TYPE is input_file.file_type:
|
||||
language_id = cli_select_language()
|
||||
print(f"processing file {input_file} with language {language_id}")
|
||||
print(
|
||||
f"processing {DICTATION_TYPE} file {input_file} with language {language_id}"
|
||||
)
|
||||
process_a_dictation_file(input_file, language_id)
|
||||
elif COMPLETED_TYPE in input_file.input_file.suffixes:
|
||||
elif COMPLETED_TYPE is input_file.file_type:
|
||||
language_id = input_file.input_file.suffixes[1][1:]
|
||||
print(f"processing file {input_file} with language {language_id}")
|
||||
print(
|
||||
f"processing {COMPLETED_TYPE} file {input_file} with language {language_id}"
|
||||
)
|
||||
proccess_a_completed_file(input_file, language_id)
|
||||
else:
|
||||
print(f"File {input_file} is not recognised")
|
||||
elif option == "f":
|
||||
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()
|
||||
for language_id in langs:
|
||||
print(f"Selected: {in_folder} with language {language_id}")
|
||||
for dirpath, dirnames, filenames in tuple(
|
||||
in_folder.absolute_input_folder.walk()
|
||||
|
||||
@@ -12,7 +12,7 @@ from genanki import Deck, Model, Note, Package
|
||||
from pinyin_tone_converter.pinyin_tone_converter import PinyinToneConverter
|
||||
|
||||
# Local
|
||||
from .constants import DICTATION_TYPE, DICT_TYPE, PHRASES_TYPE
|
||||
from .constants import COMPLETED_TYPE, DICTATION_TYPE, DICT_TYPE, PHRASES_TYPE
|
||||
from .utility import (
|
||||
DictionaryResult,
|
||||
ProcessFile,
|
||||
@@ -377,7 +377,21 @@ def output_anki_package(
|
||||
)
|
||||
deck.add_note(note)
|
||||
audios.append(result.audio_path)
|
||||
elif process_file.file_type is COMPLETED_TYPE:
|
||||
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)
|
||||
decks.append(deck)
|
||||
if decks:
|
||||
package = Package(decks)
|
||||
package.media_files = audios
|
||||
package.write_to_file(final_file)
|
||||
|
||||
@@ -266,12 +266,18 @@ def proccess_a_completed_file(process_file: ProcessFile, language_id: str) -> Pa
|
||||
|
||||
def folder_proccess(process_folder: ProcessFolder, language_id: str):
|
||||
process_folder.language_id = language_id
|
||||
print(f"Proccesing folder {process_folder} with language {language_id}")
|
||||
final_file = process_folder.output_name.with_suffix(".apkg")
|
||||
if final_file.is_file():
|
||||
print("File already proccessed")
|
||||
return final_file
|
||||
TTS.create_tts()
|
||||
TRANS.create_translator(LANGUAGES.CN, language_id)
|
||||
CCCEDICT.create_cedict(language_id)
|
||||
results = {}
|
||||
for process_file in process_folder.input_files:
|
||||
print(f"Proccessing {process_file}")
|
||||
try:
|
||||
if process_file.file_type is DICT_TYPE:
|
||||
with process_file.absolute_input_file.open(
|
||||
"r", encoding="utf8", newline="\n"
|
||||
@@ -291,13 +297,23 @@ def folder_proccess(process_folder: ProcessFolder, language_id: str):
|
||||
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()]
|
||||
text_lines = [
|
||||
line.strip() for line in file.readlines() if line.strip()
|
||||
]
|
||||
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:]
|
||||
completed_language_id = process_file.input_file.suffixes[1][1:]
|
||||
if completed_language_id != language_id:
|
||||
continue
|
||||
process_file.language_id = completed_language_id
|
||||
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()]
|
||||
text_lines = [
|
||||
line.strip() for line in file.readlines() if line.strip()
|
||||
]
|
||||
results[process_file] = completed_process(text_lines, process_file)
|
||||
except AttributeError, AssertionError:
|
||||
print(f"Error procesing {process_file} with language {language_id}")
|
||||
continue
|
||||
return output_anki_package(process_folder, results)
|
||||
|
||||
@@ -35,8 +35,11 @@ class LANGUAGES:
|
||||
RU = "ru"
|
||||
TR = "tr"
|
||||
TH = "th"
|
||||
JP = "jp"
|
||||
AvailableLanguages = (ES, EN, FR, RU, TR, TH, JP)
|
||||
JA = "ja"
|
||||
KO = "ko"
|
||||
PB = "pb"
|
||||
VI = "vi"
|
||||
AvailableLanguages = (ES, EN, FR, RU, TR, TH, JA, KO, PB, VI)
|
||||
LanguageNames = {
|
||||
EN: "English",
|
||||
ES: "Spanish",
|
||||
@@ -44,5 +47,8 @@ class LANGUAGES:
|
||||
RU: "Russian",
|
||||
TR: "Turkish",
|
||||
TH: "Thai",
|
||||
JP: "Japanese",
|
||||
JA: "Japanese",
|
||||
KO: "Korean",
|
||||
PB: "Portuguese (Brazil)",
|
||||
VI: "Vietnamese",
|
||||
}
|
||||
|
||||
@@ -106,24 +106,24 @@ def dictionary_bulk_process(words_list: list[str], process_file: ProcessFile):
|
||||
number = 1
|
||||
for words in words_list:
|
||||
word = words.split()[0]
|
||||
hint = " ".join(words.split()[1:]) if len(words.split()) > 1 else None
|
||||
hint = words.split(maxsplit=1)[1] if len(words.split()) > 1 else None
|
||||
entries_en = dictionary_en.get(word)
|
||||
entries = dictionary.get(word)
|
||||
if entries:
|
||||
pos_meanings = (
|
||||
[
|
||||
all_meanings = [meaning for entry in entries for meaning in entry.meanings]
|
||||
pos_meanings = [
|
||||
meaning
|
||||
for entry, entry_en in zip(entries, entries_en)
|
||||
for meaning, meaning_en in zip(
|
||||
entry.meanings, entry_en.meanings
|
||||
)
|
||||
if hint in meaning_en or hint in meaning
|
||||
if hint and (hint in meaning_en or hint in meaning)
|
||||
]
|
||||
if hint
|
||||
else [meaning for entry in entries for meaning in entry.meanings]
|
||||
)
|
||||
pos_meanings = pos_meanings or all_meanings
|
||||
if not pos_meanings:
|
||||
raise ValueError(f"Not menaing found for hint {hint}, {all_meanings}")
|
||||
meanings_text = "\n".join(
|
||||
f"{n}: {meaning}" for n, meaning in enumerate(pos_meanings)
|
||||
f"{n+1}: {meaning}" for n, meaning in enumerate(pos_meanings)
|
||||
)
|
||||
entry = entries[0]
|
||||
tsv_writer.writerow(
|
||||
@@ -212,7 +212,6 @@ def dictionary_process(process_file: ProcessFile) -> list[DictionaryResult]:
|
||||
if not audio_path.exists():
|
||||
wavs, sr = TTS.generate(f"{line['simplified']}。")
|
||||
sf.write(audio_path, wavs[0], sr)
|
||||
print(line)
|
||||
result = DictionaryResult(
|
||||
**line, audio_path=audio_path, language_id=process_file.language_id
|
||||
)
|
||||
|
||||
@@ -54,7 +54,6 @@ class TRANS:
|
||||
TRANS.PACKAGES,
|
||||
)
|
||||
)
|
||||
print(f"available packages {packages[:5]}")
|
||||
packages_to_install = []
|
||||
ready = False
|
||||
for in_package in packages:
|
||||
@@ -121,11 +120,10 @@ class TranslatedEntry:
|
||||
if not self._translated_meanings:
|
||||
for meaning in self.entry.meanings:
|
||||
if self.language_id != LANGUAGES.EN:
|
||||
print(f"translating from {LANGUAGES.EN} to {self.language_id}")
|
||||
print(f"-> {meaning}")
|
||||
trans_meaning = argostranslate.translate.translate(
|
||||
meaning, LANGUAGES.EN, self.language_id
|
||||
)
|
||||
print(f"{meaning}-> {trans_meaning}")
|
||||
else:
|
||||
trans_meaning = meaning
|
||||
self._translated_meanings.append(trans_meaning)
|
||||
@@ -318,6 +316,8 @@ class ProcessFile:
|
||||
"""Posible name for the output file, still missing the filetype"""
|
||||
if self.language_id is None:
|
||||
raise ValueError("Not a valid language selected")
|
||||
if self.file_type is COMPLETED_TYPE:
|
||||
return self.out_folder / f"{self.input_file.stem}.temp"
|
||||
return self.out_folder / f"{self.input_file.stem}.{self.language_id}.temp"
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user