-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertGrammarToHier.py
190 lines (145 loc) · 5.35 KB
/
convertGrammarToHier.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
from __future__ import division
import sys
import cPickle as pickle
from gzip import GzipFile
import re
from path import path
from AIMA import DefaultDict
from HierGrammar import HierGrammar, HierRule
def readLambdas(ff):
lambdas = {}
for line in ff:
if not line.strip():
return lambdas
fields = line.strip().split()
(nt, prob) = fields
prob = float(prob)
lambdas[nt] = prob
return lambdas
def readProductionTable(ff):
ntToWord = DefaultDict({})
ct = 0
for line in ff:
if not line.strip():
return ntToWord
if ct % 1000 == 0:
print >>sys.stderr, "read", ct, "..."
ct += 1
fields = line.strip().split()
(prob, nt, arrow, word) = fields
assert(arrow == "->")
ntToWord[nt][word] = float(prob)
return ntToWord
def listEval(lst):
undelimited = lst.lstrip("[").rstrip("]")
items = undelimited.split(",")
return items
if __name__ == "__main__":
(grammarStem, out) = sys.argv[1:]
print "Grammar stem:", grammarStem, "Output:", out
grammar = HierGrammar(out, mode='w')
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)
assert(arrow == "->")
grammar.addAncestry(level, parNT, childNT)
grammar.writeback("hierarchy")
for level in range(maxLevel+1):
print >>sys.stderr, "Level", level
grammarFile = workDir/("%s-txt-lvl%d.grammar" % (basename, level))
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)
rule.setup(lhs, rhs, prob)
if [rule.lhs,] == rule.rhs and rule.prob == 1.0:
print >>sys.stderr, "Warning: X->X", rule.lhs, rule.rhs
else:
grammar.addRule(rule)
grammar.writeback("grammar")
for level in range(maxLevel+1):
print >>sys.stderr, "Level", level
lexicon = workDir/("%s-txt-lvl%d.lexicon" % (basename, level))
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:]))
for num,prob in enumerate(lst):
preterm = "%s_%d" % (pos, num)
rule = HierRule(level)
rule.setup(preterm, [word,], float(prob))
if [rule.lhs,] == rule.rhs and rule.prob == 1.0:
print >>sys.stderr, "Warning: X->X", rule.lhs, rule.rhs
else:
grammar.addTerminalRule(rule)
grammar.writeback("terminals")
for level in range(maxLevel+1):
print >>sys.stderr, "Level", level
lookahead = workDir/("%s-lookahead-lvl%d.lookahead.gz" %
(basename, level))
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 level == 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")