-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-youdao-dict.py
558 lines (480 loc) · 15.9 KB
/
py-youdao-dict.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#coding=utf-8
import os
import sys
from urllib import request
from urllib import parse
from urllib import error
import simplejson as json
import re
import time
import collections
import string
from tkinter import *
#GUI程序
class Application(object):
def __init__(self):
self.InitApp()
self.CreateWidgets()
self.top.mainloop()
def InitApp(self):
#有道词典的OPEN API
self.API_KEY = '1437381740'
self.KEYFROM = 'py-youdao-dict'
self.spellCorrector = SpellCorrector()
self.predictor = Predictor()
self.fileSets = [[] for i in range(26)]
self.listboxIdx = -1
self.currentText = ''
self.correctUserInput = ''
self.EnglishToChinese = False
self.ChineseToEnglish = False
self.LoadSavedFile()
def CreateWidgets(self):
self.top = Tk()
self.top.wm_attributes('-topmost', 1)
self.top.title('py-youdao-dict')
self.inputFrame = Frame(self.top)
self.inputFrame.pack()
self.userText = StringVar()
self.userTextEntry = Entry(self.inputFrame, vcmd=self.userTextChanged, textvariable=self.userText)
self.userTextEntry.grid(row=0, column=0)
self.userTextEntry.bind('<Key>', self.userTextChanged)
self.userTextEntry.focus_set()
self.searchButton = Button(self.inputFrame, text=u'查询', command=self.Search)
self.searchButton.grid(row=0, column=1)
self.translateFrame = Frame(self.top)
self.translateText = Text(self.translateFrame, width=60, height=20)
# self.translateText.grid(row=1, column=0, columnspan=2)
self.translateText.grid(row=0, column=0)
self.predictFrame = Frame(self.top)
self.userTextPredictListbox = Listbox(self.predictFrame)
self.userTextPredictListbox.grid(row=0, column=0)
self.correctFrame = Frame(self.top)
self.correctUserInput = StringVar()
self.correctEntry = Entry(self.correctFrame, vcmd=self.correctGetInput, textvariable=self.correctUserInput)
self.correctEntry.grid(row=0, column=0)
self.correctEntry.bind('<Key>', self.correctGetInput)
def correctGetInput(self, event):
# txt = self.correctUserInput.get()
if event.keysym in ['y', 'Y']:
self.correctInput = True
elif event.keysym in ['n', 'N']:
self.correctInput = False
# self.correctEntry.delete(0, END)
# self.correctEntry.insert(0, txt)
def userTextChanged(self, event):
# print(self.userText.get())
# print('1',event.keycode)
# print('2',event.char)
# print('3',event.keysym)
# print(self.userText.get())
predictFlag = False
idx = self.userTextEntry.index(INSERT)-1
listboxLines = self.userTextPredictListbox.size()
#print(event.keysym, event.char, event.keycode)
if event.keysym == 'BackSpace':
self.listboxIdx = -1
self.translateFrame.forget()
self.translateText.forget()
self.currentText = self.userText.get()[:idx] + self.userText.get()[idx+1:]
#print('---:'+self.currentText)
if self.currentText:
predictFlag = True
else:
self.EnglishToChinese = False
self.ChineseToEnglish = False
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
elif event.keysym == 'Delete':
self.listboxIdx = -1
self.translateFrame.forget()
self.translateText.forget()
self.currentText = self.userText.get()[:idx+1] + self.userText.get()[idx+2:]
if self.currentText:
predictFlag = True
else:
self.EnglishToChinese = False
self.ChineseToEnglish = False
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
elif event.keysym == 'Return':
self.Search()
elif event.keysym == 'Up':
if self.listboxIdx > 0:
self.listboxIdx -= 1
self.userTextPredictListbox.activate(self.listboxIdx)
t = self.userTextPredictListbox.get(self.listboxIdx)
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, t)
elif self.listboxIdx == 0:
self.listboxIdx = listboxLines
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, self.currentText)
elif self.listboxIdx == -1:
self.listboxIdx = listboxLines-1
self.userTextPredictListbox.activate(self.listboxIdx)
t = self.userTextPredictListbox.get(self.listboxIdx)
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, t)
elif event.keysym == 'Down':
if self.listboxIdx < listboxLines-1:
self.listboxIdx += 1
self.userTextPredictListbox.activate(self.listboxIdx)
t = self.userTextPredictListbox.get(self.listboxIdx)
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, t)
elif self.listboxIdx == listboxLines-1:
self.listboxIdx += 1
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, self.currentText)
elif self.listboxIdx == listboxLines:
self.listboxIdx = 0
self.userTextPredictListbox.activate(self.listboxIdx)
t = self.userTextPredictListbox.get(self.listboxIdx)
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, t)
elif event.keysym in string.ascii_lowercase:
self.EnglishToChinese = True
self.currentText = self.userText.get() + event.char
predictFlag = True
elif event.keycode == 0:
self.ChineseToEnglish = True
self.currentText = self.userText.get() + event.char
predictFlag = True
if predictFlag:
predictText = self.predictor.Predict(self.currentText)
#print(self.currentText)
#print(predictText)
if predictText:
self.predictFrame.pack()
self.userTextPredictListbox.pack()
self.userTextPredictListbox['height'] = len(predictText)
self.userTextPredictListbox.delete(0, listboxLines)
line = 0
for predict in predictText:
self.userTextPredictListbox.insert(line, predict)
line += 1
else:
listboxLines = self.userTextPredictListbox.size()
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
def Search(self):
if not self.currentText:
return
if self.EnglishToChinese and self.ChineseToEnglish:
self.listboxIdx = -1
listboxLines = self.userTextPredictListbox.size()
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
warningInfo = u'不能中英文混合查询'
self.translateText['height'] = 1
self.translateText.delete(0.0, END)
self.translateText.insert(0.0, warningInfo)
self.translateFrame.pack()
self.translateText.pack()
elif self.EnglishToChinese:
self.EnglishToChineseTranslate()
elif self.ChineseToEnglish:
self.ChineseToEnglishTranslate()
def ChineseToEnglishTranslate(self):
listboxLines = self.userTextPredictListbox.size()
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
txt = self.userTextEntry.get()
#print(txt)
translate = self.SjsonChinese(self.GetTranslate(txt))
num = 0
head = 0
for i in translate:
head += 1
if head >= self.translateText['width']:
num += 1
head = 0
if i == '\n':
num += 1
head = 0
#print(num)
#print(self.translateText['width'])
#print(len(translate))
self.translateText['height'] = num
self.translateText.delete(0.0, END)
self.translateText.insert(0.0, translate)
self.translateFrame.pack()
self.translateText.pack()
def EnglishToChineseTranslate(self):
listboxLines = self.userTextPredictListbox.size()
self.userTextPredictListbox.delete(0, listboxLines)
self.userTextPredictListbox.forget()
self.predictFrame.forget()
txt = self.userTextEntry.get().lower()
correctTxt = self.spellCorrector.correct(txt)
if txt != correctTxt:
self.correctFrame.pack()
self.correctEntry.delete(0, END)
self.correctEntry.insert(0, u'您是否想要输入'+correctTxt+'?(y/n)')
self.correctEntry.pack()
self.correctEntry.focus_set()
while True:
self.correctEntry.wait_variable(self.correctUserInput)
if self.correctInput:
txt = correctTxt
self.userTextEntry.delete(0, END)
self.userTextEntry.insert(0, txt)
break
elif not self.correctInput:
break
self.correctEntry.forget()
self.correctFrame.forget()
self.userTextEntry.focus_set()
translate = self.Sjson(self.GetTranslate(txt))
num = 0
head = 0
for i in translate:
head += 1
if head >= self.translateText['width']:
num += 1
head = 0
if i == '\n':
num += 1
head = 0
#print(num)
#print(self.translateText['width'])
#print(len(translate))
self.translateText['height'] = num
self.translateText.delete(0.0, END)
self.translateText.insert(0.0, translate)
self.translateFrame.pack()
self.translateText.pack()
def LoadSavedFile(self):
try:
filepath = r'py-youdao-dict.json'
fp = open(filepath, 'r+')
except FileNotFoundError:
print(u'%s文件不存在' %filepath)
return
fp.seek(os.SEEK_SET)
file = fp.readlines()
file.sort(key=lambda x:json.loads(x).get('query', ''))
#for line in file:
# print(line)
fp.close()
for line in file:
currentQuery = json.loads(line).get('query', '')
setIdx = ord(currentQuery.lower()[0]) - ord('a')
self.fileSets[setIdx].append(json.loads(line))
'''
for i in self.fileSets:
if i != []:
print('*' * 40)
for j in i:
print(j)
'''
def GetTranslateFromFile(self, query):
query = query.lower()
findWord = False
setIdx = ord(query[0]) - ord('a')
if [] != self.fileSets[setIdx]:
for wordSaved in self.fileSets[setIdx]:
if query == wordSaved.get('query', ''):
findWord = True
break
if findWord:
return wordSaved
else:
return None
def SaveWordToFile(self, query, jsonData):
query = query.lower()
choices = 'y'
if choices in ['y', 'Y']:
alreadySaved = False
setIdx = ord(query[0]) - ord('a')
#print(self.fileSets[setIdx])
if [] == self.fileSets[setIdx]:
self.fileSets[setIdx].append(jsonData)
else:
for wordSaved in self.fileSets[setIdx]:
if query == wordSaved.get('query', ''):
alreadySaved = True
break
if alreadySaved:
print(u'单词已经在单词本\r\n')
else:
self.fileSets[setIdx].append(jsonData)
filepath = r'py-youdao-dict.json'
fp = open(filepath, 'a+')
fp.write(json.dumps(jsonData)+'\n')
#fp.write('\r\n')
fp.close()
print(u'写入单词本成功\r\n')
def GetTranslateChinese(self, query):
url = 'http://fanyi.youdao.com/openapi.do'
data = {
'keyfrom': self.KEYFROM,
'key': self.API_KEY,
'type': 'data',
'doctype': 'json',
'version': 1.1,
'q': query
}
data = parse.urlencode(data)
url = url+'?'+data
req = request.Request(url)
offline = False
try:
response = request.urlopen(req)
except error.URLError:
offline = True
result = GetTranslateFromFile(query)
if not offline:
result = json.loads(response.read())
return result
def GetTranslate(self, query):
query = query.lower()
url = 'http://fanyi.youdao.com/openapi.do'
data = {
'keyfrom': self.KEYFROM,
'key': self.API_KEY,
'type': 'data',
'doctype': 'json',
'version': 1.1,
'q': query
}
data = parse.urlencode(data)
url = url+'?'+data
req = request.Request(url)
offline = False
try:
response = request.urlopen(req)
except error.URLError:
offline = True
result = GetTranslateFromFile(query)
if not offline:
result = json.loads(response.read())
return result
def SjsonChinese(self, jsonData):
if None == jsonData:
return u'单词本中没有收录该单词,请联网查询!\r\n'
query = jsonData.get('query', '')
translation = jsonData.get('translation', '')
basic = jsonData.get('basic', '')
if basic == '':
return u'查询单词出现错误'
sequence = jsonData.get('web', [])
phonetic, explains_txt, seq_txt, log_word_explains = \
'', '', '', ''
if basic:
phonetic = basic.get('phonetic', '')
explains = basic.get('explains', '')
for obj in explains:
explains_txt += obj+'\n'
log_word_explains += obj+','
if sequence:
for obj in sequence:
seq_txt += obj['key']+'\n'
values = ''
for i in obj['value']:
values += i+','
seq_txt += values+'\n'
result = ''
result += u'查询对象: %s [%s]\n' %(query, phonetic)
result += explains_txt
result += '-'*20+'\n'+seq_txt
#self.SaveWordToFile(query, jsonData)
return result
def Sjson(self, jsonData):
if None == jsonData:
return u'单词本中没有收录该单词,请联网查询!\r\n'
query = jsonData.get('query', '')
translation = jsonData.get('translation', '')
basic = jsonData.get('basic', '')
if basic == '':
return u'查询单词出现错误'
sequence = jsonData.get('web', [])
phonetic, explains_txt, seq_txt, log_word_explains = \
'', '', '', ''
if basic:
phonetic = basic.get('phonetic', '')
explains = basic.get('explains', '')
for obj in explains:
explains_txt += obj+'\n'
log_word_explains += obj+','
if sequence:
for obj in sequence:
seq_txt += obj['key']+'\n'
values = ''
for i in obj['value']:
values += i+','
seq_txt += values+'\n'
result = ''
result += u'查询对象: %s [%s]\n' %(query, phonetic)
result += explains_txt
result += '-'*20+'\n'+seq_txt
self.SaveWordToFile(query, jsonData)
return result
def WordbookSample(self):
with open('wordbook-sample.txt', 'r') as fp:
print(u'单词本开始收录')
file = fp.readlines()
txtCounter = 0
for line in file:
wordbookSample = list(filter(lambda x:x!= '', re.split(r'[^a-zA-Z]+', line)))
for txt in wordbookSample:
txtCounter += 1
if 0 == (txtCounter & 15):
time.sleep(1)
#使用API查询,请求频率限制为每小时1000次,超过限制会被封禁
if txtCounter >= 900:
return
Sjson(GetTranslate(txt.lower()))
print(u'单词本收录完毕')
#拼写检查
class SpellCorrector(object):
def __init__(self):
self.NWORDS = self.train(self.words(open('big.txt', 'r').read()))
self.alphabet = string.ascii_lowercase
def words(self, text):
return re.findall('[a-z]+', text.lower())
def train(self, features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
def edits1(self, word):
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in splits if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
replaces = [a + c + b[1:] for a, b in splits for c in self.alphabet if b]
inserts = [a + c + b for a, b in splits for c in self.alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(self, word):
return set(e2 for e1 in self.edits1(word) for e2 in self.edits1(e1) if e2 in self.NWORDS)
def known(self, words):
return set(w for w in words if w in self.NWORDS)
def correct(self, word):
candidates = self.known([word]) or self.known(self.edits1(word)) or self.known_edits2(word) or [word]
return max(candidates, key=self.NWORDS.get)
#用户输入预测
class Predictor(object):
def __init__(self):
with open('big.txt', 'r') as f:
self.model = collections.defaultdict(lambda: 1)
self.text = re.findall('[a-z]+', f.read().lower())
for word in self.text:
self.model[word] += 1
def Predict(self, txt):
predictText = re.findall(txt+'[a-z]*', str(self.text))
predictText = list(set(predictText))
# predictText.sort()
predictText.sort(key=lambda x:self.model[x], reverse=True)
#for x in predictText:
# print(self.model[x])
return predictText[0:5] if len(predictText) > 5 else predictText
def gui_main():
app = Application()
if __name__ == '__main__':
gui_main()