-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (75 loc) · 2.67 KB
/
main.py
File metadata and controls
96 lines (75 loc) · 2.67 KB
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
import json
import random
from gtts import gTTS
from playsound import playsound
import pyttsx3
pronounciationEnabled = False
glossary = 'voca.json'
num_iter = 32
def both_know(select_word, weight):
if select_word in weight.keys():
weight[select_word] += 1
else:
weight[select_word] = 1
return weight
def misunderstand(select_word, weight):
if select_word in weight.keys() and weight[select_word] < -6:
return weight
if select_word in weight.keys():
weight[select_word] -= 2
else:
weight[select_word] = -2
return weight
def dont_know(select_word, weight):
if select_word in weight.keys() and weight[select_word] < -6:
return weight
if select_word in weight.keys():
weight[select_word] -= 3
else:
weight[select_word] = -3
return weight
def pronounceOnline(word):
tts = gTTS(word, lang='en')
tts.save('temp.mp3')
playsound('temp.mp3')
def pronounce(engine, word):
engine.setProperty("rate", 300)
engine.say(word)
engine.runAndWait()
def main():
word_dict = json.load(open(glossary, 'r'))
try:
word_dict_weight = json.load(open('weight.json', 'r'))
except:
word_dict_weight = {}
engine = pyttsx3.init()
for i in range(num_iter):
print('---------------------第', i+1, '个单词---------------------')
select_word_cn = random.choice(list(word_dict.keys()))
reco, know = None, None
select_word = random.choice(word_dict[select_word_cn])
while select_word in word_dict_weight.keys() and word_dict_weight[select_word] > 2:
select_word_cn = random.choice(list(word_dict.keys()))
select_word = random.choice(word_dict[select_word_cn])
if (pronounciationEnabled):
pronounce(engine, select_word)
# input user's answer
while reco != "j" and reco != "k":
reco = input(select_word + ' (j/k)\n')
if reco == "k":
know = "k"
print(select_word_cn, '\n')
while know != "j" and know != "k":
know = input('是否和释义一致?(j/k)\n')
# determine weight
if reco == 'j' and know == 'j':
word_dict_weight = both_know(select_word, word_dict_weight)
elif reco == 'j' and know == 'k':
word_dict_weight = misunderstand(select_word, word_dict_weight)
elif reco == 'k' and know == 'k':
word_dict_weight = dont_know(select_word, word_dict_weight)
print('weight: ', word_dict_weight[select_word], '\n')
json.dump(word_dict_weight, open('weight.json', 'w+'))
print('保存成功!')
if __name__ == '__main__':
main()