-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-postproc-lisp-rules.lisp
2360 lines (2001 loc) · 58.1 KB
/
schema-postproc-lisp-rules.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
(load "ll-load.lisp")
(ll-load "schema-el-parser.lisp")
(ll-load "schema-postproc-utils.lisp")
(defparameter *SCHEMA-CLEANUP-FUNCS* '(
; correct-nonverbal-aux ; run this before skolemize-adets
split-marked-lists
pull-out-lambda-advs ; run this before lambda splitters
skolemize-adets
split-and-eps
norm-conjunctive-infixes
split-top-level-lambda-ands
apply-mono-lambdas
; weird-lambda-to-do
split-top-level-ands
unfloat-modifiers
split-double-charstars
split-and-preds
split-and-charstar-preds
bubble-up-pred-charstars
sink-prop-mods
name-skolems
deindex-will
purposify-ka-args
rename-gensyms
flatten-nested-ands
apply-such-determiners
remove-aux-do-did
retag-det-preds
split-whens
adv-ify-temporals
atemporalize-adj-preds
past-tense-story-to-present
; liberate-starred-prop-args ; TODO: fix this so that it looks at args, not stacked charstars
fix-ka-be-pre-arg
fix-apostrophe-poss
rename-np-preds-skolems
temporalize-naked-verb-props
; bubble-up-sent-mods
; reify-pred-args
skolemize-kinds ; This is a bad idea, but I'm doing it anyway.
fix-subs
fix-home-adv
agentify-names-and-pronouns
))
(ldefun agentify-names-and-pronouns (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf targets (dedupe (get-elements-pred phi-copy
(lambda (x) (and
(or (personal-pronoun? x)
(lex-name? x))
(not (contains phi-copy (list x 'AGENT.N))))))))
(loop for target in targets
do (setf phi-copy (append phi-copy (list
(list target 'AGENT.N)))))
(return-from outer phi-copy)
)
)
(ldefun fix-home-adv (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf to-fix (get-elements-pred phi-copy (lambda (x)
(and
(canon-charstar? x)
(contains (prop-mods (car x)) 'HOME.ADV)
)
)))
(loop for tf in to-fix do (block fix
(setf agent (prop-pre-arg (car tf)))
(setf new-home
(list 'A.D (list 'L 'X (list 'AND (list 'X 'HOME.N) (list 'X 'PERTAIN-TO agent)))))
(setf fixed (replace-vals 'HOME.ADV (list 'ADV-A (list 'TO.P new-home)) tf))
(setf phi-copy (replace-vals tf fixed phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun split-and-preds (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf and-props (get-elements-pred phi-copy (lambda (x)
(and
(listp x)
(canon-individual? (car x))
(and-list? (second x))
; TODO: do this for > 2 and-chained preds
; (here and in split-and-eps)
(equal (length (cdr (second x))) 2)
(loop for e in (cdr (second x))
always (probably-pred? e))
)
)))
; (format t "split-and-preds got ~s~%" and-props)
(loop for and-prop in and-props do (block replc
(setf individual (car and-prop))
(setf preds (cdr (second and-prop)))
(setf lhs-prop (list individual (car preds)))
(setf rhs-prop (list individual (second preds)))
(setf new-prop (list lhs-prop 'and rhs-prop))
(setf phi-copy (replace-vals and-prop new-prop phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun fix-subs (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf subs (get-elements-pred phi-copy (lambda (x)
(and (listp x)
(equal (length x) 3)
(equal (car x) 'SUB)))))
(loop for sub in subs do (block fix-sub
(setf hole-form (third sub))
(setf fixed-form (replace-vals '*H (second sub) hole-form))
(setf phi-copy (replace-vals sub fixed-form phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun skolemize-kinds (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf kinds (dedupe (get-elements-pred phi-copy #'canon-k?)))
(loop for kind in kinds do (block skk
(setf kind-pred (second kind))
(setf new-name (new-skolem! 'OBJECT))
(setf phi-copy (replace-vals kind new-name phi-copy))
(setf phi-copy (append phi-copy
(list (list new-name kind-pred))))
))
(return-from outer phi-copy)
)
)
(ldefun temporalize-naked-verb-props (phi)
(block outer
(setf phi-copy (copy-item phi))
; Find all unstarred verb propositions
(setf unstarred-verb-props
(loop for form in phi-copy
if (and (canon-prop? form)
(not (canon-charstar? form))
(verb-pred? (prop-pred form)))
; then
collect form))
; Find the NOW-time of the sentence
(setf now (car
(get-elements-pred phi-copy #'is-now?)))
(loop for uvp in unstarred-verb-props do (block inner
; Make a new episode and orient it to the
; NOW symbol.
(setf new-ep (new-skolem! 'E))
(setf phi-copy (append phi-copy
(list (list new-ep 'AT-ABOUT now))))
; Star the prop with the new ep
(setf phi-copy (replace-vals uvp
(list uvp '** new-ep)
phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun bubble-up-sent-mods (phi)
(block outer
(setf phi-copy (copy-item phi))
; (loop for form in phi-copy do (block fix
; ))
(return-from outer phi-copy)
)
)
(ldefun split-marked-lists (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf split-parents (get-elements-pred phi-copy
(lambda (x) (and (listp x)
(loop for y in x
thereis (and (listp y) (equal (car y) 'LL-SPLIT)))))))
(loop for parent in split-parents
do (block split
(setf new-parent (loop for e in parent
if (and (listp e) (equal (car e) 'LL-SPLIT))
append (cdr e)
else
collect e
))
(setf phi-copy (replace-vals parent
new-parent phi-copy))
)
)
(return-from outer phi-copy)
)
)
(ldefun rename-np-preds-skolems (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf nps (get-elements-pred phi (lambda (x)
(mp x (list (id? 'NP+PREDS) 'lex-skolem?
(list (id? '=) 'lex-name?))))))
(loop for np in nps do (block fix-np
(setf sk-name (second np))
(setf name (second (third np)))
; If the unnamed Skolem is actually a
; Skolemized temporal pred, we'll
; flatten it and bail out so that
; another rule can adv-ify it.
(if (temporal-arg? sk-name phi)
; then
(block flatten-temporal
(setf phi-copy (replace-vals np
; mark this to be split
(list 'LL-SPLIT sk-name name)
phi-copy))
(return-from fix-np)
)
)
(setf phi-copy (replace-vals np name phi-copy))
(setf phi-copy (replace-vals sk-name name phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun fix-apostrophe-poss (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf possessed (get-elements-pred phi-copy
(lambda (x) (mp x (list
(list 'canon-small-individual? (id? '|'S|))
'canon-pred?)))))
(loop for poss in possessed
do (setf phi-copy (replace-vals poss
`(SOME.D (L (X) (AND
(X PERTAIN-TO ,(caar poss))
(X ,(second poss)))))
phi-copy)))
(return-from outer phi-copy)
)
)
; Sometimes there's a (KA BE.V) in the prefix
; arg list as an extra arg, when it really
; should just be a copula (and then be processed
; out later, of course!).
(ldefun fix-ka-be-pre-arg (phi)
(block outer
(setf phi-copy (copy-item phi))
(loop for form in phi-copy do (block fix-form
(if (not (canon-prop? form))
(return-from fix-form))
(setf pre-args (prop-pre-args form))
(if (< (length pre-args) 2)
(return-from fix-form))
(if (equal (car (last pre-args)) '(KA BE.V))
(progn
(setf new-form (render-prop
(subseq pre-args 0 (- (length pre-args) 1))
(prop-pred form)
(prop-post-args form)
(prop-mods form)))
(setf phi-copy (replace-vals form new-form phi-copy))
)
)
))
(return-from outer phi-copy)
)
)
; For nested characterized episodes, like
; ((I say ((you said that) ** E2)) ** E1),
; split out the inner episode to be
; independently floating alongside the outer
; one, and leave the argument in the outer
; one as (for now---corresponding with Len
; about a better option) an unstarred copy
; of the same inner prop.
(ldefun liberate-starred-prop-args (phi)
(block outer
(setf phi-copy (copy-item phi))
(loop for form in phi-copy
if (and
(canon-charstar? form)
(canon-charstar? (car form)))
; (loop for arg in (prop-all-args (car form)) thereis (canon-charstar? arg))
; then
do (block liberate-inner
; These returned stripped-eps are in the depth-first
; ordering as originally fetched from the embedded
; props
(setf stp (strip-charstar-eps form))
(setf stripped-form (car stp))
(setf all-stripped-eps (second stp))
; We don't need the outermost ep
(setf stripped-eps
(subseq all-stripped-eps 0 (- (length all-stripped-eps) 1)))
; These come in depth-first order, and so can go
; directly with the eps
(setf inner-charstar-args
(get-elements-pred
(prop-all-args stripped-form)
#'canon-prop?))
; No args are also props, and were likely
; never characterized by the multiple episodes
(if (not (equal (length inner-charstar-args) (length stripped-eps)))
; then
(return-from liberate-inner)
)
; Add the "liberated" inner ** props back as siblings
(loop for inner in inner-charstar-args
for inner-ep in stripped-eps
do (setf phi-copy
(append phi-copy (list (list inner '** inner-ep))))
)
; Re-characterize the outermost formula with only its
; own episode
(setf phi-copy (replace-vals
form
(list stripped-form '** (car (last all-stripped-eps)))
phi-copy))
)
)
(return-from outer phi-copy)
)
)
; The BEFORE relation is not as restrictive as the
; AT-ABOUT relation, so for stories in the past tense,
; we're unable to construct an ordering of the sentences
; just given an ordering of the NOW-relations they're each
; related to. So, we'll assume that the past-tense story
; can just be shifted forward in time to "NOW".
(ldefun past-tense-story-to-present (phi)
(block outer
(setf phi-copy (copy-list phi))
(setf now-props
(loop for form in phi
if (and
(time-prop? form)
(not (null (get-elements-pred form #'is-now?))))
; then
collect form))
; The BEFORE props may not all be valid yet,
; or may not all have been extracted from
; invalid structures yet. We'll know it's
; ready when the number of now-props equals
; the number of NOW-symbols in the entire story.
(if (not (equal
(length now-props)
(length (get-elements-pred phi #'is-now?))))
; then
(return-from outer phi-copy)
)
(if (loop for now-prop in now-props
always (equal (prop-pred now-prop) 'BEFORE))
; then
(loop for now-prop in now-props
do (block replace-past-tense
(setf new-now-prop
(replace-vals 'BEFORE 'AT-ABOUT now-prop))
(setf phi-copy
(replace-vals now-prop new-now-prop phi-copy))
)
)
)
(return-from outer phi-copy)
)
)
(ldefun atemporalize-adj-preds (phi)
(block outer
(setf phi-copy (copy-list phi))
(loop for form in phi do (block loop-outer
(if (not (canon-charstar? form))
(return-from loop-outer)
)
(setf stp (strip-charstar-eps form))
(setf stripped-form (car stp))
(setf stripped-eps (second stp))
(if (not (lex-adj? (pred-base (prop-pred stripped-form))))
(return-from loop-outer)
)
(setf phi-copy (replace-vals form stripped-form phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun reify-pred-args (phi)
(block outer
(setf phi-copy (copy-list phi))
(loop for form in phi do (block loop-outer
(if (not (canon-prop? form))
(return-from loop-outer))
(setf stripped-form (copy-item form))
(setf stripped-eps (list))
(if (canon-charstar? stripped-form)
(progn
; (setf stripped-form (car stripped-form)))
(setf stp (strip-charstar-eps stripped-form))
(setf stripped-form (car stp))
(setf stripped-eps (second stp))
)
)
(if (be-verb? (prop-pred stripped-form))
(return-from loop-outer))
(setf new-form (copy-item stripped-form))
(loop for arg in (prop-post-args stripped-form)
if (canon-pred? arg)
do (setf new-form (replace-vals arg (list 'KJ arg) new-form))
)
;(if (not (equal stripped-form form))
;(setf new-form (list new-form '** (third form))))
(if (not (null stripped-eps))
(setf new-form (apply-charstar-eps new-form stripped-eps))
)
(setf phi-copy (replace-vals form new-form phi-copy))
))
(return-from outer phi-copy)
)
)
(ldefun deindex-will (phi)
(block outer
(setf phi-copy (copy-list phi))
(loop for form in phi do (block loop-outer
(if (and
(canon-charstar? form)
(not (null (member 'WILL.MD (prop-mods (car form)) :test #'equal)))
)
; then
(block loop-inner
(setf deindexed-no-ep (ttt-replace
(car form)
(list 'will.md '_!1)
'_!1
))
(setf new-ep (new-skolem! 'E))
(setf deindexed (list deindexed-no-ep '** new-ep))
(setf phi-copy (replace-vals form deindexed phi-copy))
(setf phi-copy (append phi-copy (list (list new-ep 'AFTER (third form)))))
)
)
))
(return-from outer phi-copy)
)
)
(ldefun personal-pronoun? (p)
(and
(symbolp p)
(has-suffix? (string p) "PRO")
(or
(has-prefix? (string p) "I$")
(has-prefix? (string p) "ME$")
(has-prefix? (string p) "HE$")
(has-prefix? (string p) "SHE$")
(has-prefix? (string p) "WE$")
(has-prefix? (string p) "THEY$")
)
)
)
(ldefun has-skolem-prefix? (sym pre)
(and
(symbolp sym)
(let ((spl (split-str (string sym) ".")))
(equal (length spl) 2)
(equal (second spl) "SK")
(has-prefix? (car spl) pre)
(is-num-str? (remove-prefix (car spl) pre))
)
)
)
(ldefun is-characterized? (phi sym)
(not (null (get-elements-pred phi (lambda (x)
(and
(canon-charstar? x)
(equal (third x) sym)
)
))))
)
; Ideally, this wouldn't be necessary, but sometimes
; Skolemized things still get the OBJECT name, e.g. if
; a lambda-and predicate was Skolemized and couldn't be
; parsed for a name until it was split later.
; Further, certain things can be stuck with the default
; "E" name even if they don't characterize any episodes;
; we'll fix that, too.
(ldefun name-skolems (phi)
(name-skolems-maybe-accept-non-nouns
(name-skolems-maybe-accept-non-nouns phi nil)
t
)
)
(ldefun is-skolemized-from-pred (sk pred)
(let (noun-sym)
(block outer
(setf noun-sym (extract-noun-sym pred))
(if (and
(not (null noun-sym))
(has-prefix? (format nil "~s" sk) noun-sym)
)
; then
(return-from outer t)
)
(return-from outer nil)
)
)
)
(ldefun temporal-arg? (arg &optional story)
(block outer
(if (loop for tp in *TEMPORAL-PREDS*
thereis (has-element (car (clean-idx-tags (list arg))) (list 'K tp)))
; then
(return-from outer t)
)
(if (loop for tp in *TEMPORAL-PREDS*
thereis (has-element-pred (car (clean-idx-tags (list arg)))
(lambda (x) (mp x (list
'lex-det? (id? tp))))))
; then
(return-from outer t))
(if (null story)
(return-from outer nil))
(setf arg-rcs (loop for phi in story
if (and (equal (length phi) 2) (equal (car phi) arg))
collect (second phi)))
(if (loop for rc in arg-rcs thereis
(loop for tp in *TEMPORAL-PREDS* thereis
(contains (car (clean-idx-tags (list (listify-nonlists rc)))) tp)))
; then
(return-from outer t)
)
(return-from outer nil)
)
)
(ldefun adv-ify-temporals (phi)
(block outer
(setf phi-copy (copy-list phi))
(loop for form in phi do (block loop-outer
(if (not (canon-prop? form))
(return-from loop-outer)
)
(setf stripped-form (copy-item form))
(setf stripped-eps (list))
(if (canon-charstar? form)
; (setf stripped-form (car stripped-form)))
(progn
(setf stp (strip-charstar-eps form))
(setf stripped-form (car stp))
(setf stripped-eps (second stp))
)
)
(setf post-args (prop-post-args stripped-form))
(setf post-temporals (loop for pa in post-args if (temporal-arg? pa phi) collect pa))
(if (not (null post-temporals))
(setf stripped-form (remove-prop-post-args stripped-form post-temporals))
)
(setf temporals post-temporals)
; If there are multiple prefix args, remove
; all of the temporal ones.
(setf pre-args (prop-pre-args stripped-form))
(setf pre-temporals (loop for pa in pre-args if (temporal-arg? pa phi) collect pa))
(if (and
(not (null pre-temporals))
(> (length pre-args) (length pre-temporals)))
; then
(progn
(setf stripped-form (remove-prop-pre-args stripped-form pre-temporals))
(setf temporals (append pre-temporals temporals))
)
)
;(setf temporal-mods (loop for temp in temporals
; if (and (canon-kind? temp) (symbolp (second temp)))
; collect (retag-as (second temp) "ADV-E")
; if (and (canon-kind? temp) (not (symbolp (second temp))))
; collect (list 'ADV-E (second temp))
; if (not (canon-kind? temp))
; collect (list 'ADV-E temp)
;))
(setf temporal-mods (loop for temp in temporals
collect (list 'ADV-E (list 'during temp))))
(setf stripped-form (add-prop-mods stripped-form temporal-mods))
;(if (canon-charstar? form)
; then
;(setf stripped-form (list stripped-form '** (third form)))
;)
(if (not (null stripped-eps))
(setf stripped-form (apply-charstar-eps stripped-form stripped-eps))
)
(if (not (equal form stripped-form)) (progn
; (format t "replacing ~s with ~s~%" form stripped-form)
(setf phi-copy (replace-vals form stripped-form phi-copy))
))
))
(return-from outer phi-copy)
)
)
(ldefun name-skolems-maybe-accept-non-nouns (phi accept-non-nouns)
(block outer
(setf phi-copy (copy-list phi))
(loop for form in phi do (block loop-outer
(if (not (listp form))
(return-from loop-outer)
)
(if (and
(listp form)
; (canon-charstar? form)
(pseudo-charstar? form)
(lex-skolem? (third form))
; (has-prefix? (format nil "~s" (third form)) "OBJECT")
(has-skolem-prefix? (third form) "OBJECT")
)
; then
(block loop-inner
(setf new-skolem (new-skolem! 'E))
(setf phi-copy (replace-vals (third form) new-skolem phi-copy))
)
)
(setf noun-sym (extract-noun-sym (second form)))
(setf constraints (loop for form2 in phi if (and (listp form2) (equal (length form2) 2) (equal (car form2) (car form))) collect form2))
(if (and
(listp form)
(equal 2 (length form))
(lex-skolem? (car form))
; don't rename it if another noun predicate has already "claimed" it:
(loop for c in constraints never (is-skolemized-from-pred (car c) (second c)))
(or
(symbolp (second form))
(and
(not (null noun-sym))
(not (has-prefix? (format nil "~s" (car form)) noun-sym))
)
)
(or
; we can replace renamed stuff if we find a noun
(has-skolem-prefix? (car form) "OBJECT")
(and
(has-skolem-prefix? (car form) "E")
(not (is-characterized? phi-copy (car form)))
)
(and
(not (null noun-sym))
(not (has-prefix? (format nil "~s" (car form)) noun-sym))
)
)
)
; then
(block loop-inner
(setf sym-prefix nil)
(if (null noun-sym)
; then
(progn
(setf sym-spl (split-str (format nil "~s" (second form)) "."))
(setf sym-prefix (car sym-spl))
(setf sym-suffix (second sym-spl))
; Note: we prefer to do this with nouns. We can do it
; with adjectives if no other "word" predicates are
; available, but if there are several, we'll always
; choose a noun first.
(if (and (not accept-non-nouns) (not (equal sym-suffix "N")))
; then
(return-from loop-outer)
)
; if the thing is actually given the OBJECT.N predicate,
; we don't need to rename it
(if (equal sym-prefix "OBJECT")
; then
(return-from loop-outer)
)
)
; else
(setf sym-prefix noun-sym)
)
; (format t "replacing ~s with ~s~%" (car form) sym-prefix)
(setf new-skolem (new-skolem! (intern sym-prefix)))
(setf phi-copy (replace-vals (car form) new-skolem phi-copy))
)
)
))
(return-from outer phi-copy)
)
)
(ldefun unique-var (cursor used)
(if (not (member cursor used :test #'equal))
; then
cursor
; else
(unique-var (next-str cursor) used)
)
)
(ldefun replace-dets-with-k (x)
(block outer
(if (not (listp x))
; then
(return-from outer x)
)
(setf x-copy (copy-item x))
(setf dets (get-elements-pred x #'lex-det?))
(loop for det in dets
do (setf x-copy (replace-vals det 'K x-copy))
)
(return-from outer x-copy)
)
)
(ldefun arg-with-adv-lambda? (arg)
(and
(listp arg)
(equal (car arg) 'K)
(canon-lambda? (second arg))
(listp (third (second arg)))
(equal (car (third (second arg))) 'AND)
(loop for y in (cdr (third (second arg)))
thereis (canon-mod? (second y)))
)
)
(ldefun remove-aux-do-did (phi)
(block outer
(setf phi-copy (copy-item phi))
(loop for form in phi do (block inner
(if (not (listp form))
(return-from inner))
(if (has-element form 'DO.AUX)
; then
(setf phi-copy (replace-vals form (rec-remove form 'DO.AUX) phi-copy))
)
(if (has-element form 'DOES.AUX)
; then
(setf phi-copy (replace-vals form (rec-remove form 'DOES.AUX) phi-copy))
)
; Re-orient the episode backward in time.
(if (has-element form 'DID.AUX)
; then
(block did1
; Remove DID.AUX.
(setf new-form (rec-remove form 'DID.AUX))
; If there's an episode, replace the
; episode in the formula with a new one,
; and add a formula constraining the
; new one to be before the old one.
(if (canon-charstar? new-form)
; then
(block did2
(setf ep (third new-form))
(setf new-ep (new-skolem! 'E))
(setf phi-copy (append phi-copy (list (list new-ep 'BEFORE ep))))
(setf new-form (list (car new-form) (second new-form) new-ep))
)
)
; Replace the old formula.
(setf phi-copy (replace-vals form new-form phi-copy))
)
)
))
(return-from outer phi-copy)
)
)
; Sometimes, determiners like ONE.D get used as
; predicates, e.g. (ONE1.SK ONE.D). We'll turn
; that into a noun here.
(ldefun retag-det-preds (phi)
(block outer
(setf phi-copy (copy-item phi))
(loop for e in phi-copy
if (and
(listp e)
(equal 2 (length e))
(canon-individual? (car e))
(lex-det? (second e))
)
; then
do (setf phi-copy (replace-vals
e
(list (car e) (retag-as (second e) 'N))
phi-copy))
)
(return-from outer phi-copy)
)
)
(ldefun split-whens (phi)
(block outer
(setf phi-copy (copy-item phi))
(loop for e in phi-copy
if (and
; (canon-charstar? e) [it won't be a valid prop]
(listp e)
(equal 3 (length e))
(equal '** (second e))
(canon-individual? (third e))
(listp (car e))
(equal 3 (length (car e)))
(or
(matches-ttt (caar e) '((!1 (ll-curry eq-no-idx-tags? WHEN.P)) (!2 probably-prop?)))
(matches-ttt (caar e) '((!1 (ll-curry eq-no-idx-tags? WHEN.ADV)) (!2 probably-prop?)))
)
(canon-individual? (second (car e)))
(probably-pred? (third (car e)))
)
; then
do (block split-when
; split the two props
(setf before-prop (second (caar e)))
(setf after-prop (append (list (second (car e)) (list (third (car e))))))
(setf after-ep (third e))
; create a new episode for the "when..."
; framing proposition, oriented at-about
; the existing episode.
(setf before-ep (new-skolem! 'E))
(setf phi-copy (append phi-copy (list (list after-ep 'AT-ABOUT before-ep))))
(setf before-prop (list before-prop '** before-ep))
(setf after-prop (list after-prop '** after-ep))
; replace the original episode with the "after" episode
(setf phi-copy (replace-vals e after-prop phi-copy))
; and add the "before" episode separately
(setf phi-copy (append phi-copy (list before-prop)))
)
)
(return-from outer phi-copy)
)
)
(ldefun correct-nonverbal-aux (phi)
(block outer
(setf phi-copy (copy-item phi))
; (format t "got phi ~s~%" phi)
(setf need-fixing (get-elements-pred phi-copy
(lambda (x)
(and
(canon-pred? x)
(not (lex-verb? (pred-base x)))
(loop for m in (pred-mods x)
thereis (lex-modal? m))
)
)
))
(loop for pred in need-fixing
do (block fix-pred
(setf new-pred (copy-item pred))
(loop for m in (pred-mods pred)
if (lex-modal? m)
do (setf new-pred (replace-vals m (retag-as m "V") new-pred))
)
(setf phi-copy (replace-vals pred new-pred phi-copy))
)
)
; (format t "~s need fixing~%" need-fixing)
; (if (not (equal phi phi-copy))
; (format t "replaced ~s with ~s~%" phi phi-copy)
; )
(return-from outer phi-copy)
)
)
(ldefun pull-out-lambda-advs (phi)
(block outer
(setf phi-copy (copy-item phi))
(setf lambda-advs (get-elements-pred phi-copy
(lambda (x) (and