/
lfgalign.lisp
1861 lines (1671 loc) · 66.9 KB
/
lfgalign.lisp
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
;;; quickstart:
; (swank:operate-on-system-for-emacs "lfgalign" (quote load-op)) (swank:set-package "LFGALIGN") (lisp-unit:run-tests)
;;; Tell SBCL we want full debugging info (eg. no function inlining),
;;; but don't care about speed:
(declaim (optimize (speed 0) (safety 3) (debug 3)))
(in-package #:lfgalign)
(define-condition several-topnodes (unexpected-input) ()
(:report (lambda (condition stream)
(format stream "Found superfluous topmost nodes: ~A" (text condition)))))
(defvar *no-warnings* t)
(defvar *debug* nil)
(defvar *pro-affects-c-linking* nil) ; Whether unlinked pro-elements may hinder linking c-structure nodes of two predicates
(defvar *arg-order-smoothing* 0.01) ; Added to numerator and denominator in arg-order-rate
(defvar *sub-f-smoothing* 0.01) ; Added to numerator and denominator in sub-f-rate
(defvar *lpt-smoothing* 0.00) ; Added to numerator and denominator in lpt-rate
(defvar *max-adjs* 8) ; maximum amount of adjs to try to match in `adjalign'
(defvar *include-unreferenced-preds* t) ; Whether we add all unreferenced PRED f-structures to adjuncts of SENTENCE (with f-structure id -1)
(defvar *include-spec* nil) ; Whether we add SPEC elements to the set of adjuncts
;;;;;;;; C-STRUCTURE TREE:
;;;;;;;; -----------------
(defun terminal? (tree)
"Return true if `tree' is a terminal node."
(and (numberp (first tree))
(stringp (second tree))
(listp (third tree))
(not (fourth tree))))
(defun maketree (tab)
"Returns a binary tree created from the |subtree| and |terminal|
alists of the table `tab'. The second value returned contains the
back-references from each branch ID. Does not modify the input
table. Efficiency: 100 real-life trees takes about 0.5 seconds on a
laptop, should be OK."
(when *debug* (loop for tr in (gethash '|terminal| tab)
when (not (terminal? tr)) do (error "terminal? failed on ~A" tr)))
(let* ((subtree (copy-tree (gethash '|subtree| tab)))
(tree (append subtree
(copy-tree (gethash '|terminal| tab))))
;; subtree elts are of the form (id name left-id right-id)
(refs (mapcan-true (lambda (b)
(list (aif (third b) (list it (first b) 'left))
(aif (fourth b) (list it (first b) 'right))))
subtree)))
(loop
for branch in tree
for ref = (assoc (car branch) refs)
when ref do
(case (third ref)
(left (setf (third (assoc (second ref) tree)) branch))
(right (setf (fourth (assoc (second ref) tree)) branch)))
else
collect branch into newtree
finally
(aif (cdr newtree)
(error 'several-topnodes :text it)
(return (values (car newtree) refs))))))
(defun maketrim (tree)
(cons 'snip (car tree)))
(defun trim? (tree)
(not (listp (cdr tree))))
(defun trimtree (c-ids tree)
"Trim off the branches of the tree that aren't in `c-ids'.
Where we've chopped off branches, we get a pair where the cdr is a
c-id number referring to what used to be there."
(when tree
(if (cddddr tree) (error "Non-binary tree!") t) ; none of these in my test-set
(or (and (listp tree)
(member (car tree) c-ids) ; in-domain
(if (terminal? tree)
tree
(list (first tree)
(second tree)
(trimtree c-ids (third tree))
(trimtree c-ids (fourth tree)))))
;; out-of-domain:
(maketrim tree))))
(defun f-tag-tree (node tab)
"Debug function, just conses the phi(node) onto the node and
any non-terminal child under it."
(when node
(if (terminal? node)
node
(list (phi (first node) tab)
(first node)
(second node)
(f-tag-tree (third node) tab)
(f-tag-tree (fourth node) tab)))))
(defun treefind (c-id tree)
"Just return the subtree of `tree' starting with `c-id'."
(if (and tree (listp tree))
(if (eq (car tree) c-id)
tree
(or (treefind c-id (third tree))
(treefind c-id (fourth tree))))))
(defun topnodes (c-ids tree)
"`c-ids' (given by `phi^-1') describes a functional domain, find the
topmost of the nodes in the c-structure which project this
domain (member of `c-ids').
Since we may have discontiguous domains, returns a list.
Unfortunately, id's aren't sorted in any smart way :-/"
(when (and tree (listp tree))
(if (member (car tree) c-ids)
(list tree)
(let ((Ltree (topnodes c-ids (third tree)))
(Rtree (topnodes c-ids (fourth tree))))
(cond ((and Ltree Rtree)
(append Ltree Rtree))
;; Not in both, but try either left or right:
(Ltree Ltree)
(Rtree Rtree)
;; Not in either: return nil
)))))
(defun pred-tag-alignment (f-alignment tab_s tab_t)
"Given an `f-alignment' from `f-align', exchange var numbers for the
names in the pred values. Also handles output from `flatten'/`rank'."
(when f-alignment
(if (f-link? f-alignment)
(cons (second (get-pred (car f-alignment) tab_s))
(second (get-pred (cdr f-alignment) tab_t)))
(cons (pred-tag-alignment (car f-alignment) tab_s tab_t)
(if (f-link? (first (cdr f-alignment)))
(mapcar (lambda (sub) ; flat
(pred-tag-alignment sub tab_s tab_t))
(cdr f-alignment))
(mapcar (lambda (branch) ; branching
(mapcar (lambda (sub)
(pred-tag-alignment sub tab_s tab_t))
branch))
(cdr f-alignment)))))))
(defun eq-phi (c-id1 c-id2 tab)
"`c-id1' and `c-id2' are two c-structure id-s in `tab'.
Return `nil' iff the phi of these are non-equal."
(intersection (get-equivs (phi c-id1 tab) tab)
(get-equivs (phi c-id2 tab) tab)))
(defun phi (c-id tab)
"Returns the f-var given by phi of `c-id', use (gethash f-var `tab')
to find the content, but the f-var might have equivs..."
(cdr (assoc c-id (gethash '|phi| tab))))
(defun phi^-1 (f-var tab)
"The inverse phi (c-structure id's that map to `f-var' in
`tab'). Includes c-ids that map to variables that are equivalent to
`f-var'."
(let ((f-vars (adjoin f-var
(get-equivs f-var tab))))
(mapcar-true #'car
(remove-if (lambda (phi)
(not (member (cdr phi) f-vars)))
(gethash '|phi| tab)))))
(defun skip-suff_base (tree)
"Trees are either nil, a fourtuple, or a cons cell where the cdr is
a number (pointing to where the trimmed tree was cut off)."
(when tree
(if (or (trim? tree) (terminal? tree))
tree
(if (and (not (trim? (fourth tree)))
(search "SUFF_BASE" (second (fourth tree))))
(skip-SUFF_BASE (third tree))
(list (first tree) (second tree)
(skip-suff_base (third tree))
(skip-suff_base (fourth tree)))))))
(defun pretty-topnode (f-var tab tree)
"Skip the more boring nodes."
(mapcar #'skip-suff_base
(topnodes (phi^-1 f-var tab) tree)))
;;;;;;;; VARIOUS HELPERS:
;;;;;;;; ----------------
(defun null-pred? (Pr)
(equal Pr "NULL"))
(defun get-equivs (val tab &optional include-this)
(let ((equivs
(dset3-findall val (gethash '|eq-sets| tab))))
(if include-this
(adjoin val equivs)
equivs)))
(defun eq-f (f-id1 f-id2 tab)
"`f-id1' and `f-id2' are two f-structure id-s in `tab'.
Return `nil' iff non-equal."
(intersection (get-equivs f-id1 tab)
(get-equivs f-id2 tab)))
(defun unravel-helper (att stack seen tab)
"Call with empty `seen' and a `stack' containing the variable you
want to unravel the attribute `att' of. This function makes sure to
add all equivalent variables and their possible expansions."
(when stack
(let ((x (pop stack)))
(pushnew x seen)
(if (numberp x)
(progn (awhen (assoc att (gethash x tab) :test #'equal) ; add rhs
(pushnew (cdr it) stack))
(mapcar (lambda (equiv) ; add all eq variables
(unless (member equiv seen)
(pushnew equiv stack)))
(get-equivs x tab))
(unravel-helper att stack seen tab))
(cons x (unravel-helper att stack seen tab))))))
(defun pred-equal (Pr1 Pr2 tab)
"Apparently there was good reason to make pred-equality so
complicated. Why were eq-vars not enough? TODO remember."
(labels ((args-equal (args1 args2)
(loop for arg1 in args1
for arg2 in args2
always (or (and (null-pred? arg1) (null-pred? arg2))
(eq-f arg1 arg2 tab)))))
(and (equal (first Pr1) (first Pr2))
(eq (second Pr1) (second Pr2))
(args-equal (third Pr1) (third Pr2))
(args-equal (fourth Pr1) (fourth Pr2)))))
(defun unravel (att val tab)
(awhen (remove-duplicates (unravel-helper att (list val) nil tab)
:test (lambda (a b) (pred-equal a b tab)))
(when (cdr it)
(error "Found superfluous, non-equal unravellings of ~A ~A:~%~A~%" val att it))
(cons att (car it))))
(defun semform-pos (var tab)
"According to
http://www2.parc.com/isl/groups/nltt/xle/doc/xle.html#Prolog_Output
`The [semform] identifiers are ordered based on the string position of
the places where the semantic forms were instantiated.'
Thus we can treat this as the SENTENCE POSITION. However, if there is
no semform id (i.e. the pred was a 'fake' one, see `get-pred'), we
return -1."
(let ((Pr (get-pred var tab)))
(aif (third Pr)
it
-1)))
(defun coord-to-pred (var lemma tab)
"Turn a coordination into a fake pred like
`and<>conjunct1,conjunct2,...'
TODO: order conjuncts by appearance in the sentence."
(list var
lemma
nil ; semform_data?
nil ; inside args
(sort ; outside args
(mapcar-true
;; For coordinations, the f-structure id, here var, is the set id:
(lambda (setpair) (when (eq (cdr setpair) var)
(skip-pp (car setpair) tab)))
(cdr (gethash '|in_set| tab)))
#'<
:key (lambda (v) (semform-pos v tab)))))
(defun outer-pred (tab)
"The -1 pred is lfgalign-internal, we use it to collect all the
`unreferenced-preds' which we can try aligning, since they don't
appear in as arguments/adjuncts of other preds.
The unreferenced pred's are put into the adjunct list, see
`get-adjs'."
(list -1 "_SENTENCE"
nil
(when (get-pred 0 tab 'nil-on-none) (list 0))
nil))
(defun get-pred (var tab &optional nil-on-none)
"Use `nil-on-none' to return nil if no PRED was found,
otherwise a fake one is made up for PRED-less variables."
(if (null-pred? var)
var
(if (eq -1 var)
(outer-pred tab)
(aif (unravel "PRED" var tab)
(cons var (cdr it))
;; No PRED-value, perhaps make one up...
(aif (unravel "COORD-FORM" var tab)
(coord-to-pred var (cdr it) tab)
(unless nil-on-none
(unless *no-warnings*
(warn "No PRED for var ~A, treating it as a pro~%" var))
(list var "pro" nil nil nil)))))))
(defun predp (Pr)
(and (listp Pr)
(numberp (first Pr))
(stringp (second Pr))
(or (numberp (third Pr)) (null (third Pr)))
(listp (fourth Pr))
(listp (fifth Pr))
(null (sixth Pr))
(eq 5 (length Pr))))
(defun get-args (Pr tab &optional no-nulls)
"The argument `Pr' is either a pred or a variable id that we can
look up to get a pred."
(let ((Pr (if (predp Pr) Pr (get-pred Pr tab))))
(remove-if (lambda (p) (and no-nulls (null-pred? p)))
(mapcar (lambda (var)
(skip-pp var tab))
(union (fourth Pr) (fifth Pr))))))
(defun skip-pp (var tab)
"Skip adpositions, as defined in footnote 3 in
dyvik2009lmp.
TODO: do all prepositions/postpositions have PFORM/CHECK->_POSTP and
OBJ, or are there other ways of representing them? This really ought
to be user-configurable."
(if (and (or (awhen (assoc-equal "CHECK" (gethash var tab))
(assoc-equal "_POSTP" (gethash (cdr it) tab)))
(assoc-equal "PFORM" (gethash var tab)))
(assoc-equal "OBJ" (gethash var tab)))
(cdr (assoc-equal "OBJ" (gethash var tab)))
var))
(defun get-spec (exclude var tab)
(let* ((spec-var (cdr (assoc-equal "SPEC" (gethash var tab))))
(spec-poss-var (cdr (assoc-equal "POSS" (gethash spec-var tab))))
(spec-det-var (cdr (assoc-equal "DET" (gethash spec-var tab)))))
(append
(when (and spec-poss-var
(not (member spec-poss-var exclude)))
(list spec-poss-var))
(when (and spec-det-var
(not (member spec-det-var exclude)))
(list spec-det-var)))))
(defun get-adjs (var tab &optional no-error)
"Use `no-error' to return nil if no ADJUNCT was found.
TODO: find example to test where we need `unravel' / eq-sets.
TODO: Skip prepositions, as defined in footnote 3 in
dyvik2009lmp, with `skip-pp'."
(append
(mapcar
(lambda (v) (skip-pp v tab))
(if (eq -1 var)
(when *include-unreferenced-preds* (remove 0 (unreferenced-preds tab)))
(aif (assoc "ADJUNCT" (gethash var tab) :test #'equal)
(if (get-equivs (cdr it) tab)
(error 'unexpected-input "eqvar of ADJUNCT, TODO")
(mapcar-true (lambda (setpair) (when (eq (cdr setpair) (cdr it))
(car setpair)))
(cdr (gethash '|in_set| tab))))
(unless no-error (error 'no-adjs-error-todo var)))))
(when *include-spec*
(get-spec nil var tab))))
(defun references (parentv childv tab)
"Give a list of attributes of var `parentv' in `tab' which refer to
var `childv'."
(loop
for attval in (gethash parentv tab)
when (eq childv (cdr attval))
collect attval))
(defun lemma (Pr) (second Pr))
(defun pron-form (var tab)
(cdr (assoc-equal "PRON-FORM" (gethash var tab))))
(defun L (Pr tab)
"Return the lexical expression (ie. surfaceform) of PRED `Pr'.
Note: a real, dropped \"pro\" argument will return its verb! A fake
\"pro\" element (created by get-pred for var's that have no PRED
element) will return \"pro\"."
(if (third Pr)
(let* ((semform_id (third Pr))
(semform (assoc semform_id (gethash '|semform_data| tab)))
(preterminal (assoc (second semform)
(gethash '|subtree| tab)))
(terminal (assoc (fourth preterminal)
(gethash '|terminal| tab)))
(surfaceform (assoc (car (third terminal))
(gethash '|surfaceform| tab))))
(or (second surfaceform)
(and (equal "pro" (lemma Pr))
(pron-form (car Pr) tab))))
(second Pr)))
(defun all-pred-vars (tab)
"TODO: cache/memoise in table?"
(let (seen)
(mapcar-true
(lambda (x) (let ((var (car x)))
(when (and (numberp var)
(assoc "PRED" (cdr x) :test #'equal))
(unless (intersection seen (get-equivs var tab))
(pushnew var seen)
var))))
(table-to-alist tab))))
(defun unreferenced-preds (tab)
"Return a list of variables of those PRED's which are not arguments
or adjuncts of others."
(let (vars backrefs)
(mapcar (lambda (var)
(pushnew var vars)
(setq backrefs (union
backrefs
(union
(get-args var tab 'no-nulls)
(get-adjs var tab 'no-error)))))
(mapcar (lambda (v) (skip-pp v tab))
(all-pred-vars tab)))
(remove-if (lambda (v) (intersection (get-equivs v tab 'include-this)
backrefs))
vars)))
;;;;;;;; PRETTY-PRINTING:
;;;;;;;; ----------------
(defun simple-f-str (var tab seen)
"Extract the 'important' features from an f-structure (from `var' and onwards)"
(let* ((Pr (get-pred var tab)))
(if (and (listp Pr) (not (member var seen)))
(remove-if
#'null
(list (first Pr)
(if (equal "pro" (second Pr))
(aif (assoc-equal "PRON-FORM" (gethash var tab))
(if (numberp (cdr it)) ; ugly, but works.. todo prettier
(loop for x in (get-equivs (cdr it) tab) if (stringp x) return x)
(cdr it))
(second Pr))
(second Pr))
(third Pr)
(awhen (append
(mapcar (lambda (v) (simple-f-str v tab (cons var seen)))
(fourth Pr))
(mapcar (lambda (v) (simple-f-str v tab (cons var seen)))
(fifth Pr)))
(cons 'arg it))
(awhen (mapcar (lambda (v) (simple-f-str v tab (cons var seen)))
(get-adjs var tab 'no-error))
(cons 'adj it))))
(list var))))
(defun pretty-simple-f-str (var tab)
(labels
((out-f (fs indent)
(concatenate 'string
(format nil "~A~A[ `~A<~{~A~^, ~}>'"
indent
(first fs)
(second fs)
(mapcar #'car (cdr (assoc 'arg (cdddr fs)))))
(when (fourth fs)
(format nil "~%~A~{~A~}"
indent
(mapcar (lambda (fsa)
(out-f fsa (concatenate 'string indent " ")))
(cdr (fourth fs)))))
(when (fifth fs)
(format nil "~%~A~{~A~}"
indent
(mapcar (lambda (fsa)
(out-f fsa (concatenate 'string indent " ")))
(cdr (fifth fs)))))
(format nil "~A ]~%" indent))
))
(out-f (simple-f-str var tab nil)
"")))
(defun out-two-f-str (tab_s tab_t)
"Pretty-print the f-structures of two tables next to each other"
(loop for src in (split-str-by (pretty-simple-f-str 0 tab_s) #\Newline)
for trg in (split-str-by (pretty-simple-f-str 0 tab_t) #\Newline)
do (out "~A~40T~A~%" src trg)))
;;;;;;;; LPT:
;;;;;;;; ----
(defun make-LPT ()
"Just make an empty LPT pair"
(cons (make-hash-table :test #'equal)
(make-hash-table :test #'equal)))
(defun get-LPT (w_s w_t LPTs)
"For now, LPTs is just a cons of hash-tables, where the first lets
you look up a list of possible `w_t' matches using `w_s' as key, and the
second vice versa. Does not assume all keys in the first table are
values in the second (and vice versa). If neither `w_s' nor `w_t' are in
there, it's a trivial match.
As with hash tables, the second return value tells us whether either
\"key\" was in its table."
(let* ((LPT-s-t (car LPTs))
(LPT-t-s (cdr LPTs))
(tr_s (gethash w_s LPT-s-t))
(tr_t (gethash w_t LPT-t-s)))
(values
(or (member w_t tr_s :test #'equal)
(member w_s tr_t :test #'equal)
(and (null tr_s)
(null tr_t)))
(or tr_s tr_t))))
(defun add-to-LPT (w_s w_t LPTs)
"Add `w_s' and `w_t' to `LPTs' as translations of each other."
(setf (gethash w_s (car LPTs))
(pushnew w_t (gethash w_s (car LPTs)) :test #'equal))
(setf (gethash w_t (cdr LPTs))
(pushnew w_t (gethash w_t (cdr LPTs)) :test #'equal))
LPTs)
(defun noun? (var tab)
"TODO: do all and only nouns have an NTYPE?"
(assoc "NTYPE" (gethash var tab) :test #'equal))
(defun LPT? (src tab_s trg tab_t LPTs)
"Are the lexical expressions of `Pr_s' and `Pr_t',
L(Pr_s) and L(Pr_t), Linguistically Predictable Translations?
True if both are in `get-LPT' as translations (or neither is), or one
is a pro and the other is a noun (see `noun?') or a pro.
TODO: The pro of a verb has that verb as its L, while the pro of a
reflexive has that reflexive... At the moment, we look up the L of
the pro no matter what, but will this give us trouble?"
(let* ((Pr_s (if (numberp src) (get-pred src tab_s) src))
(Pr_t (if (numberp trg) (get-pred trg tab_t) trg))
(LPr_s (L Pr_s tab_s)) (lem_s (lemma Pr_s))
(LPr_t (L Pr_t tab_t)) (lem_t (lemma Pr_t)))
(or (and (equal lem_s "pro")
(equal lem_t "pro"))
(and (equal lem_s "pro")
(noun? (car Pr_t) tab_t))
(and (equal lem_t "pro")
(noun? (car Pr_s) tab_s))
(multiple-value-bind (L-tr L-in) (get-LPT LPr_s LPr_t LPTs)
(multiple-value-bind (lem-tr lem-in) (get-LPT lem_s lem_t LPTs)
(cond ((and L-in lem-in) (or L-tr lem-tr))
(L-in L-tr)
(lem-in lem-tr)
(t (unless *no-warnings*
(warn "Neither ~A/~A nor ~A/~A are in LPTs" LPr_s lem_s LPr_t lem_t))
'trivial)))))))
;;;;;;;; ALIGNMENT:
;;;;;;;; ----------
(defun argalign (link tab_s tab_t)
"Return all possible combinations of links from `args_s'/`adjs_s' to
`args_t'/`adjs_t' that include all members of `args_s' and `args_t',
ie. return all pairs of
(args_s X (args_t U adjs_t)) U ((args_s U adjs_s) X args_t)
s.t. all args are in the set. An arg_s may be linked to a member of
`adjs_t' (and vice versa), but no pairs of adj_s and adj_t are
included.
Return nil if no alignments are possible, and (list nil) if no
alignments are necessary (there are no arguments to align).
See `argalign-p'."
(let* ((var_s (car link))
(var_t (cdr link))
(adjs_s (get-adjs var_s tab_s 'no-error))
(adjs_t (get-adjs var_t tab_t 'no-error))
(args_s (get-args var_s tab_s 'no-nulls))
(args_t (get-args var_t tab_t 'no-nulls)))
(when *debug* (format t "args_s:~A adjs_s:~A args_t:~A adjs_t:~A~%" args_s adjs_s args_t adjs_t))
(argalign-p args_s adjs_s args_t adjs_t)))
(defun margalign (link1 link2 tab_s tab_t)
"Like argalign, but `link1' and `link2' describe a two-to-one link,
so we merge the adjunct/argument lists.
Note: this should work even for a two-to-two link.
Note: we assume here that this is never used to merge adjuncts, thus
we don't set-difference from adjs"
(let* ((var_s1 (car link1))
(var_s2 (car link2))
(var_t1 (cdr link1))
(var_t2 (cdr link2))
(adjs_s (union (get-adjs var_s1 tab_s 'no-error)
(get-adjs var_s2 tab_s 'no-error)))
(adjs_t (union (get-adjs var_t1 tab_t 'no-error)
(get-adjs var_t2 tab_t 'no-error)))
(args_s (set-difference
(union (get-args var_s1 tab_s 'no-nulls)
(get-args var_s2 tab_s 'no-nulls))
(union (get-equivs var_s1 tab_s 'include-this)
(get-equivs var_s2 tab_s 'include-this))))
(args_t (set-difference
(union (get-args var_t1 tab_t 'no-nulls)
(get-args var_t2 tab_t 'no-nulls))
(union (get-equivs var_t1 tab_t 'include-this)
(get-equivs var_t2 tab_t 'include-this)))))
(when *debug* (format t "args_s:~A adjs_s:~A args_t:~A adjs_t:~A~%" args_s adjs_s args_t adjs_t))
(argalign-p args_s adjs_s args_t adjs_t)))
(defun argalign-p (args_s adjs_s args_t adjs_t)
"Helper for `argalign'."
(macrolet
((mapalign (srcs trgs)
"Within one call, we have the same src, but loop through possible `trgs',
the recursion loops through all possible `srcs'."
`(mapcan ; for each target arg/adj
(lambda (trg)
(mapcar-true ; for each permuation w/o src and trg
(lambda (perm)
(cons (cons (car ,srcs) trg)
perm))
;; recurse, removing the arg/adj that we used:
(argalign-p (if (eq ,srcs args_s) (cdr args_s) args_s)
(if (eq ,srcs adjs_s) (cdr adjs_s) adjs_s)
(if (eq ,trgs args_t) (remove trg args_t :count 1) args_t)
(if (eq ,trgs adjs_t) (remove trg adjs_t :count 1) adjs_t))))
,trgs)))
(if args_s
(append (mapalign args_s args_t)
(mapalign args_s adjs_t))
(if args_t ; no args_s
(if adjs_s
(mapalign adjs_s args_t)
;; no adjs_s, fail:
nil)
;; all args_s and args_t used up, make end-of-list:
(list nil)))))
(defun adjalign (exclude link mlink tab_s tab_t)
"Return all possible combinations of links between adjuncts that use
no id's from `exclude' (a list of links).
Return nil if no alignments are possible.
Optional argument `mlink' is a merged link, which together with `link'
describes a two-to-one link, so we merge their adjunct lists; there is
no separate madjalign function."
(let ((adjs_s (if mlink (union (get-adjs (car link) tab_s 'no-error)
(get-adjs (car mlink) tab_s 'no-error))
(get-adjs (car link) tab_s 'no-error)))
(adjs_t (if mlink (union (get-adjs (cdr link) tab_t 'no-error)
(get-adjs (cdr mlink) tab_t 'no-error))
(get-adjs (cdr link) tab_t 'no-error))))
(when *debug* (out "~A~%~A~%" adjs_s adjs_t))
(adjalign-p exclude adjs_s adjs_t)))
(defun adjalign-p (exclude adjs_s adjs_t)
"Helper for `adjalign'.
TODO: Could this leave us with duplicate solutions where we exclude?"
(let ((l_s (length adjs_s))
(l_t (length adjs_t)))
(if (or (> l_s *max-adjs*) (> l_t *max-adjs*))
;; Worst case O(n^2), or a bit less since we reduce both lists
;; concurrently. When SBCL is optimising for speed, this takes
;; 3.1 secs on 9x9, 46 secs on 10x10 (with 95 page faults) on
;; a 2.1 GHz with 3GB real memory.
(progn (unless *no-warnings*
(warn "Adjunct list length >~A, only trying first solution" *max-adjs*))
(list
(loop for src in adjs_s for trg in adjs_t collect (cons src trg))))
(remove-if
#'null
(loop for perm in (if (> l_s l_t)
(adjalign-p-on-src adjs_s adjs_t)
(adjalign-p-on-trg adjs_s adjs_t))
collect
(remove-if (lambda (link) (member-either link exclude))
perm))))))
(defun adjalign-p-on-trg (adjs_s adjs_t) "When trg list is longest"
(when (and adjs_s adjs_t)
(if (cdr adjs_s)
(loop for trg in adjs_t
append (mapcar (lambda (perm) (cons (cons (car adjs_s) trg)
perm))
(adjalign-p-on-trg (cdr adjs_s)
(remove trg adjs_t :count 1))))
(loop for trg in adjs_t
collect (list (cons (car adjs_s) trg))))))
(defun adjalign-p-on-src (adjs_s adjs_t) "When src list is longest"
(when (and adjs_s adjs_t)
(if (cdr adjs_t)
(loop for src in adjs_s
append (mapcar (lambda (perm) (cons (cons src (car adjs_t))
perm))
(adjalign-p-on-src (remove src adjs_s :count 1)
(cdr adjs_t))))
(loop for src in adjs_s
collect (list (cons src (car adjs_t)))))))
(lisp-unit:define-test test-adjalign-p
(lisp-unit:assert-equality
#'set-of-set-equal
'(((B . 2)) ((C . 2)))
(adjalign-p nil '(b c) '(2)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((A . 1)) ((B . 1)) ((C . 1)))
(adjalign-p nil '(a b c) '(1)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((A . 2)) ((B . 2)) ((A . 3)) ((B . 3)) ((A . 2) (B . 3)) ((B . 2) (A . 3)))
(adjalign-p '((c . 1)) '(a b) '(1 2 3)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((A . 1)(B . 2)) ((A . 2)(B . 1))((A . 3)(B . 1))
((A . 1)(B . 3)) ((A . 2)(B . 3))((A . 3)(B . 2)))
(adjalign-p nil '(a b) '(1 2 3)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((A . 2)) ((B . 2)))
(adjalign-p '((c . 1)) '(a b) '(1 2)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((A . 1) (B . 2)) ((A . 2) (B . 1)))
(adjalign-p nil '(a b) '(1 2))))
(lisp-unit:define-test test-both-adjaligns
(lisp-unit:assert-equal
'(((A . 1) (B . 2)) ((A . 1) (B . 3)) ((A . 2) (B . 1)) ((A . 2) (B . 3))
((A . 3) (B . 1)) ((A . 3) (B . 2)))
(adjalign-p-on-trg '(a b) '(1 2 3)))
(lisp-unit:assert-equal
'(((A . 1) (B . 2)) ((A . 1) (C . 2)) ((B . 1) (A . 2)) ((B . 1) (C . 2))
((C . 1) (A . 2)) ((C . 1) (B . 2)))
(adjalign-p-on-src '(a b c) '(1 2))))
(defun make-aligntab () (make-hash-table :test #'equal))
(defun f-align (link tab_s tab_t LPTs &optional aligntab)
"Optional hash table `aligntab' (with :test #'equal) is
destructively modified to store the alignments of all linkings, and lets
you check whether each linking was possible to sub-align.
(i) the number of arguments n and m may or may not differ
is trivially true
(ii), LPT, is now a ranking criterion, see `rank'
argalign covers (iii) and (iv)
TODO: (v) the LPT-correspondences can be aligned one-to-one -- isn't this covered by the way we handle links? Need example..."
(let* ((F_s (car link))
(F_t (cdr link))
(aligntab (or aligntab (make-aligntab)))
alignments)
(labels ((sub-f (perm)
"Try to recursively align links in perm, but keep unaligned links"
(loop for link_a in perm
do (unless (gethash link_a aligntab)
(setf (gethash link_a aligntab)
(f-align link_a tab_s tab_t LPTs aligntab)))
collect (or (gethash link_a aligntab)
(or (unless *no-warnings* (warn "sub-f failed:~A" link_a))
link_a))))
(store (subalignment) (pushnew subalignment alignments :test #'equal))
(argloop (argperms mlink)
"For each permutation of arg/adj links, store link + sub-alignment in `alignments'."
(if (equal argperms '(nil))
;; '(nil) means no recursion on args needed, try adjs
(loop for adjperm in (adjalign nil link mlink tab_s tab_t)
do (store (sub-f adjperm)))
;; try recursion on argperms (if there are any)
(loop for argperm in argperms
for new = (if mlink
(cons mlink (sub-f argperm))
(sub-f argperm))
do (store new)
;; try to fill up adj alignments
(loop for adjperm in (adjalign argperm link mlink tab_s tab_t)
do (store (append (sub-f adjperm) new)))))
; The caller will want to know whether arg-matching was even possible:
argperms))
(when ; If there are no argperms/margperms, this `when' fails and nil is returned
(or
;; Either one-to-one is possible, we don't try merging:
(argloop (argalign link tab_s tab_t)
nil)
;; One-to-one is not possible, we try merging:
(let ((mlinks_s (loop for arg in (get-args F_s tab_s 'no-nulls)
collect (cons arg F_t)))
;; TODO: merging both a_s AND a_t at once... should we?
(mlinks_t (loop for arg in (get-args F_t tab_t 'no-nulls)
collect (cons F_s arg))))
(when *debug* (out "One-to-one f-align failed; merging...~%"))
;; This will return nil if none of the margaligns succeed:
(mapcar-true (lambda (mlink)
(when (or (not LPTs)
(LPT? (get-pred (car mlink) tab_s) tab_s
(get-pred (cdr mlink) tab_t) tab_t LPTs))
(argloop (margalign link mlink tab_s tab_t)
mlink)))
(append mlinks_s mlinks_t))))
(if alignments
;; Return the full sub-alignment:
(cons link alignments)
;; Either sub-f failed, or there were no args to align,
;; check get-args F_t/F_s to find out:
link)))))
(lisp-unit:define-test test-merge
(let ((tab_s (open-and-import "dev/TEST_merge_s.pl"))
(tab_t (open-and-import "dev/TEST_merge_t.pl")))
(lisp-unit:assert-equal
'(((9 . 3)))
(margalign '(0 . 0) '(10 . 0) tab_s tab_t))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((0 . 0) (10 . 0) (9 . 3)) ; perf-qePa, bjeffe-qePa, hund-jaGli (correct)
((0 . 0) (10 . 3) (9 . 0))) ; perf-qePa, bjeffe-jaGli, hund-qePa (wrong)
(flatten (f-align '(0 . 0) tab_s tab_t (make-LPT))))))
(lisp-unit:define-test test-f-align
(let ((*arg-order-smoothing* 0.01)
(*sub-f-smoothing* 0.01)
(*lpt-smoothing* 0.00))
(let ((tab_s (open-and-import "dev/TEST_simple_s.pl"))
(tab_t (open-and-import "dev/TEST_simple_t.pl")))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((0 . 0) (5 . 3)))
(flatten (f-align '(0 . 0) tab_s tab_t (make-LPT)))))
(let ((tab_s (open-and-import "dev/TEST_regargadj_s.pl"))
(tab_t (open-and-import "dev/TEST_regargadj_t.pl"))
(LPT (make-LPT)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((0 . 0) (11 . 6) (10 . 9) (9 . 3)) ((0 . 0) (11 . 6) (10 . 3) (9 . 9))
((0 . 0) (11 . 9) (10 . 6) (9 . 3)) ((0 . 0) (11 . 9) (10 . 3) (9 . 6))
((0 . 0) (11 . 3) (10 . 6) (9 . 9)) ((0 . 0) (11 . 3) (10 . 9) (9 . 6)))
(flatten (f-align '(0 . 0) tab_s tab_t LPT)))
(add-to-lpt "Browne" "Browne" LPT)
(lisp-unit:assert-equal
'((0 . 0) (11 . 9) (10 . 3) (9 . 6))
(rank (f-align '(0 . 0) tab_s tab_t LPT)
tab_s tab_t LPT)))
(let ((tab_s (open-and-import "dev/TEST_optadj_s.pl"))
(tab_t (open-and-import "dev/TEST_optadj_t.pl"))
(LPT (make-LPT)))
(lisp-unit:assert-equality
#'set-of-set-equal
'(((0 . 0) (8 . 2) (31 . 8)) ; adjuncts optionally align
((0 . 0) (8 . 8) (31 . 2))
((0 . 0) (8 . 8)))
(flatten (f-align '(0 . 0) tab_s tab_t LPT))))))
(defun f-link? (x)
(and (consp x)
(atom (car x))
(atom (cdr x))))
(lisp-unit:define-test test-rank-sub-f
;; Should select the alignment where sub-f are really aligned
(let* ((tab_s (open-and-import "dev/TEST_merge_s.pl"))
(tab_t (open-and-import "dev/TEST_merge_t.pl"))
(LPT (make-LPT))
(f-alignments (f-align '(0 . 0) tab_s tab_t LPT)))
(lisp-unit:assert-equal ; make sure next test doesn't give false negative
'((0 . 0) ((9 . 0) (10 . 3)) ((10 . 0) (9 . 3)))
f-alignments)
(lisp-unit:assert-equality
#'set-equal
'((0 . 0) (10 . 0) (9 . 3)) ; perf-qePa, bjeffe-qePa, hund-jaGli (correct)
(values (rank f-alignments tab_s tab_t LPT)))))
(lisp-unit:define-test test-rank-argorder
;; Should select the alignment where argument orders match
(let* ((tab_s (open-and-import "dev/TEST_regargadj_s.pl"))
(tab_t (open-and-import "dev/TEST_regargadj_t.pl"))
(LPT (make-LPT))
(f-alignments (f-align '(0 . 0) tab_s tab_t LPT)))
(lisp-unit:assert-equality ; make sure next test doesn't give false negative
#'set-of-set-equal
'(((0 . 0) (11 . 6) (10 . 9) (9 . 3)) ((0 . 0) (11 . 6) (10 . 3) (9 . 9))
((0 . 0) (11 . 9) (10 . 6) (9 . 3)) ((0 . 0) (11 . 9) (10 . 3) (9 . 6))
((0 . 0) (11 . 3) (10 . 6) (9 . 9)) ((0 . 0) (11 . 3) (10 . 9) (9 . 6)))
(flatten f-alignments))
(lisp-unit:assert-equality
#'set-equal
'((0 . 0) (11 . 3) (10 . 9) (9 . 6))
(values (rank f-alignments tab_s tab_t LPT)))))
(lisp-unit:define-test test-rank-recursive
(lisp-unit:assert-equality
#'set-equal
'((0 . 0) (46 . 30) (116 . 128))
(values (rank-helper nil '((0 . 0) (((46 . 30) ((116 . 128))))))))
(lisp-unit:assert-equality
#'set-equal
'((0 . 0) (verb . verb) (subj . subj) (obj . obj) (adv . adv))
(values (rank-helper
nil '((0 . 0) (((verb . verb) ((subj . subj) (obj . obj))) (adv . adv))))))
(lisp-unit:assert-equality
#'set-equal
'((0 . 0) (46 . 27) (10 . 30) (37 . 29) (2 . 28))
(values (rank-helper
nil '((0 . 0) ((46 . 28) (10 . 29) (37 . 27) ((2 . 30) ((5 . 128))))
((46 . 28) (10 . 29) (37 . 30) (2 . 27))
((46 . 28) (10 . 27) (37 . 29) ((2 . 30) ((5 . 128))))
((46 . 28) (10 . 27) (37 . 30) (2 . 29))
((46 . 28) (10 . 30) (37 . 29) (2 . 27))
((46 . 28) (10 . 30) (37 . 27) (2 . 29))
((46 . 29) (10 . 28) (37 . 27) ((2 . 30) ((5 . 128))))
((46 . 29) (10 . 28) (37 . 30) (2 . 27))
((46 . 29) (10 . 27) (37 . 28) ((2 . 30) ((5 . 128))))
((46 . 29) (10 . 27) (37 . 30) (2 . 28))
((46 . 29) (10 . 30) (37 . 28) (2 . 27))
((46 . 29) (10 . 30) (37 . 27) (2 . 28))
((46 . 27) (10 . 28) (37 . 29) ((2 . 30) ((5 . 128))))
((46 . 27) (10 . 28) (37 . 30) (2 . 29))
((46 . 27) (10 . 29) (37 . 28) ((2 . 30) ((5 . 128))))
((46 . 27) (10 . 29) (37 . 30) (2 . 28))
((46 . 27) (10 . 30) (37 . 28) (2 . 29))
((46 . 27) (10 . 30) (37 . 29) (2 . 28))
(((46 . 30) ((116 . 128))) (10 . 28) (37 . 29) (2 . 27))
(((46 . 30) ((116 . 128))) (10 . 28) (37 . 27) (2 . 29))
(((46 . 30) ((116 . 128))) (10 . 29) (37 . 28) (2 . 27))
(((46 . 30) ((116 . 128))) (10 . 29) (37 . 27) (2 . 28))
(((46 . 30) ((116 . 128))) (10 . 27) (37 . 28) (2 . 29))
(((46 . 30) ((116 . 128))) (10 . 27) (37 . 29) (2 . 28)))))))
;;;;;;;; RANKING:
;;;;;;;; --------
(defun longest-sublists (lists)
"Return the longest sublists in `lists'. Could do this in one loop,
but meh."
(let ((maxlen (loop for l in lists maximize (length l))))
(mapcar-true (lambda (l) (when (>= (length l) maxlen) l))
lists)))
(defun rank (f-alignments tab_s tab_t &optional LPTs)
"This could be done in a million different ways. For now, this is
the procedure: We start with input like '((0 . 0) branch1 branch2 ...)
where branch1 is e.g. '((9 . 0) (10 . 3)),
or '((9 . 0) ((10 . 3) ((1 . 2) (7 . 8)) ((7 . 2) (1 . 8))))
`rank-helper' selects the best-ranking branch of branch1, branch2
etc. It gets a score for each branch from `rank-branch' which
calculates a score for (0 . 0) and a specific branch (set of
argument/adjunct links), multiplied with the ranks of each member of
that branch.
If there are several with equal rank, choose the first of the longest
branches (thus trying to align as many adjuncts as possible)."
(rank-helper nil f-alignments tab_s tab_t LPTs))
(defun rank-helper (seen f-alignments &optional tab_s tab_t LPTs)
(if (f-link? f-alignments)
(values (list f-alignments)
1)
(let* ((link (car f-alignments))
(outer-links (cons link seen)))
;; If (cdr f-alignments) were nil, this would return nil
(loop with best-branches = nil
with best-rate = 0 ; worst possible sub-f-score, all failed
with newbranch
with rate