-
Notifications
You must be signed in to change notification settings - Fork 6
/
ydict
executable file
·402 lines (354 loc) · 10.1 KB
/
ydict
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
#!/usr/bin/env python
# coding=UTF-8
# Chen Wen <[email protected]>
# Web Site https://github.com/chenpc/ydict
# Blog : http://chenpc.csie.in
import getopt
import sys
import string
import httplib, urllib, string, sys
from optparse import OptionParser
import locale
from codecs import EncodedFile
import shelve, os
import random
import ConfigParser
from multiprocessing import Process, Queue, Pool
import xml.etree.ElementTree as ET
version="ydict 1.3.4"
playback = ""
prefetch = ""
red="\33[31;1m"
lindigo="\33[36;1m"
indigo="\33[36m"
green="\33[32m"
yellow="\33[33;1m"
blue="\33[34;1m"
org="\33[0m"
light="\33[0;1m"
db = shelve.open(os.getenv("HOME")+"/.ydict.db","c")
try:
config = ConfigParser.ConfigParser()
config.readfp(open(os.getenv("HOME")+"/.ydictrc"))
playback = config.get('ydict', 'playback')
prefetch = config.get('ydict', 'prefetch')
except :
pass
if prefetch == "":
prefetch = "5"
def cleanup():
db.sync()
exit()
def importfile(file):
fp = open(file)
for line in fp:
newword=line.split(" ")[0]
newword=newword.split("\n")[0]
if db.has_key(newword) == 0:
db[newword]=0
print "File imported!"
def result(count, total):
if total == 0:
print ""
exit()
print "\nScore: ",int(count),"/",int(total),"(",count/total,")"
exit()
def seckey(x):
return x[1]
# XXX don't use system
def speak(result):
if playback == "" or result == None:
return
k = result.key.text
os.system(playback+" '"+result.ogg.attrib['data-src']+"' >/dev/null 2>&1 &")
def answers(iq, oq):
while(1):
key = iq.get()
result = dict(key, False)
if result == None:
result = dict(key, True)
oq.put(result)
def browse():
wordlist = db.items()
size=len(wordlist)
totalcount = 0.0
right = 0.0
lookup = Queue(maxsize = string.atoi(prefetch))
answer = Queue(maxsize = string.atoi(prefetch))
lookuper = Process( target=answers, args=(lookup, answer) )
lookuper.daemon = True
lookuper.start()
if size <= 1:
print "There must be at least two words needed in the list."
exit()
i = 0
while(1) :
while(not lookup.full()):
k=wordlist[i][0]
i = i + 1
if i >= size:
i = 0
k=k.lower()
lookup.put(k)
result = answer.get()
k = result.key.text
if not db.has_key(k):
continue
print result.show()
speak(result)
try:
word = raw_input("(d) Delete, (enter) Continue: ")
if word == "d":
del db[k]
wordlist=db.items()
size=len(wordlist)
if size <= 1:
print "There must be at least two words needed in the list."
exit()
except KeyboardInterrupt:
result(right,totalcount)
def wordlearn():
wordlist = db.items()
wordlist.sort(key=seckey)
size=len(wordlist)
totalcount = 0.0
right = 0.0
lookup = Queue(maxsize = 5)
answer = Queue(maxsize = 5)
lookuper = Process( target=answers, args=(lookup, answer) )
lookuper.daemon = True
lookuper.start()
if size <= 1:
print "There must be at least two words needed in the list."
exit()
while(1) :
while(not lookup.full()):
k=wordlist[int(random.triangular(0, size-1, 0))][0]
k=k.lower()
lookup.put(k)
result = answer.get()
if result == None:
continue
k = result.key.text
if not db.has_key(k):
continue
print result.show().replace(k, "####").replace(k.upper(), "####").replace(k[0].swapcase()+k[1:].lower(),"####")
speak(result)
word = raw_input("Input :")
if word == k.lower():
print "Bingo!"
right+=1
db[k]+=1
if db[k] >= 100:
db[k]=100
else:
db[k]-=3
if db[k] < 0:
db[k]=0
print "WRONG! Correct answer is : ",k
try:
word = raw_input("(d) Delete, (enter) Continue: ")
if word == "d":
del db[k]
wordlist=db.items()
wordlist.sort(key=seckey)
size=len(wordlist)
if size <= 1:
print "There must be at least two words needed in the list."
exit()
except KeyboardInterrupt:
result(right,totalcount)
totalcount+=1
if totalcount % (int(size/4)+1) == 0:
wordlist=db.items()
wordlist.sort(key=seckey)
def wordlist():
wordlist = db.items()
wordlist.sort(key=seckey)
for k,v in wordlist:
print k,v
def htmlspcahrs(content):
content=content.replace("&","")
content=content.replace("#39;","\'")
content=content.replace("quot;","\"")
return content
def http_postconn(word):
yahoourl="tw.dictionary.yahoo.com"
params = urllib.urlencode({'p': word})
return urllib.urlopen("http://%s/dctionary?%s" % (yahoourl, params))
class explan_node:
pass
class explanation_node:
pass
# Color print
def cprint(data, color, newline=1, indent=0, num=0):
if newline == 1:
nl="\n"
else:
nl=""
if num !=0:
nu = str(num)+". "
else:
nu = ""
idn = " " * indent
if data != None:
if isinstance(data, str) or isinstance(data, unicode):
return (idn + nu + color + data + org + nl)
elif data.text != None:
return (idn + nu + color + data.text + org + nl)
return None
class explan_word:
def show(self):
ret=""
res=[]
res.append(cprint(self.key, yellow))
if self.kk != None:
res.append(cprint("KK", org, 0))
res.append(cprint(self.kk, light, 0))
if self.dj != None:
res.append(cprint(" DJ", org, 0))
res.append(cprint(self.dj, light))
for explan_entry in self.explan_list:
res.append(cprint(explan_entry.pos_abbr, red, 0))
res.append(cprint(explan_entry.pos_desc, red, 0))
res.append(cprint("", org, 1))
i = 1
for explanation_ol_entry in explan_entry.explanation:
res.append(cprint("", org, 0, 2, i))
for explanation in explanation_ol_entry.explanation.iter():
res.append(cprint(explanation, org, 0))
res.append(cprint(explanation.tail, org, 0))
res.append(cprint("", org))
if explanation_ol_entry.example_sentence != None:
res.append(cprint(" ", indigo, 0, 0))
for text in explanation_ol_entry.example_sentence.iter():
if text.tag == 'b':
res.append(cprint(text.text, lindigo, 0))
res.append(cprint(text.tail, indigo, 0))
else:
res.append(cprint(text, indigo, 0, 0))
res.append(cprint("", org, 1))
res.append(cprint(explanation_ol_entry.samp, green, 1, 4))
i += 1
for line in res:
if line != None:
ret = ret + line # XXX bad code
return ret
def dict(word, more_exp):
output = ""
word = word.strip()
if len(word) <= 0:
return None
r1 = http_postconn(word)
data1 = r1.read()
try :
summary = data1[data1.index('<div id="left">'):]
summary = summary[:summary.index('<div id="right">')]
except ValueError:
return None
summary = htmlspcahrs(summary)
root = ET.XML(summary)
exp_word = explan_word()
exp_word.key = root.find(".//*/span[@class='yschttl']")
exp_word.kk = root.find(".//*/span[@class='proun_value'][2]")
exp_word.dj = root.find(".//*/span[@class='proun_value'][4]")
exp_word.mp3 = root.find(".//*/span[@class='source'][@data-type='audio/mpeg']")
exp_word.ogg = root.find(".//*/span[@class='source'][@data-type='audio/ogg']")
if more_exp:
search_exp = ".//*/li[@class='explanation_pos_wrapper']"
else:
search_exp = ".//*/li[@class='result_cluster_first res']/*/li[@class='explanation_pos_wrapper']"
explan_list = list()
for explan in root.findall(search_exp):
explanation_ol_list = list()
explan_entry = explan_node()
explan_entry.pos_abbr = explan.find("./h5/span[@class='pos_abbr']")
explan_entry.pos_desc = explan.find("./h5/span[@class='pos_desc']")
for explanation_ol in explan.findall("./ol[@class='explanation_ol']/li"):
explanation_ol_entry = explanation_node()
explanation_ol_entry.explanation = explanation_ol.find("./p[@class='explanation']")
explanation_ol_entry.example_sentence = explanation_ol.find("./p[@class='sample']/samp[@class='example_sentence']")
explanation_ol_entry.samp = explanation_ol.find("./p[@class='sample']/samp[2]")
explanation_ol_list.append(explanation_ol_entry)
explan_entry.explanation = explanation_ol_list
explan_list.append(explan_entry)
exp_word.explan_list = explan_list
if not isinstance(exp_word.key.text, unicode):
db[exp_word.key.text] = 1
return exp_word
if __name__ == '__main__':
parser = OptionParser(usage = "Usage: ydict [options] word1 word2 ......")
parser.add_option("-e", "--exp", dest="more_exp", help="Show more explanation.",default=False,action="store_true")
parser.add_option("-c", "--nocolor", dest="nocolor", help="force no color code",default=False, action="store_true")
parser.add_option("-v", "--version", dest="version", help="show version.",default=False,action="store_true")
parser.add_option("-l", "--learn", dest="learnmode", help="start learning mode.",default=False,action="store_true")
parser.add_option("-b", "--browse", dest="browsemode", help="start browse mode.",default=False,action="store_true")
parser.add_option("-a", "--list", dest="listall", help="list all word in list.",default=False,action="store_true")
parser.add_option("-i", "--import", dest="importfile", type="string", help="import a word list",default=False,action="store")
(options, args) = parser.parse_args()
(lang , enc)=locale.getdefaultlocale()
if enc != "UTF-8":
print "ydict can only works with encoding=UTF-8, and you encoding is : ", lang, enc
cleanup()
if options.nocolor:
red=""
lindigo=""
indigo=""
green=""
yellow=""
blue=""
org=""
light=""
if options.importfile:
importfile(options.importfile)
cleanup()
if options.version == True:
print version
cleanup()
if options.browsemode == True:
try:
browse()
except KeyboardInterrupt:
print ""
cleanup()
except EOFError:
print ""
cleanup()
if len(args) >= 1:
for w in args:
result=dict(w, options.more_exp)
if result == None:
print cprint("[" + w + "] Not found", yellow, 0)
continue
speak(result)
print result.show()
cleanup()
if options.learnmode:
try:
wordlearn()
except KeyboardInterrupt:
print ""
cleanup()
except EOFError:
print ""
cleanup()
cleanup()
elif options.listall:
wordlist()
cleanup()
while(1):
try:
word=raw_input("<PyDict> ")
except KeyboardInterrupt:
print ""
cleanup()
except EOFError:
print ""
cleanup()
result = dict(word, options.more_exp)
if result == None:
print cprint("[" + word + "] Not found", yellow, 0)
continue
else:
speak(result)
print result.show()