-
Notifications
You must be signed in to change notification settings - Fork 44
/
oracle.py
776 lines (693 loc) · 44.3 KB
/
oracle.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#!/usr/bin/python
# class for different oracles
from constants import *
import sys
class Oracle():
def __init__(self,verbose=0):
self.verbose = verbose
def give_ref_action(self):
raise NotImplementedError('Not implemented!')
class DynOracle(Oracle):
'''Using dynamic programming find the optimal action sequence
for a initial state given gold graph
The score of one action sequence is defined as
parsed graph and gold graph
'''
def give_ref_action_seq(self,state,ref_graph):
pass
def give_ref_action(self):
pass
class DetOracleABT(Oracle):
'''
deterministic oracle try to infer the unaligned concepts given gold span graph
'''
def give_ref_action(self,state,ref_graph):
currentIdx = state.idx
currentChildIdx = state.cidx
currentGraph = state.A
return self.give_ref_action_aux(currentIdx,currentChildIdx,currentGraph,ref_graph)
def give_ref_action_aux(self,currentIdx,currentChildIdx,currentGraph,ref_graph):
def isCorrectReplace(childIdx,node,rgraph):
for p in node.parents:
if p in rgraph.nodes and childIdx in rgraph.nodes[p].children:
return True
return False
def getABTParent(gn):
for p in gn.parents:
if not isinstance(p,int):
return p
return None
def need_infer(cidx,gn,cn,gnset,cg,rg):
for p in cn.parents:
#if p not in gnset and rg.isContained(p):
# return False
if p in gn.parents or gn.contains(cg.nodes[p]) or p in gn.children: # correct/merge/swap arc
return False
return True
def is_aligned(abtIdx,rg,cg):
return abtIdx in rg.abt_node_table and rg.abt_node_table[abtIdx] in cg.nodes
def need_swap(idx,idx_p,cg,gc,rg):
cur_head_p = cg.find_true_head(idx_p)
try:
cur_head = cg.abt_node_table[cur_head_p] if not isinstance(cur_head_p,int) else cur_head_p
except KeyError:
print >> sys.stderr,"inferred abt node cur_head_p not in ref graph!"
return None
if cur_head in gc.children:
return cur_head
cur_grand_head = getABTParent(rg.nodes[cur_head])
if (not isinstance(cur_head,int) and cur_grand_head in gc.children):
return cur_grand_head
return None
if currentIdx == START_ID:
return {'type':NEXT2},None
currentNode = currentGraph.nodes[currentIdx] #state.get_current_node()
currentChild = currentGraph.nodes[currentChildIdx] if currentChildIdx in currentGraph.nodes else None #state.get_current_child()
goldNodeSet = [ref_graph.abt_node_table[k] if k in ref_graph.abt_node_table else k for k in ref_graph.nodes]
result_act_type = None
result_act_label = None
if currentIdx in goldNodeSet:
currentIdx_p = currentIdx
if currentIdx not in ref_graph.nodes:
currentIdx = currentGraph.abt_node_table[currentIdx]
goldNode = ref_graph.nodes[currentIdx]
#for child in currentNode.children:
if currentChildIdx:
if currentChildIdx == START_ID:
abtParentIdx = getABTParent(goldNode)
#if state.action_history and state.action_history[-1]['type'] in [REPLACEHEAD,NEXT2,DELETENODE]
if need_infer(currentIdx,goldNode,currentNode,goldNodeSet,currentGraph,ref_graph) \
and abtParentIdx and not is_aligned(abtParentIdx,ref_graph,currentGraph) \
and (len(ref_graph.nodes[abtParentIdx].children) == 1 or not isinstance(currentIdx_p,int) or ((goldNode.words[0] in NOMLIST or currentGraph.sent[currentIdx_p]['lemma'].lower() in NOMLIST) and len(goldNode.words) == 1)):
gold_tag = ref_graph.nodes[abtParentIdx].tag
abt_node_index = ABT_PREFIX+str(currentGraph.abt_node_num)
ref_graph.add_abt_mapping(abtParentIdx,abt_node_index)
currentGraph.add_abt_mapping(abt_node_index,abtParentIdx)
return {'type':INFER},gold_tag
else:
return {'type':NEXT1},START_EDGE
if currentChildIdx in goldNodeSet:
currentChildIdx_p = currentChildIdx
if currentChildIdx not in ref_graph.nodes:
currentChildIdx = currentGraph.abt_node_table[currentChildIdx]
try:
goldChild = ref_graph.nodes[currentChildIdx]
except KeyError:
import pdb
pdb.set_trace()
if goldChild.contains(currentNode) or goldNode.contains(currentChild):
return {'type':MERGE}, None # merge
#result_act_type = {'type':MERGE}
#if currentIdx in goldChild.children and \
# currentChildIdx in goldNode.children:
'''
if currentChildIdx in goldNode.children and ref_graph.is_cycle(currentIdx):
print >> sys.stderr, "Circle detected in gold graph!"
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
'''
#result_act_type = {'type':NEXT1}
#result_act_label = gold_edge
if currentChildIdx in goldNode.children: # correct
#parents_to_add = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_reentrance_constrained(currentIdx_p,currentChildIdx_p)]
parents_to_add = [p for p in goldChild.parents if p not in [currentGraph.abt_node_table[cp] if cp in currentGraph.abt_node_table else cp for cp in currentChild.parents]]
if parents_to_add:
pta = parents_to_add[0]
if pta in ref_graph.abt_node_table: pta = ref_graph.abt_node_table[pta]
if pta in currentGraph.get_possible_parent_unconstrained(currentIdx_p,currentChildIdx_p):
gold_edge = ref_graph.get_edge_label(parents_to_add[0],currentChildIdx)
return {'type':REENTRANCE,'parent_to_add':pta},gold_edge
else:
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
else:
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
thead = need_swap(currentIdx,currentIdx_p,currentGraph,goldChild,ref_graph)
if thead is not None:#in goldChild.children:
gold_edge = ref_graph.get_edge_label(currentChildIdx,thead)
return {'type':SWAP}, gold_edge # swap
#result_act_type = {'type':SWAP}
parents_to_attach = [p for p in goldChild.parents if p not in [currentGraph.abt_node_table[cp] if cp in currentGraph.abt_node_table else cp for cp in currentChild.parents]]
if parents_to_attach:
pta = parents_to_attach[0]
if pta in ref_graph.abt_node_table: pta = ref_graph.abt_node_table[pta]
if pta in currentGraph.get_possible_parent_unconstrained(currentIdx_p,currentChildIdx_p):
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
return {'type':REATTACH,'parent_to_attach':pta},gold_edge
#elif not isinstance(pta,int): # abstract (or unaligned) nodes
# abt_node_index = ABT_PREFIX+str(currentGraph.abt_node_num)
# if pta == parents_to_attach[0]:
# ref_graph.add_abt_mapping(pta,abt_node_index)
# currentGraph.add_abt_mapping(abt_node_index,pta)
# else:
# currentGraph.add_abt_mapping(abt_node_index,parents_to_attach[0])
# return {'type':INFER},None
else:
return {'type':NEXT1},None # violates the attachment constraints
else:
if self.verbose > 1: print >> sys.stderr, "Current child node %s doesn't have parents in gold span graph!"%(currentChildIdx)
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
if goldNode.contains(currentChild):
return {'type':MERGE},None
#result_act_type = {'type':MERGE}
else:
#return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
#k = ref_graph.isContained(currentChildIdx)
#if k:
# return {'type':REATTACH,'parent_to_attach':k}
#else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#if len(currentNode.children) <= len(goldNode.children) and set(currentNode.children).issubset(set(goldNode.children)):
#children_to_add = [c for c in goldNode.children if c not in currentNode.children and c in currentGraph.get_possible_children_constrained(currentIdx)]
#if children_to_add:
# child_to_add = children_to_add[0]
# gold_edge = ref_graph.get_edge_label(currentIdx,child_to_add)
# return {'type':ADDCHILD,'child_to_add':child_to_add,'edge_label':gold_edge}
#else:
gold_tag = goldNode.tag
return {'type':NEXT2, 'tag':gold_tag},None # next: done with the current node move to next one
#else:
# if self.verbose > 2:
# print >> sys.stderr, "ERROR: Missing actions, current node's and gold node's children:%s %s"%(str(currentNode.children), str(goldNode.children))
# pass
elif ref_graph.isContained(currentIdx):
if currentChildIdx:
if currentChildIdx in goldNodeSet:
#goldChild = ref_graph.nodes[currentChildIdx] if currentChildIdx in ref_graph.nodes else ref_graph.nodes[currentGraph.abt_node_table[currentChildIdx]]
currentChildIdx_p = currentChildIdx
if currentChildIdx not in ref_graph.nodes:
currentChildIdx = currentGraph.abt_node_table[currentChildIdx]
goldChild = ref_graph.nodes[currentChildIdx]
if goldChild.contains(currentNode):
return {'type':MERGE},None
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx_p)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if ref_graph.nodes[parents_to_attach[0]].contains(currentNode):
return {'type':NEXT1},None # delay action for future merge
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT2},None
else:
'''
if currentChildIdx:
#assert len(currentNode.children) == 1
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if (isCorrectReplace(currentChildIdx,currentNode,ref_graph,state.beta) or len(currentNode.children) == 1):
return {'type':REPLACEHEAD},None # replace head
#result_act_type = {'type':REPLACEHEAD}
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if isCorrectReplace(parents_to_attach[0],currentNode,ref_graph,state.beta):
return {'type':NEXT1},None # delay action for future replace head
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#return {'type':DELETEEDGE}
#result_act_type = {'type':DELETEEDGE}
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
'''
if currentChildIdx:
if currentChildIdx == START_ID:
return {'type':NEXT1},None
if currentChildIdx in goldNodeSet:
#goldChild = ref_graph.nodes[currentChildIdx] if currentChildIdx in ref_graph.nodes else ref_graph.nodes[currentGraph.abt_node_table[currentChildIdx]]
currentChildIdx_p = currentChildIdx
if currentChildIdx not in ref_graph.nodes:
currentChildIdx = currentGraph.abt_node_table[currentChildIdx]
goldChild = ref_graph.nodes[currentChildIdx]
if isCorrectReplace(currentChildIdx,currentNode,ref_graph) or len(currentNode.children) == 1: #current node's parents are already aligned
return {'type':REPLACEHEAD},None # replace head
else:
parents_to_attach = [p for p in goldChild.parents if p not in [currentGraph.abt_node_table[cp] if cp in currentGraph.abt_node_table else cp for cp in currentChild.parents]]
if parents_to_attach:
pta = parents_to_attach[0]
if pta in ref_graph.abt_node_table: pta = ref_graph.abt_node_table[pta]
if pta in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx_p):
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
return {'type':REATTACH,'parent_to_attach':pta},gold_edge
#elif not isinstance(pta,int) and pta not in currentGraph.nodes: # abstract (or unaligned) nodes
'''
#heuristic 1: just assign the abstract node index according to its first gold child
#assert pta != currentIdx
if pta == parents_to_attach[0] and state.is_possible_align(currentIdx,parents_to_attach[0],ref_graph):
ref_graph.add_abt_mapping(pta,currentIdx)
currentGraph.add_abt_mapping(currentIdx,pta)
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
return {'type':NEXT1},gold_edge
else:
return {'type':REPLACEHEAD},None
'''
else:
return {'type':NEXT1},None # violates the attachment constraints
else:
if self.verbose > 1: print >> sys.stderr, "Current child node %s doesn't have parents in gold span graph!"%(currentChildIdx)
return {'type':NEXT1},None
else:
if self.verbose > 1: print >> sys.stderr, "Current child node %s should have been deleted!"%(currentChildIdx)
return {'type':NEXT1},None
else:
# here currentNode.children must be empty
if currentNode.children and self.verbose > 1: print >> sys.stderr, "Unaligned node %s has children"%(currentNode.start)
# avoid delete name entity; will delete that in further reentrance action
#if currentGraph.sent[currentIdx]['ne'] == 'O' or currentGraph.sent[currentIdx]['pos'] in FUNCTION_TAG:
return {'type':DELETENODE},None
#else:
# return {'type':NEXT2},None
skip = NEXT1 if currentChildIdx else NEXT2
return {'type':skip},None
class DetOracleSC(Oracle):
'''
deterministic oracle keeps strong connectivity of the graph
1) using reattach rather than delete edge
2) discard ADDCHILD
'''
def give_ref_action(self,state,ref_graph):
def isCorrectReplace(childIdx,node,rgraph):
for p in node.parents:
if p in rgraph.nodes and childIdx in rgraph.nodes[p].children:
return True
return False
currentIdx = state.idx
currentChildIdx = state.cidx
currentNode = state.get_current_node()
currentChild = state.get_current_child()
currentGraph = state.A
goldNodeSet = ref_graph.nodes.keys()
result_act_type = None
result_act_label = None
if currentIdx in goldNodeSet:
goldNode = ref_graph.nodes[currentIdx]
#for child in currentNode.children:
if currentChildIdx:
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if goldChild.contains(currentNode) or goldNode.contains(currentChild):
return {'type':MERGE}, None # merge
#result_act_type = {'type':MERGE}
if currentIdx in goldChild.children and \
currentChildIdx in goldNode.children:
if self.verbose > 1: print >> sys.stderr, "Circle detected in gold graph!"
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
#result_act_type = {'type':NEXT1}
#result_act_label = gold_edge
elif currentIdx in goldChild.children:
gold_edge = ref_graph.get_edge_label(currentChildIdx,currentIdx)
return {'type':SWAP}, gold_edge # swap
#result_act_type = {'type':SWAP}
elif currentChildIdx in goldNode.children: # correct
parents_to_add = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_reentrance_constrained(currentIdx,currentChildIdx)]
#parents_to_add = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_add:
gold_edge = ref_graph.get_edge_label(parents_to_add[0],currentChildIdx)
return {'type':REENTRANCE,'parent_to_add':parents_to_add[0]},gold_edge
else:
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
else:
#return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
if goldNode.contains(currentChild):
return {'type':MERGE},None
#result_act_type = {'type':MERGE}
else:
#return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
#k = ref_graph.isContained(currentChildIdx)
#if k:
# return {'type':REATTACH,'parent_to_attach':k}
#else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#if len(currentNode.children) <= len(goldNode.children) and set(currentNode.children).issubset(set(goldNode.children)):
#children_to_add = [c for c in goldNode.children if c not in currentNode.children and c in currentGraph.get_possible_children_constrained(currentIdx)]
#if children_to_add:
# child_to_add = children_to_add[0]
# gold_edge = ref_graph.get_edge_label(currentIdx,child_to_add)
# return {'type':ADDCHILD,'child_to_add':child_to_add,'edge_label':gold_edge}
#else:
gold_tag = goldNode.tag
return {'type':NEXT2, 'tag':gold_tag},None # next: done with the current node move to next one
#else:
# if self.verbose > 2:
# print >> sys.stderr, "ERROR: Missing actions, current node's and gold node's children:%s %s"%(str(currentNode.children), str(goldNode.children))
# pass
elif ref_graph.isContained(currentIdx):
if currentChildIdx:
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if goldChild.contains(currentNode):
return {'type':MERGE},None
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if ref_graph.nodes[parents_to_attach[0]].contains(currentNode):
return {'type':NEXT1},None # delay action for future merge
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT2},None
else:
if currentChildIdx:
#assert len(currentNode.children) == 1
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if isCorrectReplace(currentChildIdx,currentNode,ref_graph):# or len(currentNode.children) == 1:
return {'type':REPLACEHEAD},None # replace head
#result_act_type = {'type':REPLACEHEAD}
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if isCorrectReplace(parents_to_attach[0],currentNode,ref_graph):
return {'type':NEXT1},None # delay action for future replace head
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#return {'type':DELETEEDGE}
#result_act_type = {'type':DELETEEDGE}
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
# here currentNode.children must be empty
return {'type':DELETENODE},None
#result_act_type = {'type':DELETENODE}
skip = NEXT1 if currentChildIdx else NEXT2
return {'type':skip},None
class DetOracle(Oracle):
'''
deterministic oracle keeps weak connectivity of the graph
1) using delete node rather than reattach
2) discard ADDCHILD
'''
def give_ref_action(self,state,ref_graph):
def isCorrectReplace(childIdx,node,rgraph):
for p in node.parents:
if p in rgraph.nodes and childIdx in rgraph.nodes[p].children:
return True
return False
currentIdx = state.idx
currentChildIdx = state.cidx
currentNode = state.get_current_node()
currentChild = state.get_current_child()
currentGraph = state.A
goldNodeSet = ref_graph.nodes.keys()
result_act_type = None
result_act_label = None
if currentIdx in goldNodeSet:
goldNode = ref_graph.nodes[currentIdx]
#for child in currentNode.children:
if currentChildIdx:
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if goldChild.contains(currentNode) or goldNode.contains(currentChild):
return {'type':MERGE}, None # merge
#result_act_type = {'type':MERGE}
if currentIdx in goldChild.children and \
currentChildIdx in goldNode.children:
if self.verbose > 1: print >> sys.stderr, "Circle detected in gold graph!"
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
#result_act_type = {'type':NEXT1}
#result_act_label = gold_edge
elif currentIdx in goldChild.children:
gold_edge = ref_graph.get_edge_label(currentChildIdx,currentIdx)
return {'type':SWAP}, gold_edge # swap
#result_act_type = {'type':SWAP}
elif currentChildIdx in goldNode.children: # correct
parents_to_add = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_reentrance_constrained(currentIdx,currentChildIdx)]
#parents_to_add = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_add:
gold_edge = ref_graph.get_edge_label(parents_to_add[0],currentChildIdx)
return {'type':REENTRANCE,'parent_to_add':parents_to_add[0]},gold_edge
else:
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1}, gold_edge # next
else:
#return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
if goldNode.contains(currentChild):
return {'type':MERGE},None
#result_act_type = {'type':MERGE}
else:
#return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
#k = ref_graph.isContained(currentChildIdx)
#if k:
# return {'type':REATTACH,'parent_to_attach':k}
#else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#if len(currentNode.children) <= len(goldNode.children) and set(currentNode.children).issubset(set(goldNode.children)):
#children_to_add = [c for c in goldNode.children if c not in currentNode.children and c in currentGraph.get_possible_children_constrained(currentIdx)]
#if children_to_add:
# child_to_add = children_to_add[0]
# gold_edge = ref_graph.get_edge_label(currentIdx,child_to_add)
# return {'type':ADDCHILD,'child_to_add':child_to_add,'edge_label':gold_edge}
#else:
gold_tag = goldNode.tag
return {'type':NEXT2, 'tag':gold_tag},None # next: done with the current node move to next one
#else:
# if self.verbose > 2:
# print >> sys.stderr, "ERROR: Missing actions, current node's and gold node's children:%s %s"%(str(currentNode.children), str(goldNode.children))
# pass
elif ref_graph.isContained(currentIdx):
if currentChildIdx:
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if goldChild.contains(currentNode):
return {'type':MERGE},None
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if ref_graph.nodes[parents_to_attach[0]].contains(currentNode):
return {'type':NEXT1},None # delay action for future merge
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
return {'type':NEXT2},None
else:
if currentChildIdx:
#assert len(currentNode.children) == 1
if currentChildIdx in goldNodeSet:
goldChild = ref_graph.nodes[currentChildIdx]
if isCorrectReplace(currentChildIdx,currentNode,ref_graph):# or len(currentNode.children) == 1:
return {'type':REPLACEHEAD},None # replace head
#result_act_type = {'type':REPLACEHEAD}
else:
parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_constrained(currentIdx,currentChildIdx)]
#parents_to_attach = [p for p in goldChild.parents if p not in currentChild.parents and p in currentGraph.get_possible_parent_unconstrained(currentIdx,currentChildIdx)]
if parents_to_attach:
if isCorrectReplace(parents_to_attach[0],currentNode,ref_graph):
return {'type':NEXT1},None # delay action for future replace head
else:
gold_edge = ref_graph.get_edge_label(parents_to_attach[0],currentChildIdx)
#if gold_edge == 'x': # not actually gold edge, skip this
# return {'type':NEXT1},None
#else:
return {'type':REATTACH,'parent_to_attach':parents_to_attach[0]},gold_edge
else:
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
#return {'type':DELETEEDGE}
#result_act_type = {'type':DELETEEDGE}
return {'type':NEXT1},None
#return {'type':REATTACH,'parent_to_attach':None},None
else:
# here currentNode.children must be empty
return {'type':DELETENODE},None
#result_act_type = {'type':DELETENODE}
skip = NEXT1 if currentChildIdx else NEXT2
return {'type':skip},None
'''
give local optimal action based on current state and the gold graph
def give_ref_action_seq(self,state):
pass
def give_ref_action(self,state,ref_graph):
def isCorrectReplace(childIdx,node,rgraph):
for p in node.parents:
if p in rgraph.nodes and childIdx in rgraph.nodes[p].children:
return True
return False
currentIdx = state.idx
currentChildIdx = state.cidx
currentNode = state.get_current_node()
currentChild = state.get_current_child()
currentGraph = state.A
goldNodeSet = ref_graph.nodes.keys()
result_act_type = None
result_act_label = None
if currentIdx in goldNodeSet:
goldNode = ref_graph.nodes[currentIdx]
#for child in currentNode.children:
if currentChildIdx:
if currentChildIdx in goldNodeSet:
if ref_graph.nodes[currentChildIdx].contains(currentNode) or ref_graph.nodes[currentIdx].contains(currentChild):
return {'type':MERGE} # merge
#result_act_type = {'type':MERGE}
if currentIdx in ref_graph.nodes[currentChildIdx].children and \
currentChildIdx in ref_graph.nodes[currentIdx].children:
print >> sys.stderr, "Circle detected in gold graph!"
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1, 'edge_label':gold_edge} # next
#result_act_type = {'type':NEXT1}
#result_act_label = gold_edge
elif currentIdx in ref_graph.nodes[currentChildIdx].children:
return {'type':SWAP} # swap
#result_act_type = {'type':SWAP}
elif currentChildIdx in ref_graph.nodes[currentIdx].children: # correct
gold_edge = ref_graph.get_edge_label(currentIdx,currentChildIdx)
return {'type':NEXT1, 'edge_label':gold_edge} # next
#result_act_type = {'type':NEXT1}
#result_act_label = gold_edge
else:
return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
else:
if ref_graph.nodes[currentIdx].contains(currentChild):
return {'type':MERGE}
#result_act_type = {'type':MERGE}
else:
return {'type':DELETEEDGE} # delete edge
#result_act_type = {'type':DELETEEDGE}
else:
if set(currentNode.children) == set(goldNode.children):
gold_tag = goldNode.tag
return {'type':NEXT2, 'tag':gold_tag} # next: done with the current node move to next one
#result_act_type = {'type':NEXT2,'tag':gold_tag}
elif len(currentNode.children) < len(goldNode.children):
nodes_to_add = [c for c in goldNode.children if c not in currentNode.children]
child_to_add = nodes_to_add[0]
if child_to_add != 0 and child_to_add != currentIdx \
and child_to_add not in currentNode.children and child_to_add in currentGraph.nodes:
gold_edge = ref_graph.get_edge_label(currentIdx,child_to_add)
return {'type':ADDCHILD, 'child_to_add':child_to_add, 'edge_label':gold_edge} # add one child each action
#result_act_type = {'type':ADDCHILD, 'child_to_add':nodes_to_add[0]}
#result_act_label = gold_edge
else:
if self.verbose > 2:
print >> sys.stderr, "Not a correct link between %s and %s!"%(currentIdx,nodes_to_add[0])
return {'type':NEXT2}
#result_act_type = {'type':NEXT2}
else:
if self.verbose > 2:
print >> sys.stderr, "Missing actions, current node's and gold node's children:%s %s"%(str(currentNode.children), str(goldNode.children))
pass
elif ref_graph.isContained(currentIdx):
if currentChildIdx and currentChildIdx in goldNodeSet and \
ref_graph.nodes[currentChildIdx].contains(currentNode):
return {'type':MERGE}
#result_act_type = {'type':MERGE}
else:
if currentChildIdx:
return {'type':NEXT1}
#result_act_type = {'type':NEXT1}
else:
return {'type':NEXT2}
#result_act_type = {'type':NEXT2}
else:
if currentChildIdx:
#assert len(currentNode.children) == 1
if currentChildIdx in goldNodeSet:
if (isCorrectReplace(currentChildIdx,currentNode,ref_graph) or len(currentNode.children) == 1):
return {'type':REPLACEHEAD} # replace head
#result_act_type = {'type':REPLACEHEAD}
else:
return {'type':NEXT1} #
#result_act_type = {'type':NEXT1}
else:
return {'type':DELETEEDGE}
#result_act_type = {'type':DELETEEDGE}
else:
# here currentNode.children must be empty
return {'type':DELETENODE}
#result_act_type = {'type':DELETENODE}
skip = NEXT1 if currentChildIdx else NEXT2
return {'type':skip}
'''