-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.py
188 lines (147 loc) · 4.3 KB
/
main.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
from nltk.stem import PorterStemmer
porter = PorterStemmer()
def load_csv(top=None):
words=set()
with open('source/results-20191210-170511.csv') as f:
skip_head=True
c=0
for l in f:
if skip_head:
skip_head=False
continue
c=c+1
if top and c>top:
break
(word,count)=l.split(',')
words.add(word)
return words
def load_word(fiilename):
words=set()
with open(fiilename) as f:
for l in f:
words.add(l.strip())
return words
def load_include():
return load_word('include/words_alpha.txt')
def load_exclude():
words=set()
with open('exclude/voa_words.txt') as f:
for l in f:
if len(l.strip())<2:
continue
w=l.split(' ')[0]
st = porter.stem(w)
words.add(w)
words.add(st)
common = load_word('exclude/common3000.txt')
return words|common
def load_dict():
import csv
dict={}
with open('dictionary/ecdict.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
w=row['word']
dict[w]=row
return dict
words=load_csv(16000)
include=load_include()
words=words&include
exclude=load_exclude()
# common=voa|common
# r=r.union(top2000)
d={}
for w in sorted(words):
st=porter.stem(w)
if len(st) <=3 or st in exclude or w in exclude:
continue
if st in d:
d[st].append(w)
else:
d[st]=[w]
def write_head(section,f):
f.write('\n\n## '+section+'\n\n')
f.write(f'<a name="{section}"></a>[TO Head](#A)\n\n')
f.write('Word|Phonetic|Inflection|Definition|Translation|Example\n')
f.write('----|--------|----------|----------|-----------|-------\n')
def write_row(f,word,inflection=[],phonetic=' ',definition=' ',translation=' ',example=' '):
row=[]
definition=definition.replace('\\n','<br>')
translation = translation.replace('\\n', '<br>')
for i in [word,phonetic,'<br>'.join(inflection),definition,translation,example]:
if len(i)==0:
row.append(' ')
else:
row.append(i)
f.write('|'.join(row)+'\n')
last_alpha=None
dictionary=load_dict()
result_dict=[]
for i in d.keys():
w=i
if w in dictionary:
o = dictionary[w]
result_dict.append(
{
'word':w,
'phonetic':o['phonetic'],
'inflection':d[i][1:],
'definition':o['definition'],
'translation':o['translation']
}
)
else:
result_dict.append(
{
'word':w,
'phonetic':' ',
'inflection':d[i][1:],
'definition':' ',
'translation':' '
}
)
with open('readme.md','w') as f,open('head.md') as h:
for l in h:
f.write(l)
total=len( d.keys())
f.write(f'### Total words:{total}\n\n')
for a in range(ord('A'), ord('Z')+1):
c=chr(a)
f.write(f'[{c}](#{c}) ')
f.write('\n\n');
for item in result_dict:
word=item['word']
if word[0]!=last_alpha:
write_head(word[0].upper(),f)
last_alpha=word[0]
write_row(f, word, phonetic=item['phonetic'], inflection=item['inflection'], definition=item['definition'],
translation=item['translation'])
#
print('total words:',len(d.keys()))
import genanki
import copy
my_model = genanki.Model(
1607392319,
'Simple Model',
fields=[
{'name': 'word'},
{'name': 'phonetic'},
{'name': 'definition'},
{'name': 'translation'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '<center><h1>{{word}}</h1><br>{{phonetic}}</center>',
'afmt': '<center><h1>{{word}}</h1><br>{{phonetic}}</center><hr id="answer">{{definition}}<br>{{translation}}',
},
])
my_deck = genanki.Deck(
10591234111,
'Simple-IT-English(en-cn)')
for item in result_dict:
my_note = genanki.Note(
model=my_model,
fields=[item['word'],item['phonetic'],item['definition'],item['translation']])
my_deck.add_note(copy.deepcopy(my_note))
genanki.Package(my_deck).write_to_file('Simple-IT-English(en-cn).apkg')