/
mkdict.py
executable file
·310 lines (279 loc) · 7.52 KB
/
mkdict.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
#!/usr/bin/env python
##
## convdict.py - WordNet Dictionary converter
##
import sys
import re
import os.path
import math
try:
import cdb
except ImportError:
import pycdb as cdb
C = 1.0/math.log(2)
def convfreq(n):
return C*math.log(n)+0.5
pat_ys = re.compile(r'.*[^auieo]y$')
def get_s(s):
if s.endswith('s'):
return s[:-1]+'ses'
elif s.endswith('x'):
return s[:-1]+'xes'
elif s.endswith('z'):
return s[:-1]+'zes'
elif pat_ys.match(s):
return s[:-1]+'ies'
elif s.endswith('ch'):
return s[:-2]+'ches'
elif s.endswith('sh'):
return s[:-2]+'shes'
elif s.endswith('man'):
return s[:-3]+'men'
else:
return s+'s'
def get_plural(s, exc):
x = exc.get(s)
if x: return x
return get_s(s)
def get_mw(w, f, exc):
return f(w[0], exc)+'_'+'_'.join(w[1:])
def get_pres3rd(s, exc):
x = exc.get(s)
if x: return x
w = s.split('_')
if 1 < len(w):
return get_mw(w, get_pres3rd, exc)
elif s.endswith('o'):
return s+'es'
else:
return get_s(s)
def get_past(s, exc):
x = exc.get(s)
if x: return x
w = s.split('_')
if 1 < len(w):
return get_mw(w, get_past, exc)
if s.endswith('e'):
return s+'d'
else:
return s+'ed'
def get_pastpart(s, exc):
x = exc.get(s)
if x: return x
w = s.split('_')
if 1 < len(w):
return get_mw(w, get_pastpart, exc)
return get_past(s, exc)
def get_gerund(s, exc):
x = exc.get(s)
if x: return x
if s == 'be':
return s+'ing'
elif s.endswith('e') and not s.endswith('ee'):
return s[:-1]+'ing'
else:
return s+'ing'
def get_comparative(s, exc):
x = exc.get(s)
if x: return x
if s.endswith('e'):
return s+'r'
elif s.endswith('y'):
return s+'ier'
else:
return s+'er'
def get_superlative(s, exc):
x = exc.get(s)
if x: return x
if s.endswith('e'):
return s+'st'
elif s.endswith('y'):
return s+'iest'
else:
return s+'est'
## DictionaryConverter
##
class DictionaryConverter(object):
def __init__(self, basedir):
self.basedir = basedir
self.read_noun_exc()
self.read_adj_exc()
self.read_adv_exc()
self.read_verb_exc()
self.read_cntlist()
self.skip = set()
self._words = {}
return
def _open_file(self, name):
print >>sys.stderr, 'Reading: %r' % name
path = os.path.join(self.basedir, name)
return file(path, 'r')
def _read_exc(self, name):
fp = self._open_file(name+'.exc')
for line in fp:
f = line.strip().split(' ')
yield (f[1], f[0])
fp.close()
return
def read_noun_exc(self):
self.nns_exc = {}
for (s0, s1) in self._read_exc('noun'):
self.nns_exc[s0] = s1
return
def read_adj_exc(self):
self.jjr_exc = {}
self.jjs_exc = {}
for (s0, s1) in self._read_exc('adj'):
if s1.endswith('r'):
self.jjr_exc[s0] = s1
elif s1.endswith('t'):
self.jjs_exc[s0] = s1
return
def read_adv_exc(self):
self.rbr_exc = {}
self.rbs_exc = {}
for (s0, s1) in self._read_exc('adv'):
if s1.endswith('r'):
self.rbr_exc[s0] = s1
elif s1.endswith('t'):
self.rbs_exc[s0] = s1
return
pat_past = re.compile(r'[^_]+([deklmtwy]|ang?|on)_')
pat_pastpart = re.compile(r'[^_]+(ne|ung|en|ain|rn|un|wn)_')
pat_gerund = re.compile(r'[^_]+ing_')
def read_verb_exc(self):
self.vbz_exc = {}
self.vbd_exc = {}
self.vbn_exc = {}
self.vbg_exc = {}
for (s0, s1) in self._read_exc('verb'):
if s1.endswith('s'):
self.vbz_exc[s0] = s1
elif self.pat_past.match(s1+'_') and not s1.endswith('ne'):
self.vbd_exc[s0] = s1
elif self.pat_pastpart.match(s1+'_'):
self.vbn_exc[s0] = s1
elif self.pat_gerund.match(s1+'_'):
self.vbg_exc[s0] = s1
return
def read_cntlist(self):
freq = {}
fp = self._open_file('cntlist')
for line in fp:
f = line.strip().split(' ')
n = int(f[0])
f = f[1].split(':')
(w,_,_) = f[0].partition('%')
freq[w] = freq.get(w, 0)+n
fp.close()
self.weight = {}
for (w,n) in freq.iteritems():
self.weight[w] = int(convfreq(n))
return
def read_skip(self, fp):
for line in fp:
(line,_,_) = line.strip().partition('#')
if not line: continue
w = line
self.skip.add(w.lower())
return
pat_word = re.compile(r'^[a-zA-Z]+$')
def read(self, name):
fp = self._open_file('index.'+name)
for line in fp:
if line.startswith(' '): continue
f = line.strip().split(' ')
(w, t) = (f[0], f[1])
if not self.pat_word.match(w): continue
if len(w) < 2: continue
w = w.lower()
if t == 'a': # adj
self._add_pos(w, w, 'JJ')
self._add_pos(w, get_comparative(w, self.jjr_exc), 'JJR')
self._add_pos(w, get_superlative(w, self.jjs_exc), 'JJS')
elif t == 'r': # adv
self._add_pos(w, w, 'RB')
self._add_pos(w, get_comparative(w, self.rbr_exc), 'RBR')
self._add_pos(w, get_superlative(w, self.rbs_exc), 'RBS')
elif t == 'n': # noun
self._add_pos(w, w, 'NN')
self._add_pos(w, get_plural(w, self.nns_exc), 'NNS')
elif t == 'v': # verb
self._add_pos(w, w, 'VB')
self._add_pos(w, w, 'VBP')
self._add_pos(w, get_pres3rd(w, self.vbz_exc), 'VBZ')
self._add_pos(w, get_past(w, self.vbd_exc), 'VBD')
self._add_pos(w, get_pastpart(w, self.vbn_exc), 'VBN')
if not '_' in w:
self._add_pos(w, get_gerund(w, self.vbg_exc), 'VBG')
else:
assert 0, (w, t)
return self
def _add_pos(self, w0, w, pos):
if w in self.skip: return
if w in self._words:
(_,poss) = self._words[w]
else:
n = self.weight.get(w0, 0)
poss = set()
self._words[w] = (n, poss)
poss.add(pos)
return
def write(self, g2wpath, w2gpath):
print >>sys.stderr, 'Sorting...'
grp2words = {}
for (w, (n,poss)) in self._words.iteritems():
grp = '%s:%d' % ('+'.join(poss), n)
if grp not in grp2words: grp2words[grp] = []
grp2words[grp].append(w)
word2grp = {}
r = sorted(grp2words.iteritems(), key=lambda (k,v):len(v), reverse=True)
for (grp, words) in r:
words.sort()
for (n,w) in enumerate(words):
word2grp[w] = (grp, n)
print >>sys.stderr, ' Group: %r (%d)' % (grp, len(words))
print >>sys.stderr, 'Writing: %r' % g2wpath
g2w = cdb.cdbmake(g2wpath, g2wpath+'.tmp')
for (grp,words) in grp2words.iteritems():
g2w.add(grp, ' '.join(words))
g2w.finish()
print >>sys.stderr, 'Writing: %r' % w2gpath
w2g = cdb.cdbmake(w2gpath, w2gpath+'.tmp')
for (word,(grp,n)) in word2grp.iteritems():
w2g.add(word, '%s,%d' % (grp,n))
for w in self.skip:
w2g.add(w, ',0')
w2g.finish()
return
# main
def main(argv):
import getopt
def usage():
print 'usage: %s [-O outdir] [-s skip] basedir' % argv[0]
return 100
try:
(opts, args) = getopt.getopt(argv[1:], 'O:s:')
except getopt.GetoptError:
return usage()
outdir = '.'
skips = []
for (k, v) in opts:
if k == '-O': output = v
elif k == '-s': skips.append(v)
if not args: return usage()
basedir = args.pop(0)
converter = DictionaryConverter(basedir)
for path in skips:
fp = file(path, 'r')
converter.read_skip(fp)
fp.close()
converter.read('adj')
converter.read('adv')
converter.read('noun')
converter.read('verb')
g2wpath = os.path.join(outdir, 'g2w.cdb')
w2gpath = os.path.join(outdir, 'w2g.cdb')
converter.write(g2wpath, w2gpath)
return
if __name__ == '__main__': sys.exit(main(sys.argv))