-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.py
executable file
·251 lines (201 loc) · 8.07 KB
/
util.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
import collections
import nltk
import numpy
import scipy
import sys
import math
import time
"""
"""
def log_add(log_a, log_b):
if log_a < log_b:
return log_b + numpy.log(1 + numpy.exp(log_a - log_b))
else:
return log_a + numpy.log(1 + numpy.exp(log_b - log_a))
class GraphNode(object):
def __init__(self, non_terminal, next_nodes=set()):
self._non_terminal = non_terminal
self._next_nodes = next_nodes
def add_node(self, next_node):
assert (isinstance(next_node, self.__class__))
self._next_nodes.add(next_node)
class AdaptedProduction(nltk.grammar.Production):
def __init__(self, lhs, rhs, productions):
super(AdaptedProduction, self).__init__(lhs, rhs)
self._productions = productions
productions_hash = 0
for production in productions:
productions_hash = productions_hash // 1000000000
productions_hash += production.__hash__()
self._hash = hash((self._lhs, self._rhs, productions_hash))
def get_production_list(self):
return self._productions
def match_grammaton(self, production_list):
if len(self._productions) != len(production_list):
return False
for x in range(len(self._productions)):
if self._productions[x] != production_list[x]:
return False
return True
def __eq__(self, other):
"""
@return: true if this C{Production} is equal to C{other}.
@rtype: C{boolean}
"""
if not isinstance(other, self.__class__):
return False
if self._lhs != other._lhs:
return False
if self._rhs != other._rhs:
return False
return self.match_grammaton(other._productions)
def __str__(self):
str = "%s -> %s (" % (self._lhs, " ".join(["%s" % elt for elt in self._rhs]))
str += ", ".join(["%s" % production for production in self._productions])
str += ")"
return str
def retrieve_tokens_of_adapted_non_terminal(self, adapted_non_terminal):
if self.lhs() == adapted_non_terminal:
return ["".join(self.rhs())]
else:
token_list = []
for candidate_production in self._productions:
if isinstance(candidate_production, self.__class__):
token_list += candidate_production.retrieve_tokens_of_adapted_non_terminal(adapted_non_terminal)
else:
continue
return token_list
def __hash__(self):
return super(AdaptedProduction, self).__hash__()
class HyperNode(object):
def __init__(self,
node,
span):
self._node = node
self._span = span
self._derivation = []
self._log_probability = []
# TODO: this would incur duplication in derivation
# self._derivation_log_probability = {}
self._accumulated_log_probability = float('NaN')
def add_new_derivation(self, production, log_probability, hyper_nodes=None):
self._derivation.append((production, hyper_nodes))
self._log_probability.append(log_probability)
'''
if (production, hyper_nodes) not in self._derivation_log_probability:
self._derivation_log_probability[(production, hyper_nodes)] = log_probability
else:
self._derivation_log_probability[(production, hyper_nodes)] = log_add(log_probability, self._derivation_log_probability[(production, hyper_nodes)])
'''
if math.isnan(self._accumulated_log_probability):
self._accumulated_log_probability = log_probability
else:
self._accumulated_log_probability = log_add(log_probability, self._accumulated_log_probability)
return
def random_sample_derivation(self):
# print self._derivation_log_probability.keys()
random_number = numpy.random.random()
'''
for (production, hyper_nodes) in self._derivation_log_probability:
current_probability = numpy.exp(self._derivation_log_probability[(production, hyper_nodes)] - self._accumulated_log_probability)
if random_number>current_probability:
random_number -= current_probability
else:
return production, hyper_nodes
'''
# print "<<<<<<<<<<debug>>>>>>>>>>"
# for x in xrange(len(self._derivation)):
# print self._derivation[x], numpy.exp(self._log_probability[x] - self._accumulated_log_probability)
# sys.exit()
assert (len(self._derivation) == len(self._log_probability))
for x in range(len(self._derivation)):
current_probability = numpy.exp(self._log_probability[x] - self._accumulated_log_probability)
if random_number > current_probability:
random_number -= current_probability
else:
# return self._derivation[x][0], self._derivation[x][1]
return self._derivation[x][0], self._derivation[x][1], self._log_probability[
x] - self._accumulated_log_probability
def __len__(self):
# return len(self._derivation_log_probability)
return len(self._log_probability)
def __str__(self):
output_string = "[%s (%d:%d) " % (self._node, self._span[0], self._span[1])
'''
for (production, hyper_nodes) in self._derivation_log_probability:
output_string += "<%s" % (production)
print production, len(hyper_nodes)
if hyper_nodes!=None:
print len(hyper_nodes)
for hyper_node in hyper_nodes:
output_string += " %s" % (hyper_node)
output_string += "> "
return output_string
'''
for x in range(len(self._derivation)):
production = self._derivation[x][0]
hyper_nodes = self._derivation[x][1]
output_string += "[%s" % (production)
# print production, len(hyper_nodes)
if hyper_nodes is not None:
# print len(hyper_nodes)
for hyper_node in hyper_nodes:
output_string += " %s" % (hyper_node)
output_string += "] "
return output_string
def __repr__(self):
return self.__str__()
def __hash__(self):
return hash((self._node, self._span))
class PassiveEdge(object):
def __init__(self,
node,
left,
right
):
self._node = node
self._left = left
self._right = right
class ActiveEdge(object):
def __init__(self,
lhs,
rhs,
left,
right,
parsed=0
):
self._lhs = lhs
self._rhs = rhs
self._left = left
self._right = right
self._parsed = parsed
class HyperGraph(object):
def __init__(self):
self._top_down_approach = True
return
def parse(self, sentence, grammar):
words = sentence.split()
self.initialize(words, grammar, PassiveEdge(grammar._start_symbol, 0, len(words)))
while len(self._finishing_agenda) > 0:
while len(self._explore_agenda) > 0:
break
edge = self._finishing_agenda.pop()
self.finish_edge(edge)
return
def initialize(self, words, grammar, goal=None):
# TODO: create new chart and agenda
self._explore_agenda = []
self._finishing_agenda = []
for x in len(words):
self._finishing_agenda.append(PassiveEdge(words[x], x, x + 1))
if self._top_down_approach:
for production in grammar.productions(lhs=grammar._start_symbol):
self._finishing_agenda.append(ActiveEdge(production.lhs(), production.rhs(), 0, 0))
def finish_edge(self, edge):
# TODO: add edge to chart
self.do_fundamental_rule(edge)
self.do_rule_introduction(edge)
def do_fundamental_rule(self, edge):
return
def do_rule_introduction(self, edge):
return