-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictcc2anki.py
101 lines (84 loc) · 3.19 KB
/
dictcc2anki.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
from tkinter import *
from tkinter import ttk
import sys, re
from anki import Collection
from anki.importing import TextImporter
import csv, os, re
from dictcc import Dict, AVAILABLE_LANGUAGES
collection_path = 'path/to/your/collection.anki2' #change according to your file path.
def ensure_unicode(string):
if hasattr(string, "decode"):
return string.decode("utf-8")
return string
#searching the translation of the entered word
def run(event):
translation_lb.delete(0, END)
word = in_word_entry.get()
input_language = in_lan_cb.get()
output_language = out_lan_cb.get()
max_results = 50
result = Dict.translate(word,
input_language,
output_language)
if not result.n_results:
print("No results found for {} ({} <-> {}).".format(
word, input_language, output_language))
return
for i, (input_word, output_word) in enumerate(result.translation_tuples):
print_translation(output_word)
if i == max_results:
break
def print_translation(output_word):
translation_lb.insert(END, output_word)
#adding the flashcard to anki
def make_cards(event):
card_type = 'Basic (and reversed card)' #can be changed to just Basic if you want to learn one directional
deck_name = 'Vocabulary' #change for outher deck name
card_front = str(in_word_entry.get())
card_back = str(translation_lb.get(translation_lb.curselection()[0]))
deck = Collection(collection_path);
deck_id = deck.decks.id(deck_name)
deck.decks.select( deck_id )
model = deck.models.byName( card_type )
model['did']=deck_id
deck.models.save( model )
deck.models.setCurrent( model )
fact = deck.newNote()
fact['Front'] = card_front
fact['Back'] = card_back
deck.addNote( fact )
deck.save()
deck.close()
print("{} <-> {} card was added".format(card_front, card_back))
in_word_entry.delete(0, END)
#guisetup
window = Tk()
window.config(bg="WHITE")
lan = ['de', 'en', 'ro', 'it', 'fr', 'ru', 'bg', 'pt', 'es'] #avaliable languages
in_word_entry = ttk.Entry(window, width=54)
in_lan_label = Label(window, text='Input Language')
out_lan_label = Label(window, text='Output Language')
in_lan_label.config(bg="WHITE")
out_lan_label.config(bg="WHITE")
in_lan_var = StringVar(window)
in_lan_var.set('en') #preset input language
out_lan_var = StringVar(window)
out_lan_var.set('de') #preset output language
in_lan_cb = ttk.Combobox(window, textvariable=in_lan_var)
in_lan_cb['values'] = lan
out_lan_cb = ttk.Combobox(window, textvariable=out_lan_var)
in_lan_cb.config(width=4)
out_lan_cb.config(width=4)
translation_lb = Listbox(window, width=54)
add_to_anki_btn = ttk.Button(window, text='Add to Anki', width=54)
in_word_entry.grid(column=1, row=0, columnspan=2)
in_lan_label.grid(column=1, row=1, sticky=W)
out_lan_label.grid(column=1, row= 2, sticky=W)
in_lan_cb.grid(column=2, row=1, sticky=E)
out_lan_cb.grid(column=2, row=2, sticky=E)
translation_lb.grid(column=1, row=3, columnspan=2)
add_to_anki_btn.grid(column=1, row=4, columnspan=2)
#event binding
add_to_anki_btn.bind('<Button-1>', make_cards)
in_word_entry.bind('<Return>', run)
window.mainloop()