-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_deck.py
151 lines (142 loc) · 5.37 KB
/
generate_deck.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
145
146
147
148
149
150
151
import copy
import csv
import os
import genanki
def generate_deck():
result_dict = []
with open("./data/source_examples.csv", encoding="UTF8") as csvfile:
reader = csv.reader(csvfile, delimiter="\t")
for row in reader:
template_dict = dict.fromkeys(
[
"Hanzi",
"Pinyin",
"Pinyin RAW",
"English",
"Kanxi Number",
"Alternatives",
"Sound",
"Ancient Character",
"Stroke Order",
"Examples",
]
)
for i, key in enumerate(template_dict.keys()):
template_dict[key] = row[i]
result_dict.append(template_dict)
print(result_dict[0])
my_model = genanki.Model(
1087741751,
"Chinese (Radicals)",
fields=[
{"name": "Hanzi"},
{"name": "Pinyin"},
{"name": "English"},
{"name": "Kanxi Number"},
{"name": "Alternatives"},
{"name": "Sound"},
{"name": "Ancient Character"},
{"name": "Stroke Order"},
{"name": "Examples"},
],
templates=[
{
"name": "Recall",
"qfmt": "\n".join(
[
"<div class=container>",
"<div class=radical_number>Radical Number: {{Kanxi Number}}</div>",
"<div class=tags>{{Deck}} {{#Tags}} -- {{/Tags}}{{Tags}}</div>",
"</div>",
"<div class=chinese> {{Hanzi}}</div>",
"{{#Alternatives}}<div class=note>Alternatives: <div class=chinese>{{Alternatives}}</div></div>{{/Alternatives}}",
]
),
"afmt": "\n".join(
[
"{{FrontSide}}",
"<hr>",
"<div>{{English}}</div>",
"<br>",
"<div class=reading>{{Pinyin}}</div>",
"<div class=note>Examples:{{Examples}}</div>",
"{{#Ancient Character}}<div class=note>Ancient Character:</div>{{Ancient Character}}{{/Ancient Character}}",
"{{#Stroke Order}}<div class=note>Stroke Order:</div>{{Stroke Order}}{{/Stroke Order}}",
"<br>",
"{{Sound}}",
]
),
},
],
css="\n".join(
[
".card {",
"font-family: arial;",
"font-size: 20px;",
"text-align: center;",
"color: black;",
"background-color: white;",
"word-wrap: break-word;",
"}",
"img {",
"border: 1px solid #ddd;",
"border-radius: 4px;",
"padding: 5px;",
"width: 150px;",
"}",
'.win .chinese { font-family: "MS Mincho", "MS 明朝"; }',
".mac .chinese { }",
'.linux .chinese { font-family: "Kochi Mincho", "東風明朝"; }',
'.mobile .chinese { font-family: "Hiragino Mincho ProN"; }',
".chinese { font-size: 30px;}",
".reading { font-size: 20px;}",
".comment {font-size: 15px; color:grey;}",
".tags {color:gray;text-align:right;font-size:10pt;}",
".radical_number {color:gray;text-align:left;font-size:10pt;}",
".note {color:gray;font-size:12pt;margin-top:20pt;}",
".hint {font-size:12pt;}",
"",
".tone1 {color: red;}",
".tone2 {color: orange;}",
".tone3 {color: green;}",
".tone4 {color: blue;}",
".tone5 {color: gray;}",
"",
".container {",
" display: grid;",
" grid-gap: 1rem;",
" grid-template-columns: grid-template-columns: repeat(2, 1fr);",
' grid-template-areas:"a b"',
"}",
]
),
)
my_deck = genanki.Deck(1540858611, "Chinese Radicals Deck+")
for item in result_dict:
my_note = genanki.Note(
model=my_model,
fields=[
item["Hanzi"],
item["Pinyin"],
item["English"],
item["Kanxi Number"],
item["Alternatives"],
item["Sound"],
item["Ancient Character"],
item["Stroke Order"],
item["Examples"],
],
)
my_deck.add_note(copy.deepcopy(my_note))
my_package = genanki.Package(my_deck)
media_img_files = ["./media/img/" + i for i in os.listdir("./media/img")]
media_audio_files = ["./media/audio/" + i for i in os.listdir("./media/audio")]
media_stroke_order_files = [
"./media/stroke_order/" + i for i in os.listdir("./media/stroke_order")
]
my_package.media_files = (
media_img_files + media_audio_files + media_stroke_order_files
)
my_package.write_to_file("./decks/Chinese-Radicals-Deck-Plus.apkg")
if __name__ == "__main__":
generate_deck()