version 0.1
This commit is contained in:
159
src/anki_hsk_creator/anki_generation.py
Normal file
159
src/anki_hsk_creator/anki_generation.py
Normal file
@@ -0,0 +1,159 @@
|
||||
"""anki_generation.py
|
||||
|
||||
Produces anki output
|
||||
"""
|
||||
|
||||
# Standard Library
|
||||
import random
|
||||
|
||||
# Pip
|
||||
from genanki import Deck, Model, Note, Package
|
||||
|
||||
# Local
|
||||
from .utility import ProcessFile, TranslationResult
|
||||
|
||||
# from pinyin_tone_converter.pinyin_tone_converter import PinyinToneConverter
|
||||
|
||||
|
||||
# Constants
|
||||
|
||||
CSS = """
|
||||
.card {
|
||||
font-family: arial;
|
||||
font-size: 20px;
|
||||
text-align: center;
|
||||
color: black;
|
||||
background-color: white;
|
||||
}
|
||||
.simple {
|
||||
font-family: Arial;
|
||||
font-size: 100px;
|
||||
}
|
||||
.trad {
|
||||
font-family: Arial;
|
||||
font-size: 75px;
|
||||
}
|
||||
"""
|
||||
|
||||
# Models
|
||||
|
||||
PHRASE_MODEL = Model(
|
||||
2076166425,
|
||||
"Phrase Model",
|
||||
fields=[
|
||||
{"name": "Translated"},
|
||||
{"name": "Phrase"},
|
||||
{"name": "Audio"},
|
||||
],
|
||||
templates=[
|
||||
{
|
||||
"name": "Card 1",
|
||||
"qfmt": "{{Translated}}<br>{{Audio}}",
|
||||
"afmt": '{{FrontSide}}<hr id="answer">{{Phrase}}',
|
||||
},
|
||||
{
|
||||
"name": "Card 2",
|
||||
"qfmt": "{{Phrase}}<br>{{Audio}}",
|
||||
"afmt": '{{FrontSide}}<hr id="answer">{{Translated}}',
|
||||
},
|
||||
{
|
||||
"name": "Card 3",
|
||||
"qfmt": "{{Audio}}",
|
||||
"afmt": '{{FrontSide}}<hr id="answer">{{Phrase}}',
|
||||
},
|
||||
],
|
||||
css=CSS,
|
||||
)
|
||||
|
||||
|
||||
HSK_MODEL = Model(
|
||||
1708536519,
|
||||
"HSK Model",
|
||||
fields=[
|
||||
{"name": "English"},
|
||||
{"name": "Pinyin"},
|
||||
{"name": "Simplified"},
|
||||
{"name": "Traditional"},
|
||||
{"name": "Audio"},
|
||||
],
|
||||
templates=[
|
||||
{
|
||||
"name": "Card 1",
|
||||
"qfmt": "<strong>{{Pinyin}}</strong><br>{{English}}<br>{{Audio}}",
|
||||
"afmt": (
|
||||
"{{FrontSide}}<hr id='answer''><div class='simple'>{{Simplified}}</div>"
|
||||
"<br><div class='trad'>{{Traditional}}</div>"
|
||||
),
|
||||
},
|
||||
{
|
||||
"name": "Card 2",
|
||||
"qfmt": "<div class='simple'>{{Simplified}}</div><br><div class='trad'>"
|
||||
"{{Traditional}}</div>",
|
||||
"afmt": (
|
||||
"{{FrontSide}}<hr id='answer'><strong>{{Pinyin}}</strong>"
|
||||
"<br>{{English}}<br>{{Audio}}"
|
||||
),
|
||||
},
|
||||
{
|
||||
"name": "Card 3",
|
||||
"qfmt": "{{Audio}}",
|
||||
"afmt": (
|
||||
"{{FrontSide}}<hr id='answer'><strong>{{Pinyin}}</strong>"
|
||||
"<br><div class='simple'>{{Simplified}}</div>"
|
||||
"<br><div class='trad'>{{Traditional}}</div>"
|
||||
),
|
||||
},
|
||||
],
|
||||
css=CSS,
|
||||
)
|
||||
|
||||
# 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_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.input_fil.stem,)
|
||||
)
|
||||
deck = Deck(random.randrange(1 << 30, 1 << 31), deck_name)
|
||||
package = Package(deck)
|
||||
audios = []
|
||||
for result in results:
|
||||
note = Note(
|
||||
model=PHRASE_MODEL,
|
||||
fields=[
|
||||
result.translated,
|
||||
result.line,
|
||||
f"[sound:{result.audio_path.name}]",
|
||||
],
|
||||
)
|
||||
deck.add_note(note)
|
||||
audios.append(result.audio_path)
|
||||
package.media_files = audios
|
||||
package.write_to_file(final_file)
|
||||
Reference in New Issue
Block a user