add dictionary file process
This commit is contained in:
@@ -11,7 +11,7 @@ from pathlib import Path
|
||||
import argostranslate.package
|
||||
import argostranslate.translate
|
||||
import torch
|
||||
from cedict_utils.cedict import CedictParser
|
||||
from cedict_utils.cedict import CedictEntry, CedictParser
|
||||
from chatterbox.mtl_tts import ChatterboxMultilingualTTS
|
||||
|
||||
# Local
|
||||
@@ -34,9 +34,9 @@ class TRANS:
|
||||
TRANS.PACKAGES = argostranslate.package.get_available_packages()
|
||||
TRANS.UPDATED = True
|
||||
packages = filter(
|
||||
lambda x: x.from_code == from_code or x.to_code == to_code,
|
||||
TRANS.PACKAGES,
|
||||
)
|
||||
lambda x: x.from_code == from_code or x.to_code == to_code,
|
||||
TRANS.PACKAGES,
|
||||
)
|
||||
packages_to_install = []
|
||||
for in_package in packages:
|
||||
if in_package.from_code == from_code:
|
||||
@@ -49,6 +49,43 @@ class TRANS:
|
||||
argostranslate.package.install_from_path(package.download())
|
||||
|
||||
|
||||
class TranslatedEntry:
|
||||
"""Holder class for CCCEDIT entry translated to `language_id`"""
|
||||
|
||||
def __init__(self, entry: CedictEntry, language_id: str):
|
||||
self.entry = entry
|
||||
self.language_id = language_id
|
||||
self._translated_meanings = []
|
||||
for meaning in entry.meanings:
|
||||
if language_id != LANGUAGES.EN:
|
||||
trans_meaning = argostranslate.translate.translate(
|
||||
meaning, LANGUAGES.EN, language_id
|
||||
)
|
||||
else:
|
||||
trans_meaning = meaning
|
||||
self._translated_meanings.append(trans_meaning)
|
||||
|
||||
@property
|
||||
def simplified(self):
|
||||
"""Entry simplified"""
|
||||
return self.entry.simplified
|
||||
|
||||
@property
|
||||
def traditional(self):
|
||||
"""Entry traditional"""
|
||||
return self.entry.traditional
|
||||
|
||||
@property
|
||||
def pinyin(self):
|
||||
"""Entry piying"""
|
||||
return self.entry.pinyin
|
||||
|
||||
@property
|
||||
def meanings(self):
|
||||
"""Entry translated meaning list"""
|
||||
return self._translated_meanings
|
||||
|
||||
|
||||
class CCCEDICT:
|
||||
"""Static Class for the CCCEDIT dictionary"""
|
||||
|
||||
@@ -57,7 +94,9 @@ class CCCEDICT:
|
||||
DICTIONARY_LIST = {}
|
||||
|
||||
@staticmethod
|
||||
def create_cedict(language_id=LANGUAGES.EN):
|
||||
def create_cedict(
|
||||
language_id: str = LANGUAGES.EN,
|
||||
) -> dict[str, list[TranslatedEntry]]:
|
||||
"""Creates a create_cedict dictionary object"""
|
||||
if not CCCEDICT.PARSER:
|
||||
CCCEDICT.PARSER = CedictParser()
|
||||
@@ -66,15 +105,11 @@ class CCCEDICT:
|
||||
if language_id not in CCCEDICT.DICTIONARY_LIST:
|
||||
dictionary = {}
|
||||
for entry in CCCEDICT.ENTRIES:
|
||||
if language_id != LANGUAGES.EN:
|
||||
TRANS.create_translator(LANGUAGES.EN, language_id)
|
||||
entry = argostranslate.translate.translate(
|
||||
entry, LANGUAGES.EN, language_id
|
||||
)
|
||||
trans_entry = TranslatedEntry(entry, language_id)
|
||||
if entry.simplified not in dictionary:
|
||||
dictionary[entry.simplified] = [entry]
|
||||
dictionary[entry.simplified] = [trans_entry]
|
||||
else:
|
||||
dictionary[entry.simplified].append(entry)
|
||||
dictionary[entry.simplified].append(trans_entry)
|
||||
CCCEDICT.DICTIONARY_LIST[language_id] = dictionary
|
||||
else:
|
||||
dictionary = CCCEDICT.DICTIONARY_LIST[language_id]
|
||||
@@ -144,6 +179,11 @@ class ProcessFile:
|
||||
raise ValueError("Not a valid language selected")
|
||||
return self.out_folder / f"{self.input_file.stem}.{self.language_id}."
|
||||
|
||||
@property
|
||||
def dictionary_resource_file(self):
|
||||
"""The path for the resource tsv for dictionary files"""
|
||||
return self.resources / f"dictionary.{self.language_id}.tsv"
|
||||
|
||||
|
||||
class TranslationResult:
|
||||
"""Result of a translated process"""
|
||||
@@ -159,3 +199,23 @@ class TranslationResult:
|
||||
self.translated = translated
|
||||
self.line = line
|
||||
self.audio_path = audio_path
|
||||
|
||||
|
||||
class DictionaryResult:
|
||||
"""Result of a dictionaty process"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
language_id: str,
|
||||
simplified: str,
|
||||
traditional: str,
|
||||
pinyin: str,
|
||||
meanings: str,
|
||||
audio_path: Path,
|
||||
):
|
||||
self.language_id = language_id
|
||||
self.simplified = simplified
|
||||
self.traditional = traditional
|
||||
self.pinyin = pinyin
|
||||
self.meanings = meanings
|
||||
self.audio_path = audio_path
|
||||
|
||||
Reference in New Issue
Block a user