-
Notifications
You must be signed in to change notification settings - Fork 6
/
aligner.py
executable file
·1573 lines (1117 loc) · 85.8 KB
/
aligner.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from wordSim import *
from util import *
from coreNlpUtil import *
##############################################################################################################################
def alignNouns(source, target, sourceParseResult, targetParseResult, existingAlignments):
# source and target:: each is a list of elements of the form:
# [[character begin offset, character end offset], word index, word, lemma, pos tag]
global ppdbSim
global theta1
nounAlignments = []
sourceWordIndices = [i+1 for i in xrange(len(source))]
targetWordIndices = [i+1 for i in xrange(len(target))]
sourceWordIndicesAlreadyAligned = sorted(list(set([item[0] for item in existingAlignments])))
targetWordIndicesAlreadyAligned = sorted(list(set([item[1] for item in existingAlignments])))
sourceWords = [item[2] for item in source]
targetWords = [item[2] for item in target]
sourceLemmas = [item[3] for item in source]
targetLemmas = [item[3] for item in target]
sourcePosTags = [item[4] for item in source]
targetPosTags = [item[4] for item in target]
sourceDParse = dependencyParseAndPutOffsets(sourceParseResult)
targetDParse = dependencyParseAndPutOffsets(targetParseResult)
numberOfNounsInSource = 0
evidenceCountsMatrix = {}
relativeAlignmentsMatrix = {}
wordSimilarities = {}
# construct the two matrices in the following loop
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or (sourcePosTags[i-1][0].lower() <> 'n' and sourcePosTags[i-1].lower()<>'prp'):
continue
numberOfNounsInSource += 1
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or (targetPosTags[j-1][0].lower() <> 'n' and targetPosTags[j-1].lower()<>'prp'):
continue
if max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))<ppdbSim:
continue
wordSimilarities[(i, j)] = max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))
sourceWordParents = findParents(sourceDParse, i, sourceWords[i-1])
sourceWordChildren = findChildren(sourceDParse, i, sourceWords[i-1])
targetWordParents = findParents(targetDParse, j, targetWords[j-1])
targetWordChildren = findChildren(targetDParse, j, targetWords[j-1])
# search for common or equivalent parents
groupOfSimilarRelationsForNounParent = ['pos', 'nn', 'prep_of', 'prep_in', 'prep_at', 'prep_for']
group1OfSimilarRelationsForVerbParent = ['agent', 'nsubj', 'xsubj']
group2OfSimilarRelationsForVerbParent = ['ccomp', 'dobj', 'nsubjpass', 'rel', 'partmod']
group3OfSimilarRelationsForVerbParent = ['tmod' 'prep_in', 'prep_at', 'prep_on']
group4OfSimilarRelationsForVerbParent = ['iobj', 'prep_to']
for ktem in sourceWordParents:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+nounAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsForNounParent and ltem[2] in groupOfSimilarRelationsForNounParent) or
(ktem[2] in group1OfSimilarRelationsForVerbParent and ltem[2] in group1OfSimilarRelationsForVerbParent) or
(ktem[2] in group2OfSimilarRelationsForVerbParent and ltem[2] in group2OfSimilarRelationsForVerbParent) or
(ktem[2] in group3OfSimilarRelationsForVerbParent and ltem[2] in group3OfSimilarRelationsForVerbParent) or
(ktem[2] in group4OfSimilarRelationsForVerbParent and ltem[2] in group4OfSimilarRelationsForVerbParent)):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for common or equivalent children
groupOfSimilarRelationsForNounChild = ['pos', 'nn' 'prep_of', 'prep_in', 'prep_at', 'prep_for']
groupOfSimilarRelationsForVerbChild = ['infmod', 'partmod', 'rcmod']
groupOfSimilarRelationsForAdjectiveChild = ['amod', 'rcmod']
for ktem in sourceWordChildren:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+nounAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsForNounChild and ltem[2] in groupOfSimilarRelationsForNounChild) or
(ktem[2] in groupOfSimilarRelationsForVerbChild and ltem[2] in groupOfSimilarRelationsForVerbChild) or
(ktem[2] in groupOfSimilarRelationsForAdjectiveChild and ltem[2] in groupOfSimilarRelationsForAdjectiveChild)):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent parent-child relations
groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild = [['nsubj'], ['amod', 'rcmod']]
groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['ccomp', 'dobj', 'nsubjpass', 'rel', 'partmod'], ['infmod', 'partmod', 'rcmod']]
group1OfSimilarRelationsInOppositeDirectionForNounParentAndChild = [['conj_and'], ['conj_and']]
group2OfSimilarRelationsInOppositeDirectionForNounParentAndChild = [['conj_or'], ['conj_or']]
group3OfSimilarRelationsInOppositeDirectionForNounParentAndChild = [['conj_nor'], ['conj_nor']]
for ktem in sourceWordParents:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+nounAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent child-parent relations
for ktem in sourceWordChildren:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+nounAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForNounParentAndChild[1] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForNounParentAndChild[0])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# now use the collected stats to align
for n in xrange(numberOfNounsInSource):
maxEvidenceCountForCurrentPass = 0
maxOverallValueForCurrentPass = 0
indexPairWithStrongestTieForCurrentPass = [-1, -1]
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'n' or sourceLemmas[i-1] in stopwords:
continue
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'n' or targetLemmas[j-1] in stopwords:
continue
if (i, j) in evidenceCountsMatrix and theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]>maxOverallValueForCurrentPass:
maxOverallValueForCurrentPass = theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]
maxEvidenceCountForCurrentPass = evidenceCountsMatrix[(i, j)]
indexPairWithStrongestTieForCurrentPass = [i, j]
if maxEvidenceCountForCurrentPass > 0:
nounAlignments.append(indexPairWithStrongestTieForCurrentPass)
sourceWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[0])
targetWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[1])
for item in relativeAlignmentsMatrix[(indexPairWithStrongestTieForCurrentPass[0], indexPairWithStrongestTieForCurrentPass[1])]:
if item[0]<>0 and item[1]<>0 and item[0] not in sourceWordIndicesAlreadyAligned and item[1] not in targetWordIndicesAlreadyAligned:
nounAlignments.append(item)
sourceWordIndicesAlreadyAligned.append(item[0])
targetWordIndicesAlreadyAligned.append(item[1])
else:
break
return nounAlignments
##############################################################################################################################
##############################################################################################################################
def alignMainVerbs(source, target, sourceParseResult, targetParseResult, existingAlignments):
# source and target:: each is a list of elements of the form:
# [[character begin offset, character end offset], word index, word, lemma, pos tag]
global ppdbSim
global theta1
mainVerbAlignments = []
sourceWordIndices = [i+1 for i in xrange(len(source))]
targetWordIndices = [i+1 for i in xrange(len(target))]
sourceWordIndicesAlreadyAligned = sorted(list(set([item[0] for item in existingAlignments])))
targetWordIndicesAlreadyAligned = sorted(list(set([item[1] for item in existingAlignments])))
sourceWords = [item[2] for item in source]
targetWords = [item[2] for item in target]
sourceLemmas = [item[3] for item in source]
targetLemmas = [item[3] for item in target]
sourcePosTags = [item[4] for item in source]
targetPosTags = [item[4] for item in target]
sourceDParse = dependencyParseAndPutOffsets(sourceParseResult)
targetDParse = dependencyParseAndPutOffsets(targetParseResult)
numberOfMainVerbsInSource = 0
evidenceCountsMatrix = {}
relativeAlignmentsMatrix = {}
wordSimilarities = {}
# construct the two matrices in the following loop
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'v' or sourceLemmas[i-1] in stopwords:
continue
numberOfMainVerbsInSource += 1
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'v' or targetLemmas[j-1] in stopwords:
continue
if max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))<ppdbSim:
continue
wordSimilarities[(i, j)] = max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))
sourceWordParents = findParents(sourceDParse, i, sourceWords[i-1])
sourceWordChildren = findChildren(sourceDParse, i, sourceWords[i-1])
targetWordParents = findParents(targetDParse, j, targetWords[j-1])
targetWordChildren = findChildren(targetDParse, j, targetWords[j-1])
# search for common or equivalent children
group1OfSimilarRelationsForNounChild = ['agent', 'nsubj' 'xsubj']
group2OfSimilarRelationsForNounChild = ['ccomp', 'dobj' 'nsubjpass', 'rel', 'partmod']
group3OfSimilarRelationsForNounChild = ['tmod', 'prep_in', 'prep_at', 'prep_on']
group4OfSimilarRelationsForNounChild = ['iobj', 'prep_to']
groupOfSimilarRelationsForVerbChild = ['purpcl', 'xcomp']
for ktem in sourceWordChildren:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+mainVerbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in group1OfSimilarRelationsForNounChild and ltem[2] in group1OfSimilarRelationsForNounChild) or
(ktem[2] in group2OfSimilarRelationsForNounChild and ltem[2] in group2OfSimilarRelationsForNounChild) or
(ktem[2] in group3OfSimilarRelationsForNounChild and ltem[2] in group3OfSimilarRelationsForNounChild) or
(ktem[2] in group4OfSimilarRelationsForNounChild and ltem[2] in group4OfSimilarRelationsForNounChild) or
(ktem[2] in groupOfSimilarRelationsForVerbChild and ltem[2] in groupOfSimilarRelationsForVerbChild)):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for common or equivalent parents
groupOfSimilarRelationsForNounParent = ['infmod', 'partmod', 'rcmod']
groupOfSimilarRelationsForVerbParent = ['purpcl', 'xcomp']
for ktem in sourceWordParents:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+mainVerbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsForNounParent and ltem[2] in groupOfSimilarRelationsForNounParent) or
(ktem[2] in groupOfSimilarRelationsForVerbParent and ltem[2] in groupOfSimilarRelationsForVerbParent)):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent parent-child pairs
groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild = [['cop', 'csubj'], ['acomp']]
group1OfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['csubj'], ['csubjpass']]
group2OfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['conj_and'], ['conj_and']]
group3OfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['conj_or'], ['conj_or']]
group4OfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['conj_nor'], ['conj_nor']]
for ktem in sourceWordParents:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+mainVerbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1]) or
(ktem[2] in group4OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in group4OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent child-parent pairs
for ktem in sourceWordChildren:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+mainVerbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0]) or
(ktem[2] in group4OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in group4OfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# now use the collected stats to align
for n in xrange(numberOfMainVerbsInSource):
maxEvidenceCountForCurrentPass = 0
maxOverallValueForCurrentPass = 0
indexPairWithStrongestTieForCurrentPass = [-1, -1]
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'v' or sourceLemmas[i-1] in stopwords:
continue
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'v' or targetLemmas[j-1] in stopwords:
continue
if (i, j) in evidenceCountsMatrix and theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]>maxOverallValueForCurrentPass:
maxOverallValueForCurrentPass = theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]
maxEvidenceCountForCurrentPass = evidenceCountsMatrix[(i, j)]
indexPairWithStrongestTieForCurrentPass = [i, j]
if maxEvidenceCountForCurrentPass > 0:
mainVerbAlignments.append(indexPairWithStrongestTieForCurrentPass)
sourceWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[0])
targetWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[1])
for item in relativeAlignmentsMatrix[(indexPairWithStrongestTieForCurrentPass[0], indexPairWithStrongestTieForCurrentPass[1])]:
if item[0]<>0 and item[1]<>0 and item[0] not in sourceWordIndicesAlreadyAligned and item[1] not in targetWordIndicesAlreadyAligned:
mainVerbAlignments.append(item)
sourceWordIndicesAlreadyAligned.append(item[0])
targetWordIndicesAlreadyAligned.append(item[1])
else:
break
return mainVerbAlignments
##############################################################################################################################
##############################################################################################################################
def alignAdjectives(source, target, sourceParseResult, targetParseResult, existingAlignments):
# source and target:: each is a list of elements of the form:
# [[character begin offset, character end offset], word index, word, lemma, pos tag]
global ppdbSim
global theta1
adjectiveAlignments = []
sourceWordIndices = [i+1 for i in xrange(len(source))]
targetWordIndices = [i+1 for i in xrange(len(target))]
sourceWordIndicesAlreadyAligned = sorted(list(set([item[0] for item in existingAlignments])))
targetWordIndicesAlreadyAligned = sorted(list(set([item[1] for item in existingAlignments])))
sourceWords = [item[2] for item in source]
targetWords = [item[2] for item in target]
sourceLemmas = [item[3] for item in source]
targetLemmas = [item[3] for item in target]
sourcePosTags = [item[4] for item in source]
targetPosTags = [item[4] for item in target]
sourceDParse = dependencyParseAndPutOffsets(sourceParseResult)
targetDParse = dependencyParseAndPutOffsets(targetParseResult)
numberOfAdjectivesInSource = 0
evidenceCountsMatrix = {}
relativeAlignmentsMatrix = {}
wordSimilarities = {}
# construct the two matrices in the following loop
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'j':
continue
numberOfAdjectivesInSource += 1
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'j':
continue
if max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))<ppdbSim:
continue
wordSimilarities[(i, j)] = max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))
sourceWordParents = findParents(sourceDParse, i, sourceWords[i-1])
sourceWordChildren = findChildren(sourceDParse, i, sourceWords[i-1])
targetWordParents = findParents(targetDParse, j, targetWords[j-1])
targetWordChildren = findChildren(targetDParse, j, targetWords[j-1])
# search for common or equivalent parents
groupOfSimilarRelationsForNounParent = ['amod', 'rcmod']
for ktem in sourceWordParents:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+adjectiveAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and ((ktem[2]==ltem[2]) or (ktem[2] in groupOfSimilarRelationsForNounParent and ltem[2] in groupOfSimilarRelationsForNounParent)):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for common children
for ktem in sourceWordChildren:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+adjectiveAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (ktem[2]==ltem[2]):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent parent-child pair
groupOfSimilarRelationsInOppositeDirectionForNounParentAndChild = [['amod', 'rcmod'], ['nsubj']]
groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild = [['acomp'], ['cop', 'csubj']]
group1OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild = [['conj_and'], ['conj_and']]
group2OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild = [['conj_or'], ['conj_or']]
group3OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild = [['conj_nor'], ['conj_nor']]
for ktem in sourceWordParents:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+adjectiveAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForNounParentAndChild[0] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForNounParentAndChild[1]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent child-parent pair
for ktem in sourceWordChildren:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+adjectiveAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForNounParentAndChild[1] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForNounParentAndChild[0]) or
(ktem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[1] and ltem[2] in groupOfSimilarRelationsInOppositeDirectionForVerbParentAndChild[0]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[1] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForAdjectiveParentAndChild[0])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# now use the collected stats to align
for n in xrange(numberOfAdjectivesInSource):
maxEvidenceCountForCurrentPass = 0
maxOverallValueForCurrentPass = 0
indexPairWithStrongestTieForCurrentPass = [-1, -1]
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'j' or sourceLemmas[i-1] in stopwords:
continue
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'j' or targetLemmas[j-1] in stopwords:
continue
if (i, j) in evidenceCountsMatrix and theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]>maxOverallValueForCurrentPass:
maxOverallValueForCurrentPass = theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]
maxEvidenceCountForCurrentPass = evidenceCountsMatrix[(i, j)]
indexPairWithStrongestTieForCurrentPass = [i, j]
if maxEvidenceCountForCurrentPass > 0:
adjectiveAlignments.append(indexPairWithStrongestTieForCurrentPass)
sourceWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[0])
targetWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[1])
for item in relativeAlignmentsMatrix[(indexPairWithStrongestTieForCurrentPass[0], indexPairWithStrongestTieForCurrentPass[1])]:
if item[0]<>0 and item[1]<>0 and item[0] not in sourceWordIndicesAlreadyAligned and item[1] not in targetWordIndicesAlreadyAligned:
adjectiveAlignments.append(item)
sourceWordIndicesAlreadyAligned.append(item[0])
targetWordIndicesAlreadyAligned.append(item[1])
else:
break
return adjectiveAlignments
##############################################################################################################################
##############################################################################################################################
def alignAdverbs(source, target, sourceParseResult, targetParseResult, existingAlignments):
# source and target:: each is a list of elements of the form:
# [[character begin offset, character end offset], word index, word, lemma, pos tag]
global ppdbSim
global theta1
adverbAlignments = []
sourceWordIndices = [i+1 for i in xrange(len(source))]
targetWordIndices = [i+1 for i in xrange(len(target))]
sourceWordIndicesAlreadyAligned = sorted(list(set([item[0] for item in existingAlignments])))
targetWordIndicesAlreadyAligned = sorted(list(set([item[1] for item in existingAlignments])))
sourceWords = [item[2] for item in source]
targetWords = [item[2] for item in target]
sourceLemmas = [item[3] for item in source]
targetLemmas = [item[3] for item in target]
sourcePosTags = [item[4] for item in source]
targetPosTags = [item[4] for item in target]
sourceDParse = dependencyParseAndPutOffsets(sourceParseResult)
targetDParse = dependencyParseAndPutOffsets(targetParseResult)
numberOfAdverbsInSource = 0
evidenceCountsMatrix = {}
relativeAlignmentsMatrix = {}
wordSimilarities = {}
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or (sourcePosTags[i-1][0].lower() <> 'r'):
continue
numberOfAdverbsInSource += 1
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or (targetPosTags[j-1][0].lower() <> 'r'):
continue
if max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))<ppdbSim:
continue
wordSimilarities[(i, j)] = max(wordRelatedness(sourceWords[i-1], sourcePosTags[i-1], targetWords[j-1], targetPosTags[j-1]), wordRelatedness(sourceLemmas[i-1], sourcePosTags[i-1], targetLemmas[j-1], targetPosTags[j-1]))
sourceWordParents = findParents(sourceDParse, i, sourceWords[i-1])
sourceWordChildren = findChildren(sourceDParse, i, sourceWords[i-1])
targetWordParents = findParents(targetDParse, j, targetWords[j-1])
targetWordChildren = findChildren(targetDParse, j, targetWords[j-1])
# search for common parents
for ktem in sourceWordParents:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+adverbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (ktem[2]==ltem[2]):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for common children
for ktem in sourceWordChildren:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+adverbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (ktem[2]==ltem[2]):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent parent-child relationships
group1OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild = [['conj_and'], ['conj_and']]
group2OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild = [['conj_or'], ['conj_or']]
group3OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild = [['conj_nor'], ['conj_nor']]
for ktem in sourceWordParents:
for ltem in targetWordChildren:
if ((ktem[0], ltem[0]) in existingAlignments+adverbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# search for equivalent child-parent relationships
for ktem in sourceWordChildren:
for ltem in targetWordParents:
if ((ktem[0], ltem[0]) in existingAlignments+adverbAlignments or max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))>=ppdbSim) and (
(ktem[2]==ltem[2]) or
(ktem[2] in group1OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1] and ltem[2] in group1OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0]) or
(ktem[2] in group2OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1] and ltem[2] in group2OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0]) or
(ktem[2] in group3OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[1] and ltem[2] in group3OfSimilarRelationsInOppositeDirectionForAdverbParentAndChild[0])):
if (i, j) in evidenceCountsMatrix:
evidenceCountsMatrix[(i, j)] += max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
else:
evidenceCountsMatrix[(i, j)] = max(wordRelatedness(ktem[1], sourcePosTags[ktem[0]-1], ltem[1], targetPosTags[ltem[0]-1]), wordRelatedness(sourceLemmas[ktem[0]-1], sourcePosTags[ktem[0]-1], targetLemmas[ltem[0]-1], targetPosTags[ltem[0]-1]))
if (i, j) in relativeAlignmentsMatrix:
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
else:
relativeAlignmentsMatrix[(i, j)] = []
relativeAlignmentsMatrix[(i, j)].append([ktem[0], ltem[0]])
# now use the collected stats to align
for n in xrange(numberOfAdverbsInSource):
maxEvidenceCountForCurrentPass = 0
maxOverallValueForCurrentPass = 0
indexPairWithStrongestTieForCurrentPass = [-1, -1]
for i in sourceWordIndices:
if i in sourceWordIndicesAlreadyAligned or sourcePosTags[i-1][0].lower() <> 'r' or sourceLemmas[i-1] in stopwords:
continue
for j in targetWordIndices:
if j in targetWordIndicesAlreadyAligned or targetPosTags[j-1][0].lower() <> 'r' or targetLemmas[j-1] in stopwords:
continue
if (i, j) in evidenceCountsMatrix and theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]>maxOverallValueForCurrentPass:
maxOverallValueForCurrentPass = theta1*wordSimilarities[(i, j)]+(1-theta1)*evidenceCountsMatrix[(i, j)]
maxEvidenceCountForCurrentPass = evidenceCountsMatrix[(i, j)]
indexPairWithStrongestTieForCurrentPass = [i, j]
if maxEvidenceCountForCurrentPass > 0:
adverbAlignments.append(indexPairWithStrongestTieForCurrentPass)
sourceWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[0])
targetWordIndicesAlreadyAligned.append(indexPairWithStrongestTieForCurrentPass[1])
for item in relativeAlignmentsMatrix[(indexPairWithStrongestTieForCurrentPass[0], indexPairWithStrongestTieForCurrentPass[1])]:
if item[0]<>0 and item[1]<>0 and item[0] not in sourceWordIndicesAlreadyAligned and item[1] not in targetWordIndicesAlreadyAligned:
adverbAlignments.append(item)
sourceWordIndicesAlreadyAligned.append(item[0])
targetWordIndicesAlreadyAligned.append(item[1])
else:
break
return adverbAlignments
##############################################################################################################################
##############################################################################################################################
def alignNamedEntities(source, target, sourceParseResult, targetParseResult, existingAlignments):
# source and target:: each is a list of elements of the form:
# [[character begin offset, character end offset], word index, word, lemma, pos tag]
global punctuations
alignments = []
sourceNamedEntities = ner(sourceParseResult)
sourceNamedEntities = sorted(sourceNamedEntities, key=len)
targetNamedEntities = ner(targetParseResult)
targetNamedEntities = sorted(targetNamedEntities, key=len)
# learn from the other sentence that a certain word/phrase is a named entity (learn for source from target)
for item in source:
alreadyIncluded = False
for jtem in sourceNamedEntities:
if item[1] in jtem[1]:
alreadyIncluded = True
break
if alreadyIncluded or (len(item[2]) >0 and not item[2][0].isupper()):
continue
for jtem in targetNamedEntities:
if item[2] in jtem[2]:
# construct the item
newItem = [[item[0]], [item[1]], [item[2]], jtem[3]]
# check if the current item is part of a named entity part of which has already been added (by checking contiguousness)
partOfABiggerName = False
for k in xrange(len(sourceNamedEntities)):
if sourceNamedEntities[k][1][len(sourceNamedEntities[k][1])-1] == newItem[1][0] - 1:
sourceNamedEntities[k][0].append(newItem[0][0])
sourceNamedEntities[k][1].append(newItem[1][0])
sourceNamedEntities[k][2].append(newItem[2][0])
partOfABiggerName = True
if not partOfABiggerName:
sourceNamedEntities.append(newItem)
elif isAcronym(item[2], jtem[2]) and [[item[0]], [item[1]], [item[2]], jtem[3]] not in sourceNamedEntities:
sourceNamedEntities.append([[item[0]], [item[1]], [item[2]], jtem[3]])
# learn from the other sentence that a certain word/phrase is a named entity (learn for target from source)
for item in target:
alreadyIncluded = False
for jtem in targetNamedEntities:
if item[1] in jtem[1]:
alreadyIncluded = True
break
if alreadyIncluded or (len(item[2]) >0 and not item[2][0].isupper()):
continue
for jtem in sourceNamedEntities:
if item[2] in jtem[2]:
# construct the item
newItem = [[item[0]], [item[1]], [item[2]], jtem[3]]
# check if the current item is part of a named entity part of which has already been added (by checking contiguousness)
partOfABiggerName = False
for k in xrange(len(targetNamedEntities)):
if targetNamedEntities[k][1][len(targetNamedEntities[k][1])-1] == newItem[1][0] - 1:
targetNamedEntities[k][0].append(newItem[0][0])
targetNamedEntities[k][1].append(newItem[1][0])
targetNamedEntities[k][2].append(newItem[2][0])
partOfABiggerName = True
if not partOfABiggerName:
targetNamedEntities.append(newItem)
elif isAcronym(item[2], jtem[2]) and [[item[0]], [item[1]], [item[2]], jtem[3]] not in targetNamedEntities:
targetNamedEntities.append([[item[0]], [item[1]], [item[2]], jtem[3]])
sourceWords = []
targetWords = []
for item in sourceNamedEntities:
for jtem in item[1]:
if item[3] in ['PERSON', 'ORGANIZATION', 'LOCATION']:
sourceWords.append(source[jtem-1][2])
for item in targetNamedEntities:
for jtem in item[1]:
if item[3] in ['PERSON', 'ORGANIZATION', 'LOCATION']:
targetWords.append(target[jtem-1][2])
if len(sourceNamedEntities) == 0 or len(targetNamedEntities) == 0:
return []
sourceNamedEntitiesAlreadyAligned = []
targetNamedEntitiesAlreadyAligned = []
# align all full matches
for item in sourceNamedEntities:
if item[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION']:
continue
# do not align if the current source entity is present more than once
count = 0
for ktem in sourceNamedEntities:
if ktem[2] == item[2]:
count += 1
if count > 1:
continue
for jtem in targetNamedEntities:
if jtem[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION']:
continue
# do not align if the current target entity is present more than once
count = 0
for ktem in targetNamedEntities:
if ktem[2] == jtem[2]:
count += 1
if count > 1:
continue
# get rid of dots and hyphens
canonicalItemWord = [i.replace('.', '') for i in item[2]]
canonicalItemWord = [i.replace('-', '') for i in item[2]]
canonicalJtemWord = [j.replace('.', '') for j in jtem[2]]
canonicalJtemWord = [j.replace('-', '') for j in jtem[2]]
if canonicalItemWord == canonicalJtemWord:
for k in xrange(len(item[1])):
if ([item[1][k], jtem[1][k]]) not in alignments:
alignments.append([item[1][k], jtem[1][k]])
sourceNamedEntitiesAlreadyAligned.append(item)
targetNamedEntitiesAlreadyAligned.append(jtem)
# align acronyms with their elaborations
for item in sourceNamedEntities:
if item[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION']:
continue
for jtem in targetNamedEntities:
if jtem[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION']:
continue
if len(item[2])==1 and isAcronym(item[2][0], jtem[2]):
for i in xrange(len(jtem[1])):
if [item[1][0], jtem[1][i]] not in alignments:
alignments.append([item[1][0], jtem[1][i]])
sourceNamedEntitiesAlreadyAligned.append(item[1][0])
targetNamedEntitiesAlreadyAligned.append(jtem[1][i])
elif len(jtem[2])==1 and isAcronym(jtem[2][0], item[2]):
for i in xrange(len(item[1])):
if [item[1][i], jtem[1][0]] not in alignments:
alignments.append([item[1][i], jtem[1][0]])
sourceNamedEntitiesAlreadyAligned.append(item[1][i])
targetNamedEntitiesAlreadyAligned.append(jtem[1][0])
# align subset matches
for item in sourceNamedEntities:
if item[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION'] or item in sourceNamedEntitiesAlreadyAligned:
continue
# do not align if the current source entity is present more than once
count = 0
for ktem in sourceNamedEntities:
if ktem[2] == item[2]:
count += 1
if count > 1:
continue
for jtem in targetNamedEntities:
if jtem[3] not in ['PERSON', 'ORGANIZATION', 'LOCATION'] or jtem in targetNamedEntitiesAlreadyAligned:
continue
if item[3] <> jtem[3]:
continue
# do not align if the current target entity is present more than once
count = 0
for ktem in targetNamedEntities:
if ktem[2] == jtem[2]:
count += 1
if count > 1:
continue
# find if the first is a part of the second
if isSublist(item[2], jtem[2]):