add dictionary file process
This commit is contained in:
@@ -8,12 +8,10 @@ import random
|
||||
|
||||
# Pip
|
||||
from genanki import Deck, Model, Note, Package
|
||||
from pinyin_tone_converter.pinyin_tone_converter import PinyinToneConverter
|
||||
|
||||
# Local
|
||||
from .utility import ProcessFile, TranslationResult
|
||||
|
||||
# from pinyin_tone_converter.pinyin_tone_converter import PinyinToneConverter
|
||||
|
||||
from .utility import DictionaryResult, ProcessFile, TranslationResult
|
||||
|
||||
# Constants
|
||||
|
||||
@@ -123,33 +121,34 @@ HSK_MODEL = Model(
|
||||
# Proccess
|
||||
|
||||
|
||||
# def output_anki_dictionary(out_file, results):
|
||||
# """Creates an anki file from a dictionary results"""
|
||||
# final_file = out_file.parent / f"{out_file.stem}.apkg"
|
||||
# deck_name = "::".join(out_file.relative_to(OUTPUT).parts[:-1] + (out_file.stem,))
|
||||
# deck = Deck(random.randrange(1 << 30, 1 << 31), deck_name)
|
||||
# package = Package(deck)
|
||||
# audios = []
|
||||
# for entry, audio in results:
|
||||
# note = Note(
|
||||
# model=HSK_MODEL,
|
||||
# fields=[
|
||||
# "\n ".join(f"{n+1}. {m}" for n, m in enumerate(entry.meanings)),
|
||||
# PinyinToneConverter().convert_text(entry.pinyin),
|
||||
# entry.simplified,
|
||||
# entry.traditional,
|
||||
# f"[sound:{audio.name}]",
|
||||
# ],
|
||||
# )
|
||||
# audios.append(audio)
|
||||
# deck.add_note(note)
|
||||
# package.media_files = audios
|
||||
# package.write_to_file(final_file)
|
||||
def output_anki_dictionary(process_file: ProcessFile, results: list[DictionaryResult]):
|
||||
"""Creates an anki file from a dictionary results"""
|
||||
final_file = process_file.output_name.with_suffix(".apkg")
|
||||
deck_name = "::".join(
|
||||
process_file.input_file.parts[:-1] + (process_file.output_name.stem,)
|
||||
)
|
||||
deck = Deck(random.randrange(1 << 30, 1 << 31), deck_name)
|
||||
package = Package(deck)
|
||||
audios = []
|
||||
for result in results:
|
||||
note = Note(
|
||||
model=HSK_MODEL,
|
||||
fields=[
|
||||
"\n ".join(f"{n+1}. {m}" for n, m in enumerate(result.meanings)),
|
||||
PinyinToneConverter().convert_text(result.pinyin),
|
||||
result.simplified,
|
||||
result.traditional,
|
||||
f"[sound:{result.audio_path.name}]",
|
||||
],
|
||||
)
|
||||
audios.append(result.audio_path)
|
||||
deck.add_note(note)
|
||||
package.media_files = audios
|
||||
package.write_to_file(final_file)
|
||||
|
||||
|
||||
def output_anki_phrase(process_file: ProcessFile, results: list[TranslationResult]):
|
||||
"""Creates an anki file from a phrases results"""
|
||||
|
||||
final_file = process_file.output_name.with_suffix(".apkg")
|
||||
deck_name = "::".join(
|
||||
process_file.input_file.parts[:-1] + (process_file.output_name.stem,)
|
||||
|
||||
Reference in New Issue
Block a user