-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransition_utils.py
455 lines (309 loc) · 14 KB
/
transition_utils.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
449
450
451
452
453
454
455
import networkx as nx
import preprocessing
def inOrder(graph,root,traversal):
# in order traversal of a graph for the transition based system
if (root != None):
children=graph.successors(root)
leftChildren=[]
rightChildren=[]
for child in children:
rootId=root.split("/")[0]
childId=child.split("/")[0]
if rootId>childId:
leftChildren.append(child)
elif rootId<childId:
rightChildren.append(child)
leftChildren=sortNodeList(leftChildren)
rightChildren=sortNodeList(rightChildren)
#print "node " + root + " left " + str(leftChildren)
#print "node " + root + " right " + str(rightChildren)
for left in leftChildren:
inOrder(graph,left,traversal);
#Visit the node by Printing the node data
#print root
traversal.append(root)
#print root
for right in rightChildren:
inOrder(graph,right,traversal);
return traversal
def sortNodeList(list):
# sort the node list based on the id of the node
# return the sorted list
sortedList=[]
if len(list)>0:
ids=[]
for l in list:
ids.append(int(l.split("/")[0]))
maxId=max(ids)
for i in range(maxId+1):
for l in list:
id=int(l.split("/")[0])
if i==id :
sortedList.append(l)
return sortedList
class graphObject:
# structure that holds the transition based graph after an inorder traversal
def __init__(self,name,nodeId,traversedId,nodeName,type):
self.name=name
self.nodeId=int(nodeId)
self.traversedId=int(traversedId)
self.nodeName=nodeName
self.type=type
class Oracle:
def __init__(self,graphObjects,graph):
# the oracle with the swap operation inside
mch=machine()
mch.stack.append(graphObjects[0])
mch.bufferL=graphObjects[1:]
self.stacks=[]
self.buffers=[]
self.actions=[]
self.arcs=[]
self.arcs.append(list(mch.arcs))
self.stacks.append(list(mch.stack))
self.buffers.append(list(mch.bufferL))
nodeDependents=[]
nodeNames=[]
for g in graphObjects:
#print g.nodeName
nodeNames.append(g.nodeName)
dependentsOfNode=graph.graph.successors(g.nodeName)
nodeDependents.append(dependentsOfNode)
self.iterations=0
while (mch.isNotInEndState()):
top_2=mch.stack[len(mch.stack)-2]
top_1=mch.stack[len(mch.stack)-1]
idxHead=nodeNames.index(top_2.nodeName)
idxTail=nodeNames.index(top_1.nodeName)
projOrderHead=getIndexGivenTravId(graphObjects,idxHead)
projOrderTail=getIndexGivenTravId(graphObjects,idxTail)
dependent=None
# add left or right relation if there are no dependents
if graph.graph.has_edge(top_2.nodeName,top_1.nodeName) and top_1.nodeId!=0:
side="right"
head=top_2
dependent=top_1
elif graph.graph.has_edge(top_1.nodeName,top_2.nodeName) and top_2.nodeId!=0:
side= "left"
head=top_1
dependent=top_2
action=None
if dependent!=None and len(nodeDependents[nodeNames.index(dependent.nodeName)])==0:# check if it has no dependents left
if side=="left":
mch.leftArc()
action= "left"
elif side=="right":
mch.rightArc()
action= "right"
idxHead = nodeNames.index(head.nodeName)
nodeDependents[idxHead].remove(dependent.nodeName) # remove the dependent from the head dependents' list
elif top_2.nodeId>0 and top_2.nodeId<top_1.nodeId and projOrderHead>projOrderTail:
mch.swap()
action= "swap"
elif len(mch.bufferL)>0:
mch.shift()
action= "shift"
self.actions.append(action)
self.stacks.append(list(mch.stack))
self.buffers.append(list(mch.bufferL))
self.arcs.append(list(mch.arcs))
self.iterations+=1
class constraints: # execute permissible actions based on the predictions of the classifier for the greedy ransition based system
def __init__(self,graphObject,clf,vectorizer):
mch=machine()
mch.stack.append(graphObject[0])
mch.bufferL=graphObject[1:]
self.stacks=[]
self.buffers=[]
self.actions=[]
self.stacks.append(list(mch.stack))
self.buffers.append(list(mch.bufferL))
predsOverall=0
while (mch.isNotInEndState()):
preds=getPrediction(mch,clf,vectorizer,graphObject)[0]
rankedActions,predsR=getRankedActions(clf,preds)
stop=False
cl=0
while (stop==False):
rAction=rankedActions[cl]
if (mch.isPermissible(rAction)):
predsOverall+=predsR[cl]
if rAction=="left":
mch.leftArc()
elif rAction=="right":
mch.rightArc()
elif rAction=="swap":
mch.swap()
elif rAction=="shift":
mch.shift()
stop=True
self.actions.append(rAction)
self.stacks.append(list(mch.stack))
self.buffers.append(list(mch.bufferL))
else:
cl+=1
self.arcs=mch.arcs
class machine:
# Machine actions
def __init__(self):
self.stack=[]
self.bufferL=[]
self.arcs=[]
self.actions=[]
self.parent=None
self.ds=0
self.gradientAdded=False
self.isCorrect=None
self.wasCorrect=None
def swap(self):
top_2=self.stack[len(self.stack)-2]
self.stack.remove(top_2)
self.bufferL=[top_2]+self.bufferL
self.actions.append("swap")
def shift(self):
top=self.bufferL[0]
self.bufferL.remove(top)
self.stack.append(top)
self.actions.append("shift")
def leftArc(self):
top_2=self.stack[len(self.stack)-2]
top_1=self.stack[len(self.stack)-1]
self.arcs.append(arcObject(top_1,"part-of",top_2,"left_arc"))
self.stack.remove(top_2)
self.actions.append("left")
def rightArc(self):
top_2=self.stack[len(self.stack)-2]
top_1=self.stack[len(self.stack)-1]
self.arcs.append(arcObject(top_2,"part-of",top_1,"right_arc"))
self.stack.remove(top_1)
self.actions.append("right")
def isNotInEndState(self):
return len(self.bufferL)>0 or len(self.stack)!=1
def getRandomAction(self):
import random
intAction=random.randint(0,3)
if intAction==0:
action="left"
elif intAction==1:
action="right"
elif intAction==2:
action="shift"
elif intAction==3:
action="swap"
return action
def isPermissible(self,action):
top_2=self.stack[len(self.stack)-2]
top_1=self.stack[len(self.stack)-1]
if action=="left" and top_2.nodeId!=0: #and top_1.nodeId>top_2.nodeId:
return True
elif action=="right" and top_1.nodeId!=0: #and top_1.nodeId>top_2.nodeId:
return True
elif action=="swap" and top_2.nodeId>0 and top_2.nodeId<top_1.nodeId:
return True
elif action=="shift" and len(self.bufferL)>0 :
return True
return False
def getPermissibleActions(self):
actions=["left","right","swap","shift"]
permissibleActions=[]
for action in actions:
if self.isPermissible(action):
permissibleActions.append(action)
return permissibleActions
def graphFromArcObject(arcs):
graph_test=nx.DiGraph()
for arc in arcs:
graph_test.add_edge(arc.left.nodeName,arc.right.nodeName)
return graph_test
class arcObject:
def __init__(self,left,label,right,transition):
self.left=left
self.label=label
self.right=right
self.transition=transition
def getIndexGivenTravId(graphObjects,tId):
for idx in range(len(graphObjects)):
if graphObjects[idx].traversedId==tId:
return idx
def parseGraphs(graph,traversal,node_docs):
nodes=[]
for node in graph.graph:
nodes.append(node)
nodes=sortNodeList(nodes)
graphObjects=[]
for i in range(len(nodes)):
node=nodes[i]
id=int(node.split("/")[0])
name=node.split("/")[1]
traversedId=int(traversal[i].split("/")[0])
type=None
for i in range (len(node_docs[graph.incrementalId].nodeId)):
if int(node_docs[graph.incrementalId].nodeId[i])==id:
#print "mphka"
type=node_docs[graph.incrementalId].type[i]
#print type
graphObjects.append(graphObject(name,id,traversedId,node,type))
return graphObjects
def getRankedActions(clf,preds):
rankActions=[]
predsR=[]
sortedPreds=sorted(preds)
for spred in reversed(sortedPreds):
for i in range(len(preds)):
pred=preds[i]
if pred==spred:
rankActions.append(clf.classes_[i])
predsR.append(pred)
return rankActions,predsR
def getPrediction(machine,clf,vectorizer,graphObj):
stack=machine.stack
bufferL=machine.bufferL
arcs=machine.arcs
configFeatures=preprocessing.transitionBasedFeaturePreprocessing().process(stack,bufferL,arcs,graphObj)
testSparse=vectorizer.transform([configFeatures])
return clf.decision_function(testSparse)
def getLabelsFromPredictions(node_docs_test,clf,vectorizer): # predict the labels based on permissible actions on the transition based system
pred_labels=[]
print "Computing predictions..."
for doc in node_docs_test:
graphObj=[]
print ".",
for idx in range(len(doc.nodeId)):
graphObj.append(graphObject(doc.mention[idx],doc.nodeId[idx],-1,doc.nodeId[idx]+"/"+doc.mention[idx],doc.type[idx]))
con=constraints(graphObj,clf,vectorizer)
predictions=con.arcs
for lidx in range(len(doc.nodeId)):
for ridx in range(len(doc.nodeId)):
lnodeId=int(doc.nodeId[lidx])
rnodeId=int(doc.nodeId[ridx])
if lnodeId!=rnodeId and rnodeId!=0:
label=0
for arc in predictions:
if lnodeId==arc.left.nodeId and rnodeId==arc.right.nodeId:
label=1
pred_labels.append(label)
print ""
return pred_labels
def getLabelsFromGoldFile(node_docs_test,goldFileDocsTest): # get the labels from the gold file
labels_gold=[]
print "Get the test labels.."
for doc in node_docs_test:
graph=nx.DiGraph()
print ".",
for lidx in range(len(doc.nodeId)):
for ridx in range(len(doc.nodeId)):
lnodeId=int(doc.nodeId[lidx])
rnodeId=int(doc.nodeId[ridx])
lnodeMn=doc.mention[lidx]
rnodeMn=doc.mention[ridx]
if lnodeId!=rnodeId and rnodeId!=0:
label=0
for gf in goldFileDocsTest:
if doc.incrementalId==gf.incrementalId:
for relIdx in range(len(gf.left_mention)):
if lnodeId==int(gf.left_id[relIdx]) and rnodeId==int(gf.right_id[relIdx]):
label=1
graph.add_edge(str(lnodeId)+"/"+lnodeMn,str(rnodeId)+"/"+rnodeMn)
labels_gold.append(label)
print ""
return labels_gold