/
rem.py
155 lines (131 loc) · 3.56 KB
/
rem.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
import os,sys
import json
import random
import copy
mydict={}
def explain(word):
global mydict
if word in mydict:
return mydict[word]
def worddst(a, b):
__author__ = 'xanxus'
s1, s2 = a, b
m, n = len(s1), len(s2)
colsize, matrix = m + 1, []
for i in range((m + 1) * (n + 1)):
matrix.append(0)
for i in range(colsize):
matrix[i] = i
for i in range(n + 1):
matrix[i * colsize] = i
for i in range(n + 1)[1:n + 1]:
for j in range(m + 1)[1:m + 1]:
cost = 0
if s1[j - 1] == s2[i - 1]:
cost = 0
else:
cost = 1
minValue = matrix[(i - 1) * colsize + j] + 1
if minValue > matrix[i * colsize + j - 1] + 1:
minValue = matrix[i * colsize + j - 1] + 1
if minValue > matrix[(i - 1) * colsize + j - 1] + cost:
minValue = matrix[(i - 1) * colsize + j - 1] + cost
matrix[i * colsize + j] = minValue
return matrix[n * colsize + m]
def printnearest(word,n=6):
words=[]
for k,v in mydict.items():
if worddst(k,word)<=3:
words.append(k)
if len(words)>=n: break
if len(words)<=2:
for k,v in mydict.items():
words.append(k)
random.shuffle(words)
words=words[:6]
if word not in words: words.append(word)
random.shuffle(words)
for w in words:
print '>>',w
def rem(d,wl):
wordlist=copy.deepcopy(wl)
Recite_Num = 3
print
print '-'*35
print 'WORD LIST',d
print '-'*35
for word in wordlist:
while True:
print '-'*20
print '::',word,'::'
print explain(word)
mark=False
for i in range(Recite_Num):
inp=raw_input(str(i)+'\t')
if inp!=word:
mark=True
break
if not mark: break
random.shuffle(wordlist)
for word in wordlist:
print '-'*20
print explain(word)
printnearest(word)
while True:
w=raw_input('word: ')
if w!=word:
print 'Wrong! ::',word,'::'
# adddif(w)
else:
print 'Correct!'
break
def main():
global mydict
f=open('gre3000.dict')
ls=f.readlines()
f.close()
for l in ls:
sp=l.find('\t')
key=l[:sp]
val=l[sp+1:]
mydict[key]=val
tws=[]
print 'Type in today\'s words'
while True:
try:
word=raw_input('word: ')
ex=explain(word)
if ex!=None:
tws.append(word)
print ex
except EOFError:
break
f=open('wordhistory.json')
ds=json.loads(f.read())
f.close()
ds.append(tws)
f=open('wordhistory.json','w')
f.write(json.dumps(ds))
f.close()
x=[0,1,2,4,7,15]
x.reverse()
lends=len(ds)
rem(lends-1,ds[lends-1])
for xi in x:
if lends-1-xi>=0: rem(lends-1-xi,ds[lends-1-xi])
print '-'*35
print 'CONGRATULATIONS! '
print '-'*35
import readline
import rlcompleter
import atexit
def reg():
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file,histfile)
if __name__=='__main__':
reg()
main()