-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki_cardmaker.py
132 lines (113 loc) · 5.41 KB
/
anki_cardmaker.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
def html_replacing(cardtype: str,config:dict) -> list[str]:
from os.path import join as path_join
htmls=[]
for side in ["Front.html","Back.html"]:
with open(path_join(config["cardtypes_dir"],cardtype,side),"r") as file:
html = file.read()
for key,value in config.items():
html=html.replace(key,value)
htmls.append(html)
return {"Front":htmls[0],"Back":htmls[1]}
def stop_execution(message:str) -> None:
from rich import print
from sys import exit
print(f"[bright_red](Error!) {message} [/bright_red]")
exit(1)
def sanitize_input(mother_tongue :str,target_lang:str) -> tuple[str,str]:
import langcodes
mother_tongue=mother_tongue.strip().replace(" ","_")
target_lang=target_lang.strip().replace(" ","_")
if not len(mother_tongue)==5: stop_execution("Your language is not on the 4 character format")
if not langcodes.get(mother_tongue).is_valid(): stop_execution("Your language langcode is not a valid code")
if not len(target_lang)==5 : stop_execution("Your translation language is not on the 4 character format")
if not langcodes.get(target_lang).is_valid(): stop_execution("Your translation language langcode is not a valid code")
return mother_tongue,target_lang
def generate_dictionary_link(dictionary:str,target_lang_fullname:str,
your_lang_fullname:str)-> str:
if dictionary=="reverso":
dictionary_link=f"https://context.reverso.net/translation/"
if dictionary=="cambridge":
dictionary_link="https://dictionary.cambridge.org/dictionary/"
dictionary_link+=f"{target_lang_fullname}-{your_lang_fullname}/"
return dictionary_link
def create_dummy_deck(mother_tongue :str ,target_lang :str ,
highlight_color :str,cardtypes_bools:list[bool], cardtypes_dir,
template_name:str,dictionary, additional_voices:[str]=[]):
import langcodes
import genanki
import random
LINUX_SUPPORT="AwesomeTTS"
CSS=""".card {
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
background-color: white;
}
"""
cardtypes=[]
if cardtypes_bools[0]: cardtypes.append("Passive")
if cardtypes_bools[1]: cardtypes.append("Active")
if cardtypes_bools[2]: cardtypes.append("Writing")
if len(cardtypes)==0: stop_execution("You have not selected any cardtype")
your_lang_fullname=langcodes.get(mother_tongue).describe()["language"].lower()
your_lang=langcodes.get(mother_tongue).to_tag().replace("-","_")
target_lang_fullname=langcodes.get(target_lang).describe()["language"].lower()
target_lang=langcodes.get(target_lang).to_tag().replace("-","_")
FIELDS=(f"🔤 Phrase ({target_lang})",f"🔄 Translated phrase or word ({your_lang})",
f"❓ Unknown word ({target_lang})",f"🗣️ Pronunciation")
if additional_voices: LINUX_SUPPORT=","+LINUX_SUPPORT
your_voices=additional_voices+LINUX_SUPPORT
dictionary_link=generate_dictionary_link(dictionary,
target_lang_fullname,your_lang_fullname)
config={"your_voices":your_voices,"your_lang":your_lang,
"target_lang":target_lang,"highlight_color":highlight_color,
"dictionary_link":dictionary_link,"cardtypes_dir":cardtypes_dir}
cardtype_htmls={
cardtype:html_replacing(cardtype,config) for cardtype in cardtypes}
cardtypes_templates=[]
for cardtype in cardtype_htmls:
cardtypes_templates.append(
{"name":cardtype,
"qfmt":cardtype_htmls[cardtype]["Front"],
"afmt":cardtype_htmls[cardtype]["Back"],
})
note_template=genanki.Model(
random.randrange(1<<30,1<<31),
template_name,
fields=[
{"name":FIELDS[0]},{"name":FIELDS[1]},
{"name":FIELDS[2]},{"name":FIELDS[3]}
],
templates=cardtypes_templates,
css=CSS
)
dummy_deck=genanki.Deck(
random.randrange(1<<30,1<<31),
f"{template_name} (test the audio, delete it after have tested)"
)
dummy_note=genanki.Note(
model=note_template,
fields=["This is just an example to test the voices",
"something that is typical of the group of things that you are talking about",
"example"," /ɪɡˈzæm·pəl/"]
)
dummy_deck.add_note(dummy_note)
genanki.Package(dummy_deck).write_to_file(f"{template_name}.apkg")
if __name__=="__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mother_tongue', type=str,required=True)
parser.add_argument('--target_lang',type=str,required=True)
parser.add_argument("--cardtypes_dir",type=str,required=True)
parser.add_argument("--template_name",type=str,required=True)
parser.add_argument("--highlight_color",type=str,default="red")
parser.add_argument("--cardtypes_bools",type=str)
parser.add_argument("--dictionary",type=str,default="reverso")
parser.add_argument("--additional_voices",type=str,default="")
args = parser.parse_args()
cardtypes_bools=[x=="True" for x in args.cardtypes_bools.split()]
mother_tongue,target_lang=sanitize_input(args.mother_tongue,args.target_lang)
create_dummy_deck(mother_tongue,target_lang,args.highlight_color,
cardtypes_bools,args.cardtypes_dir,args.template_name,
args.dictionary,args.additional_voices)