-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctfScheme.py
448 lines (348 loc) · 13.2 KB
/
ctfScheme.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
from __future__ import division
import sys
import cPickle as pickle
from gzip import GzipFile
import re
from math import log, exp
from AIMA import DefaultDict
from HierGrammar import HierGrammar, HierRule
from convertLCGrammarToHier import readLambdas, readProductionTable, listEval
from path import path
from treeUtils import binarizeTree, treeToTuple, treeToStr, zeroSplit
ec05 = [
{
"ROOT_0":["ROOT_0"],
"P":["HP", "MP"],
},
{
"ROOT_0":["ROOT_0"],
"HP":["S_", "N_"],
"MP":["A_", "P_"],
},
{
"ROOT_0":["ROOT_0"],
"S_":["S_0", "VP_0", "UCP_0", "SQ_0", "SBAR_0", "SBARQ_0", "SINV_0"],
"N_":["NP_0", "NAC_0", "NX_0", "LST_0", "X_0", "UCP_0", "FRAG_0"],
"A_":["ADJP_0", "QP_0", "CONJP_0", "ADVP_0", "INTJ_0", "PRN_0"],
"P_":["PP_0", "PRT_0", "RRC_0", "WHADJP_0", "WHADVP_0",
"WHNP_0", "WHPP_0"],
},
]
def leftCorner(tree):
if type(tree[1]) is not tuple:
return (tree[0].split("_")[0], tree[1])
return leftCorner(tree[1])
class ValidationEvents:
def __init__(self):
self.events = DefaultDict(DefaultDict(0))
def record(self, tree):
if type(tree[1]) is not tuple:
#terminal
pass
else:
lhs = tree[0]
(leftPos, leftWord) = leftCorner(tree)
self.events[lhs][(leftPos, leftWord)] += 1
for child in tree[1:]:
self.record(child)
class GrammarStats:
def __init__(self):
self.ruleCounts = DefaultDict(DefaultDict(0))
self.ruleTotals = DefaultDict(0)
self.termCounts = DefaultDict(DefaultDict(0))
self.termTotals = DefaultDict(0)
self.ntToWord = DefaultDict(DefaultDict(0))
self.ntToPos = DefaultDict(DefaultDict(0))
self.ntToWordTot = DefaultDict(0)
self.ntToPosTot = DefaultDict(0)
self.lambdas = {}
def addToGrammar(self, grammar, level):
for pos in self.termCounts:
#pos tags are not merged by this ctf
# print pos
grammar.addAncestry(level, pos, pos)
for lhs,subtab in self.ruleCounts.items():
for rhs, prob in subtab.items():
rule = HierRule(level)
if rhs[0] == "EPSILON":
rhs = []
rule.setup(lhs, rhs, prob)
if rule.epsilon():
grammar.addEpsilonRule(rule)
else:
grammar.addRule(rule)
for lhs, subtab in self.termCounts.items():
for word, prob in subtab.items():
rule = HierRule(level)
rule.setup(lhs, [word,], prob)
grammar.addTerminalRule(rule)
for lhs, subtab in self.ntToWord.items():
for word, prob in subtab.items():
grammar.addWordLookahead(lhs, word, prob, level)
for lhs, subtab in self.ntToPos.items():
self.ntToPos[lhs] = dict(subtab)
grammar.addNTToPos(self.ntToPos, level)
grammar.addLambdas(self.lambdas, level)
def record(self, tree):
if type(tree[1]) is not tuple:
#terminal
lhs = tree[0]
word = tree[1]
self.termCounts[lhs][word] += 1
self.termTotals[lhs] += 1
else:
lhs = tree[0]
rhs = tuple([st[0] for st in tree[1:]])
self.ruleCounts[lhs][rhs] += 1
self.ruleTotals[lhs] += 1
(leftPos, leftWord) = leftCorner(tree)
self.ntToWord[lhs][leftWord] += 1
self.ntToPos[lhs][leftPos] += 1
self.ntToWordTot[lhs] += 1
self.ntToPosTot[lhs] += 1
for child in tree[1:]:
self.record(child)
def normalize(self):
for lhs,tot in self.ruleTotals.items():
subtab = self.ruleCounts[lhs]
for rhs in subtab:
subtab[rhs] /= tot
for lhs,tot in self.termTotals.items():
subtab = self.termCounts[lhs]
for rhs in subtab:
subtab[rhs] /= tot
for lhs,tot in self.ntToWordTot.items():
subtab = self.ntToWord[lhs]
for rhs in subtab:
subtab[rhs] /= tot
for lhs,tot in self.ntToPosTot.items():
subtab = self.ntToPos[lhs]
for rhs in subtab:
subtab[rhs] /= tot
def learnLambdas(self, validation):
(ll, wordRight,norm) = self.estep(validation)
for step in range(10):
print "LL", ll
for lhs,tot in norm.items():
lamb = wordRight[lhs]/tot
if lamb < 1e-5:
lamb = 1e-5
if lamb > 1 - 1e-5:
lamb = 1 - 1e-5
self.lambdas[lhs] = lamb
(ll, wordRight,norm) = self.estep(validation)
def estep(self, validation):
ll = 0
wordRight = DefaultDict(0)
norm = DefaultDict(0)
for lhs,subtab in validation.events.items():
for (pos, word),ct in subtab.items():
try:
pGivenWord = self.ntToWord[lhs][word]
except KeyError:
pGivenWord = 0
try:
pGivenPOS = self.ntToPos[lhs][pos] * \
self.termCounts[pos][word]
except KeyError:
pGivenPOS = 0
total = pGivenWord + pGivenPOS
if total == 0:
# print "WARNING, zero-prob event", lhs, word, pos, ct
continue
wordRight[lhs] += (pGivenWord / total) * ct
norm[lhs] += total * ct
ll += log(total) * ct
return ll, wordRight, norm
if __name__ == "__main__":
(grammarStem, out, trees, validation) = sys.argv[1:]
print "Grammar stem:", grammarStem, "Output:", out, "Training:", trees,\
"Validation:", validation
grammar = HierGrammar(out, mode='w')
topLevel = len(ec05) - 1
for level,mapping in reversed(list(enumerate(ec05))):
# print level, mapping
for anc,children in mapping.items():
for child in children:
grammar.addAncestry(level + 1, anc, child)
grammar.addAncestry(level + 1, "@%s" % anc, "@%s" % child)
# print grammar.hierarchy
grammar.makeMapping(topLevel)
# print grammar.pennToLevel
for level in range(len(ec05)):
gstats = GrammarStats()
vstats = ValidationEvents()
for ct,line in enumerate(file(trees)):
if ct % 100 == 0:
print "read trees", ct
tree = grammar.transform(level + 1, zeroSplit(
binarizeTree(
treeToTuple(line.strip()))))
# print treeToStr(tree)
gstats.record(tree)
for ct,line in enumerate(file(validation)):
if ct % 1000 == 0:
print "read validation trees", ct
tree = grammar.transform(level + 1, zeroSplit(
binarizeTree(
treeToTuple(line.strip()))))
vstats.record(tree)
gstats.normalize()
gstats.learnLambdas(vstats)
print gstats.lambdas
# print gstats.ntToWord
gstats.addToGrammar(grammar, level)
for pos in gstats.termCounts:
#pos tags are not merged by this ctf
# print pos
grammar.addAncestry(topLevel + 1, pos, pos)
#begin copied code
grammarStem = path(grammarStem).abspath()
workDir = grammarStem.dirname()
basename = grammarStem.basename()
fileLst = workDir.files(basename+"-txt-lvl*")
fileNums = [re.search("-txt-lvl(\d+)", fileName) for fileName in fileLst]
fileNums = [int(match.group(1)) for match in fileNums if match]
maxLevel = max(fileNums)
print "Max grammar level:", maxLevel
hierFile = workDir/("%s-txt.hier" % (basename,))
for line in file(hierFile):
fields = line.strip().split()
(level, parNT, arrow, childNT) = fields
level = int(level)
level += topLevel + 1
assert(arrow == "->")
grammar.addAncestry(level, parNT, childNT)
grammar.writeback("hierarchy")
for realLevel in range(maxLevel+1):
print >>sys.stderr, "Level", realLevel
grammarFile = workDir/("%s-txt-lvl%d.grammar" % (basename, realLevel))
level = realLevel + topLevel + 1
print >>sys.stderr, "Nonterms from", grammarFile
ct = 0
for line in file(grammarFile):
if ct % 1000 == 0:
print >>sys.stderr, ct, "..."
ct += 1
fields = line.strip().split()
(lhs, arrow, rhs1) = fields[0:3]
assert(arrow == "->")
if len(fields) == 5:
rhs = [rhs1, fields[3]]
prob = fields[4]
elif len(fields) == 4:
rhs = [rhs1,]
prob = fields[3]
prob = float(prob)
rule = HierRule(level)
if lhs.startswith("EPSILON"):
assert(len(rhs) == 1)
assert(rhs[0].startswith("EPSILON"))
rhs = []
rule.setup(lhs, rhs, prob)
if rule.epsilon() or rule.unary():
# print >>sys.stderr, "Skipping bogus unary", rule
pass
else:
grammar.addRule(rule)
unaryFile = workDir/("%s-txt-lvl%d.unaries.gz" %
(basename, realLevel))
print >>sys.stderr, "Unaries from", unaryFile
ct = 0
for line in GzipFile(unaryFile):
if ct % 1000 == 0:
print >>sys.stderr, ct, "..."
ct += 1
if not line.strip():
continue
#copied, factor
fields = line.strip().split()
(prob, lhs, arrow, rhs1) = fields
assert(arrow == "->")
rhs = [rhs1,]
prob = float(prob)
rule = HierRule(level)
#there will be no rules directly inserting the terminal
#epsilon in this file, because no terminal rules are in
#this file...
rule.setup(lhs, rhs, prob)
if [rule.lhs,] == rule.rhs:
print >>sys.stderr, "Warning: X->X", rule.lhs, rule.rhs
elif not rule.unary():
print >>sys.stderr, "WARNING: non-unary", rule
assert(0)
else:
grammar.addRule(rule)
grammar.writeback("grammar")
for realLevel in range(maxLevel+1):
print >>sys.stderr, "Level", realLevel
lexicon = workDir/("%s-txt-lvl%d.lexicon" % (basename, realLevel))
level = realLevel + topLevel + 1
print >>sys.stderr, "Terminals from", lexicon
ct = 0
for line in file(lexicon):
if ct % 1000 == 0:
print >>sys.stderr, ct, "..."
ct += 1
fields = line.strip().split()
(pos, word) = fields[0:2]
lst = listEval(" ".join(fields[2:]))
if word == "EPSILON":
rhs = []
else:
rhs = [word,]
for num,prob in enumerate(lst):
preterm = "%s_%d" % (pos, num)
rule = HierRule(level)
rule.setup(preterm, rhs, float(prob))
if [rule.lhs,] == rule.rhs:
print >>sys.stderr, "Warning: X->X", rule.lhs, rule.rhs
elif rule.epsilon():
grammar.addEpsilonRule(rule)
else:
grammar.addTerminalRule(rule)
grammar.writeback("terminals")
grammar.writeback("epsilons")
for realLevel in range(maxLevel+1):
print >>sys.stderr, "RealLevel", realLevel
lookahead = workDir/("%s-txt-lvl%d.lookahead.gz" %
(basename, realLevel))
level = realLevel + topLevel + 1
print >>sys.stderr, "Lookahead data from", lookahead
if lookahead.endswith(".gz"):
look = GzipFile(lookahead)
else:
look = file(lookahead)
lambdas = readLambdas(look)
grammar.addLambdas(lambdas, level)
ntToPos = readProductionTable(look)
grammar.addNTToPos(ntToPos, level)
print >>sys.stderr, "Nonterm to word"
ct = 0
for line in look:
if not line.strip():
break
if ct % 1000 == 0:
print >>sys.stderr, "read", ct, "..."
ct += 1
fields = line.strip().split()
(prob, nt, arrow, word) = fields
assert(arrow == "->")
grammar.addWordLookahead(nt, word, float(prob), level)
if realLevel == 0:
#this table doesn't depend on level
print >>sys.stderr, "Pos to word"
ct = 0
for line in look:
if not line.strip():
break
if ct % 1000 == 0:
print >>sys.stderr, "read", ct, "..."
ct += 1
fields = line.strip().split()
(prob, pos, arrow, word) = fields
assert(arrow == "->")
grammar.addPosToWord(pos, word, float(prob))
grammar.writeback("posToWord")
grammar.writeback("lookahead")
grammar.writeback("ntToWord")