"""Generate Anki .apkg decks from vocabulary data."""
import genanki
import random
# Stable model/deck IDs (generated once, kept constant)
_MODEL_ID = 1607392319
_DECK_ID = 2059400110
def _make_model():
"""Create an Anki note model with two card templates."""
return genanki.Model(
_MODEL_ID,
"GCSE Persian",
fields=[
{"name": "English"},
{"name": "Persian"},
{"name": "Finglish"},
{"name": "Category"},
],
templates=[
{
"name": "English → Persian",
"qfmt": '
{{English}}
'
'
{{Category}}',
"afmt": '{{FrontSide}}
'
'{{Persian}}
'
"
{{Finglish}}
",
},
{
"name": "Persian → English",
"qfmt": '{{Persian}}
'
'
{{Category}}',
"afmt": '{{FrontSide}}
'
'{{English}}
'
"
{{Finglish}}
",
},
],
css=".card { font-family: arial; text-align: center; }",
)
def export_deck(vocab, categories=None, output_path="gcse-persian.apkg"):
"""Generate an Anki .apkg deck from vocabulary entries.
Args:
vocab: List of vocabulary entries (dicts with english, persian, finglish, category).
categories: Optional list of categories to include. None = all.
output_path: Where to save the .apkg file.
Returns:
Path to the generated .apkg file.
"""
model = _make_model()
deck = genanki.Deck(_DECK_ID, "GCSE Persian")
for entry in vocab:
if categories and entry.get("category") not in categories:
continue
note = genanki.Note(
model=model,
fields=[
entry.get("english", ""),
entry.get("persian", ""),
entry.get("finglish", ""),
entry.get("category", ""),
],
guid=genanki.guid_for(entry.get("id", entry["english"])),
)
deck.add_note(note)
package = genanki.Package(deck)
package.write_to_file(output_path)
return output_path