Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Last active September 17, 2024 00:52
Show Gist options
  • Save fearofcode/0b5688e8cc8ba6f205ee4a025b87d0d8 to your computer and use it in GitHub Desktop.
Save fearofcode/0b5688e8cc8ba6f205ee4a025b87d0d8 to your computer and use it in GitHub Desktop.
Simple anki export of a Plover steno dictionary using the genanki (https://github.com/kerrickstaley/genanki) library
# Copyright (C) 2024 Warren Henning
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import random
import sys
from collections import defaultdict
import genanki
if __name__ == "__main__":
if len(sys.argv) < 3 or sys.argv[1] == "--help":
print("Usage: export.py <JSON path> <export name>")
sys.exit(1)
json_file_path = sys.argv[1]
export_name = sys.argv[2]
print(f"Reading in {json_file_path}")
with open(json_file_path, "r") as f:
data: dict = json.load(f)
model_id = random.randrange(1 << 30, 1 << 31)
while True:
deck_id = random.randrange(1 << 30, 1 << 31)
if deck_id != model_id:
break
deck = genanki.Deck(deck_id, f"{export_name} export")
model = genanki.Model(
model_id,
'Steno Model',
fields=[
{'name': 'Word'},
{'name': 'Outline'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '<h1>{{Word}}</h1>',
'afmt': '<h1>{{FrontSide}}</h1><br/><hr id="answer"><br/><h1>{{Outline}}</h1>',
},
])
words = defaultdict(list)
# group up all outlines for a given word into a list
for key, value in data.items():
words[value].append(key)
for key, value in words.items():
note = genanki.Note(
model=model,
fields=[key, " | ".join(value)]
)
deck.add_note(note)
package_name = f"{export_name}.apkg"
print(f"Writing exported entries out to {package_name}")
genanki.Package(deck).write_to_file(package_name)
print("Done")
cached-property==1.5.2
chevron==0.14.0
frozendict==2.4.4
genanki==0.13.1
PyYAML==6.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment