-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreateAnkiDeck.py
144 lines (125 loc) · 4.26 KB
/
createAnkiDeck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import random
import re
import math
import genanki
from createJLPTDeck import download_and_generate, parse_args
class AnkiDeck:
model = genanki.Model(
random.randrange(1 << 30, 1 << 31),
"Core Japanese Vocabulary",
fields=[
{"name": "Expression"},
{"name": "English definition"},
{"name": "Reading"},
{"name": "Grammar"},
{"name": "Additional definitions"},
],
templates=[
{
"name": "Recognition",
"qfmt": open("card_style/recognition_front.html", "r").read(),
"afmt": open("card_style/recognition_back.html", "r").read(),
},
{
"name": "Recall",
"qfmt": open("card_style/recall_front.html", "r").read(),
"afmt": open("card_style/recall_back.html", "r").read(),
},
],
css=open("card_style/style.css", "r").read(),
)
# keep a record of the notes added to avoid repeats
entries = []
# Is this deck the extended type with sound?
extended = False
def gen_decks(self):
"""
Create multiple nested decks -> common:N5::N4::N3 etc
"""
# Construct names
deck_names = []
deck_layer_names = [
"Core Japanese Vocabulary Extended"
if self.extended
else "Core Japanese Vocabulary",
"JLPT N1",
"JLPT N2",
"JLPT N3",
"JLPT N4",
"JLPT N5",
]
for i, n in enumerate(deck_layer_names):
deck_name = deck_layer_names[0]
for j in range(1, i + 1, 1):
deck_name += f"::{deck_layer_names[j]}"
deck_names.append(deck_name)
# Create decks
decks = []
for d in deck_names:
deck = genanki.Deck(random.randrange(1 << 30, 1 << 31), d)
decks.append(deck)
self.decks = decks
def __init__(self, type):
# entend the information if using extended (media sound) deck
if type == "extended":
self.extended = True
if self.extended:
self.model.name = "Core Japanese Vocabulary Extended"
self.model.fields.append({"name": "Sound"})
self.model.templates[0][
"afmt"
] += "\n\n{{Sound}}" # recognition sound on back
self.model.templates[1]["afmt"] += "\n\n{{Sound}}" # recall sound on back
self.gen_decks()
def get_deck_from_tag(self, tags):
"""
Find the easiest deck from a set of tags
"""
tags = tags.split()
possibles = [0]
for tag in tags:
found = re.search("[1-5]$", tag)
if found:
i = int(found.group())
else:
i = 0
possibles.append(i)
return max(possibles)
def add_note(self, note):
"""
Create and adds a note to the revelant deck by searching its containing tags
"""
# Ignore possible repeated entries
if note["slug"] in self.entries:
return
my_note = genanki.Note(
model=self.model,
fields=[
note["slug"],
note["english_definition"],
note["reading"],
note["grammar"],
note["additional"],
],
tags=note["jlpt"].split(),
due=str(note["index"]),
)
if self.extended:
my_note.fields.append(note["sound"] if (type(note["sound"]) == str) else "")
self.decks[self.get_deck_from_tag(note["jlpt"])].add_note(my_note)
self.entries.append(note["slug"])
def create_files(self):
"""
Creates an apkg file of the combined info
"""
# package the decks together
p_name = "Core Japanese Vocabulary Extended" if self.extended else "Core Japanese Vocabulary"
genanki.Package(self.decks).write_to_file(f"generated/{p_name}.apkg")
if __name__ == "__main__":
args = parse_args()
a = AnkiDeck(args.type)
for N in args.grades:
df = download_and_generate(N, args.type)
for index, row in df.iterrows():
a.add_note(row)
a.create_files()