-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwords.py
142 lines (116 loc) · 5.15 KB
/
words.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
from googletrans import Translator
import wordfreq
import random
import threading
import pygame as py
import os
import pickle
lang_dict = {'Bulgarian': 'bg', 'Catalan': 'ca',
'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'Finnish': 'fi', 'French': 'fr',
'German': 'de', 'Greek': 'el', 'Hungarian': 'hu', 'Indonesian': 'id',
'Italian': 'it', 'Latvian': 'lv', 'Macedonian': 'mk', 'Malay': 'ms',
'Polish': 'pl', 'Portuguese': 'pt', 'Romanian': 'ro', 'Russian': 'ru',
'Spanish': 'es', 'Swedish': 'sv', 'Turkish': 'tr', 'Ukrainian': 'uk'}
translator = Translator()
# with open("list.txt") as f:
# r = f.read().split("\n")
# r = [list(filter(lambda x: x!="", l.split(" "))) for l in r]
# r = [l[0:2] for l in r]
# for l in r:``
# lang_dict[l[0]] = l[1]
# print(lang_dict)
def get_word_pair(language):
lang = lang_dict[language]
try:
lang_word = wordfreq.random_words(lang, nwords=1)
while lang_word is None:
lang_word = wordfreq.random_words(lang, nwords=1)
eng_word = translator.translate(lang_word, src=lang, dest='en').text
# Recursion happening here
if len(lang_word) > 10 or len(eng_word) > 10 or (len(lang_word) == 1 and len(eng_word) == 1) \
or (language != "English" and lang_word == eng_word) or lang_word[0].isupper():
lang_word, eng_word = get_word_pair(language)
except:
with open("words.pkl", "rb") as f:
dictionary = pickle.load(f)
lang_word, eng_word = random.choice(dictionary[language])
return lang_word, eng_word
def create_local_file(num):
save_dict = {}
for language, lang in lang_dict.items():
save_dict[language] = []
for j in range(num):
lang_word = wordfreq.random_words(lang, nwords=1)
while lang_word is None:
lang_word = wordfreq.random_words(lang, nwords=1)
eng_word = translator.translate(lang_word, src=lang, dest='en').text
# Recursion happening here
if len(lang_word) > 10 or len(eng_word) > 10 or (len(lang_word) == 1 and len(eng_word) == 1) \
or (language != "English" and lang_word == eng_word) or lang_word[0].isupper():
lang_word, eng_word = get_word_pair(language)
save_dict[language].append((lang_word, eng_word))
with open("words.pkl", "wb") as f:
pickle.dump(save_dict, f)
class WordGenerator:
def __init__(self, language, stats_collector, reversed=False):
self.learnt_sound = py.mixer.Sound(os.path.join("sounds", "learnt.wav"))
self.reverse = reversed
self.stats_collector = stats_collector
self.language = language
self.dict = {}
self.correct_row_dict = {}
self.wrong_list = []
self.use_wrong_list = False
for i in range(20):
pair = get_word_pair(language)
self.dict[pair[0]] = pair[1]
def correct(self, word):
event = ["correct"]
if self.use_wrong_list:
self.wrong_list.remove(word)
if len(self.wrong_list) == 0:
self.use_wrong_list = False
else:
if word in self.correct_row_dict.keys():
self.correct_row_dict[word] += 1
if self.correct_row_dict[word] == 3:
### learnt!! ###
py.mixer.Sound.play(self.learnt_sound)
if not self.reverse:
del self.dict[word]
else:
self.dict = {key: value for key, value in self.dict.items() if value != word}
while word in self.wrong_list:
wrong_list.remove(word)
event.append("learnt")
def temp():
pair = get_word_pair(self.language)
self.dict[pair[0]] = pair[1]
t = threading.Thread(target=temp)
t.start()
else:
self.correct_row_dict[word] = 1
self.stats_collector.events.append(event)
def wrong(self, word):
event = ["wrong"]
if word in self.correct_row_dict.keys():
del self.correct_row_dict[word]
if not self.use_wrong_list:
self.wrong_list.append(word)
if len(self.wrong_list) >= 4:
self.use_wrong_list = True
self.stats_collector.events.append(event)
def get_word(self, answer=False):
if not self.reverse:
if self.use_wrong_list and answer:
lang_word = random.choice(self.wrong_list)
return (lang_word, self.dict[lang_word])
return random.choice(list(self.dict.items()))
if self.use_wrong_list and answer:
lang_word = random.choice(self.wrong_list)
for key, value in self.dict.items():
if value == lang_word:
return (lang_word, key)
return reversed(random.choice(list(self.dict.items()))[:])
if __name__ == "__main__":
create_local_file(100)