-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrammar.py
More file actions
398 lines (316 loc) · 11.3 KB
/
grammar.py
File metadata and controls
398 lines (316 loc) · 11.3 KB
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
'''
grammar.py
Frederik Roenn Stensaeth, Phineas Callahan
11.19.15
'''
import re, string
class Rule:
"""
Rule is a class for containing rules. An instance of the class has a
left hand side, a right hand side and probability.
"""
def __init__(self, l = None, r = None, p = 0.0, vals = None):
if vals:
self.lhs = vals[0]
self.rhs = vals[1:-1]
self.prob = vals[-1]
else:
self.lhs = l
self.rhs = r
self.prob = p
def __hash__(self):
return hash(self.lhs)+hash(tuple(self.rhs))
def __str__(self):
return self.lhs+' -> '+'|'.join(self.rhs)+' '+str(self.prob)
def __eq__(self, other):
return self.lhs == other.lhs and self.rhs == self.rhs
class Grammar:
"""
Grammar is a class for containing grammars. An instance of the class has
both a set for terminals and non terminals, and dictionaries that contain
the NR and TR rules.
"""
def __init__(self, E = None, N = None, R = None, S = 'TOP', nodes = None):
if len(nodes):
count = self.transform_nodes(nodes)
E, N, R = self.from_count(count)
self.non_terminals = N
self.terminals = E
self.NR = {}
self.TR = {}
for lhs in R:
while len(R[lhs]):
self.add_rule(R[lhs].popitem()[1])
self.start_symbol = S
self.count = 0
def incrementCount(self):
"""
incrementCount() increments the count of the grammar by one.
@params: n/a.
@return: n/a.
"""
self.count = self.count + 1
def convertToCNF(self):
"""
convertToCNF() converts the grammar to CNF from CFG.
@params: n/a.
@return: n/a.
"""
curr_rules = []
for lhs in self.NR:
curr_dict = self.NR[lhs]
while len(curr_dict):
curr_rules.append(curr_dict.popitem()[1])
for rhs in self.TR:
curr_dict = self.TR[rhs]
while len(curr_dict):
curr_rules.append(curr_dict.popitem()[1])
curr_rules = self.TERM(curr_rules)
curr_rules = self.BIN(curr_rules)
curr_rules = self.UNIT(curr_rules)
for rule in curr_rules:
# if rule.lhs == 'NAC':
# print rule
self.add_rule(rule)
self.non_terminals = set(self.NR.keys())
for lhs in self.NR:
for rule in self.NR[lhs].values():
self.non_terminals.update(rule.rhs)
self.terminals = set(self.TR.keys())
def UNIT(self, rules):
"""
UNIT() removes all unit productions in the grammar.
@params: rules.
@return: new rules.
"""
new_rules = []
rule_lookup = {x.lhs : {} for x in rules}
for rule in rules:
rule_lookup[rule.lhs]['|'.join(rule.rhs)] = rule
processed = set()
while len(rules):
rule = rules.pop()
# print rule
if self.isUnit(rule):
processed.add(rule)
# print rule
# print rule in processed
for x in rule_lookup[rule.rhs[0]].values():
prob = x.prob*rule.prob
new_rule = Rule(l = rule.lhs,r = x.rhs,p = prob)
if new_rule not in processed:
if not self.isUnit(new_rule):
new_rules.append(new_rule)
else:
rules.append(new_rule)
# print ' '+str(new_rule),
# print new_rule in processed
else:
new_rules.append(rule)
return new_rules
def TERM(self, rules):
"""
TERM() removes any rule that contains a terminal and something else.
Can be other terminals or other non-terminals.
@params: rules.
@return: new rules.
"""
new_rules = []
while(len(rules)):
rule = rules.pop()
body = rule.rhs
head = rule.lhs
if len(body) >= 2:
for i, symbol in enumerate(body):
if symbol in self.terminals:
new_rule = [self.new_symbol(),]
new_rule.append(symbol)
new_rule.append(1.0)
new_rules.append(Rule(vals = new_rule))
body[i] = new_rule[0]
new_rules.append(rule)
return new_rules
def BIN(self, rules):
"""
BIN() makes sure that everything is a binary. Anything with more than
two non-terminals are made into a sequence of rules that converts them
to binary form.
@params: rules.
@return: new rules.
"""
new_rules = []
while(len(rules)):
rule = rules.pop()
body = rule.rhs
head = rule.lhs
if len(body) > 2:
prev = body.pop()
while len(body) >= 2:
new_head = self.new_symbol()
new_rule = [new_head, body.pop(), prev, 1.0]
new_rules.append(Rule(vals = new_rule))
prev = new_head
new_rule = [head, body[0], prev, rule.prob]
new_rules.append(Rule(vals = new_rule))
else:
new_rules.append(rule)
return new_rules
def isUnit(self, rule):
"""
isUnit() checks whether a rule is a unit production.
@params: rule.
@return: boolean.
"""
return len(rule.rhs) == 1 and rule.rhs[0] not in self.terminals
def new_symbol(self):
"""
new_symbol() creates a new symbol.
@params: n/a.
@return: new symbol.
"""
n_symbol = 'X'+str(self.count)
self.incrementCount()
return n_symbol
def add_rule(self, rule):
"""
add_rule() adds the given rule to the grammar.
@params: rule.
@return: n/a.
"""
entry = {}
if len(rule.rhs) == 1 and rule.rhs[0] in self.terminals:
terminal = rule.rhs[0]
if rule.rhs[0] in self.TR:
entry = self.TR[terminal]
else:
self.TR[terminal] = entry
entry[rule.lhs] = rule
else:
if rule.lhs in self.NR:
entry = self.NR[rule.lhs]
else:
self.NR[rule.lhs] = entry
key = '|'.join(rule.rhs)
entry[key] = rule
def write(self, output_file):
"""
write() writes the grammar to the given output file.
@params: name of output file.
@return: n/a.
"""
for lhs in self.NR:
for rule in self.NR[lhs].values():
rule_str = rule.lhs+'|'+'|'.join(rule.rhs)+'|'+str(rule.prob)
output_file.write(str(rule)+'\n')
output_file.write('\n')
for lhs in self.TR:
for rule in self.TR[lhs].values():
rule_str = rule.lhs+'|'+'|'.join(rule.rhs)+'|'+str(rule.prob)
output_file.write(str(rule)+'\n')
output_file.close()
def transform_nodes(self, nodes):
"""
transform_nodes() takes a set of nodes and calls recursive_count() to
count the number of times each rule is seen in the nodes.
@params: nodes.
@return: count dictionary.
"""
count = {}
for node in nodes:
self.recursive_count(node, count)
return count
def from_count(self, count):
"""
from_count() creates terminal and non terminal sets and a dictionary
of rules from the given count dictionary.
@params: count dictionary.
@return: terminals (set), non terminals (set), rules.
"""
non_terminals = set()
terminals = set()
rules = {}
for lhs in count:
lhs_sum = sum(count[lhs].values())
for key,val in count[lhs].items():
count[lhs][key] = val/lhs_sum
for lhs in count:
for rhs in count[lhs]:
prob = count[lhs][rhs]
body = map(string.strip, rhs.split('|'))
terminals.update(body)
non_terminals.add(lhs)
entry = {}
if lhs in rules:
entry = rules[lhs]
else:
rules[lhs] = entry
entry[rhs] = Rule(l = lhs, r = body, p = prob)
terminals -= non_terminals
return terminals, non_terminals, rules
def recursive_count(self, node, count):
"""
recursive_count() recusively counts the number of times a rule is seen,
given a root node of a tree.
@params: node, count dictionary.
@return: n/a.
"""
if len(node.children):
entry = {}
if node.value in count:
entry = count[node.value]
else:
count[node.value] = entry
children = [child.value for child in node.children]
child_string = '|'.join(children)
if child_string in entry:
entry[child_string] += 1.0
else:
entry[child_string] = 1.0
for child in node.children:
self.recursive_count(child, count)
def read_grammar(input_file):
"""
read_grammar() takes the contents of a file and reads in the grammar from
that file.
@params: contents of file were grammar is stored.
@return: grammar object.
"""
rules = {}
non_terminals = set()
terminals = set()
for line in input_file:
rule = map(string.strip, line.split('|'))
non_terminals.add(rule[0])
terminals.update(rule[1:-1])
entry = {}
if rule[0] in rules:
entry = rules[rule[0]]
else:
rules[rule[0]] = entry
entry[' '.join(rule[1:-1])] = Rule(rule)
terminals -= non_terminals
return Grammar(E = terminals, N = non_terminals, R = rules)
def storeGrammar(prob_dict):
"""
storeGrammar() takes a probabilities dictionary and stores all the
grammar rules with their associated probabilities in grammar.txt.
@params: probabilities dictionary.
@return: n/a.
"""
with open('cfg.txt', 'w') as f:
for lhs in prob_dict:
for rhs in prob_dict[lhs]:
rule = lhs + '|' + rhs + '|' + str(prob_dict[lhs][rhs])
f.write(rule + '\n')
# TEST
#def main():
# d = {}
# d['A'] = {}
# d['B'] = {}
# d['A']['aa zz <br> aa <br> aa <br> bb <br> aa zz <br> aa <br> bb'] = 0.5
# # d['B']['BB BB'] = 0.9
#
# storeGrammar(d)
# convertToCNF('cfg.txt')
if __name__ == '__main__':
main()