-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-el.lisp
777 lines (655 loc) · 15.6 KB
/
schema-el.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
(load "ll-load.lisp")
(ll-load "schema-el-lex.lisp")
(ll-load "ll-util.lisp")
(ll-load "ll-cache.lisp")
(defparameter *TYPES*
'(
canon-kind?
canon-individual?
; canon-attr?
canon-pred?
canon-mod?
canon-prop?
)
)
(ldefun special-str (x)
; (not (null (member x *KEYWORDS* :test #'equal)))
(gethash x *KEYWORDS-MAP*)
)
(ldefun typecheck (x)
(loop for c in *TYPES*
if (funcall c x)
collect c
)
)
; TODO (CURRENT): rewrite parse functions to determine whether
; something is a pred, proposition, modifier, individual...
(ldefun canon-n-preds? (x)
(mp x (list (list 'id? 'N+PREDS) 'canon-pred?+))
)
(ldefun canon-lambda? (x)
(or
;(mp x (list (list 'id? 'LAMBDA.EL) 'ent-list? 'canon-prop?))
;(mp x (list (list 'id? 'L) 'ent-list? 'canon-prop?))
(mp x (list (list 'id? 'LAMBDA.EL) 'ent-list? 'any?))
(mp x (list (list 'id? 'L) 'ent-list? 'any?))
(canon-n-preds? x)
)
)
(ldefun canon-ka? (x)
(or
(mp x (list (list 'id? 'KA) 'canon-pred?))
(mp x (list (list 'id? 'KA) 'canon-pred? 'canon-individual?+))
)
)
(ldefun canon-ke? (x)
(or
(mp x (list (list 'id? 'KE) 'canon-prop?))
)
)
(ldefun canon-that? (x)
(or
(mp x (list (list 'id? 'THAT) 'canon-prop?))
(mp x (list (list 'id? 'THT) 'canon-prop?))
)
)
(ldefun canon-event? (x)
(or
(canon-ke? x)
(canon-that? x)
)
)
(ldefun canon-k? (x)
(or
(mp x (list (list 'id? 'K) 'canon-pred?))
(mp x (list (list 'id? 'K) 'canon-pred? 'canon-individual?+))
)
)
(ldefun canon-kind? (x)
(or
; TODO: restrictions on VP/non-VP preds for KA/K?
(canon-k? x)
(canon-ka? x)
(mp x (list (list 'id? 'KE) 'canon-prop?))
(mp x (list (list 'id? 'KJ) 'adj-pred?))
)
)
(ldefun canon-small-individual? (x)
(ll-cache #'u-canon-small-individual?
(list x) 100 nil)
)
(ldefun u-canon-small-individual? (x)
(and
(symbolp x)
(not (equal ':R x)) ; string renders of ':R sometimes omit :
(or
(numberp x)
(lex-speech? x)
(lex-skolem? x)
(lex-pronoun? x)
(lex-name? x)
(lex-naked-var? x)
)
)
)
(ldefun canon-individual? (x)
(or
(canon-small-individual? x)
(canon-kind? x)
(varp x)
(mp x (list 'lex-fn? 'canon-individual?+)) ; can a function have
(mp x (list (list 'id? 'IND) 'any?+)) ; Type-shifter
; 0 args?
(mp x (list 'lex-p-arg? 'canon-individual?))
(mp x (list (list 'id? 'THAT) 'canon-prop?))
(mp x (list (list 'id? 'THT) 'canon-prop?))
(mp x (list (list 'id? 'ANS-TO) 'canon-prop?))
(mp x (list 'lex-det? 'canon-pred?))
; SET-OF with individual arguments is an individual.
; SET-OF with a predicate argument is a predicate.
(mp x (list (list 'id? 'SET-OF) 'canon-individual?+))
)
)
(ldefun canon-attr? (x)
(or
(equal x 'PLUR)
(equal x 'SET-OF)
(mp x (list (list 'id? 'ATTR) 'canon-pred?))
(mp x (list (list 'id? 'NN) 'canon-pred?))
(lex-attr-pred? x)
)
)
(ldefun canon-prep? (x)
(or
; (lex-p? x)
(mp x (list 'lex-p? 'canon-individual?))
)
)
; Anything that can be returned from pred-base, i.e., no
; args or mods.
(ldefun atomic-pred? (x)
(or
; Explicitly marked predicates are predicates
(lex-pred? x)
; SET-OF with individual arguments is an individual.
; SET-OF with a predicate argument is a predicate.
(mp x (list (list 'id? 'SET-OF) 'canon-pred?))
; Just for right now.
; (lex-det? x)
; Lambda functions are predicates
(canon-lambda? x)
; Some special symbols are predicates
(not (null (member x *KEYWORD-PREDS* :test #'equal)))
; Bare verbs/modals, nouns, adjectives, and prepositions are predicates
(lex-verb? x)
(lex-metapred? x)
(lex-noun? x)
; (lex-modal? x)
(lex-adj? x)
(lex-p? x)
(mp x (list (id? 'PASV) 'lex-verb?))
)
)
(ldefun canon-pred? (x)
(ll-cache #'u-canon-pred? (list x) 100 nil)
)
(ldefun u-canon-pred? (x)
(or
(atomic-pred? x)
; (mp x (list 'lex-modal? 'canon-pred?))
; Allow attributes, like ((attr happy.a) boy.n)
; (mp x (list 'canon-attr? 'canon-pred?))
; Allow serialized arguments for e.g. verb phrases
(mp x (list 'canon-pred? 'canon-arg?+))
; Prepositions with individual complements are predicates
; (mp x (list 'lex-p? 'canon-individual?))
; Modified predicates are predicates (e.g. adverbialized VPs)
(mp x (list 'canon-mod? 'canon-pred?))
)
)
(ldefun canon-pred-or-mod? (x)
(or
(canon-pred? x)
(canon-mod? x)
)
)
(ldefun canon-sent-mod? (x)
(or
(has-ext? x ".AUX-S")
(has-ext? x ".ADV-S")
(has-ext? x ".ADV-E")
(equal x 'NOT)
(equal x 'PROG)
(equal x 'PERF)
(mp x (list (list 'id? 'ADV-S) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-E) 'canon-pred-or-mod?+))
)
)
(ldefun strip-charstar-eps (phi)
(block outer
(setf eps (list))
(loop while (canon-charstar? phi)
do (progn
(setf eps (append eps (list (third phi))))
(setf phi (car phi))
)
)
(return-from outer (list phi (reverse eps)))
)
)
(ldefun apply-charstar-eps (phi eps)
(block outer
(setf new-phi (copy-item phi))
(loop for ep in eps
do (setf new-phi (list new-phi '** ep))
)
(return-from outer new-phi)
)
)
(ldefun canon-any-mod? (x)
(or
(canon-sent-mod? x)
(canon-mod? x)
)
)
(ldefun canon-adv? (x)
(or
(lex-adv? x)
(mp x (list (list 'id? 'ADV) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-A) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-F) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-E) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-S) 'canon-pred-or-mod?+))
)
)
(ldefun canon-mod? (x)
(or
(lex-adv? x)
(lex-attr-pred? x)
(lex-modal? x)
(lex-metapred? x)
(equal x 'BE.PASV)
(equal x 'PLUR)
; Sometimes sentential modifiers
; get caught in predicate modifier
; nests, so we'll allow them to be
; "valid" here, but then float them
; upward during postprocessing.
(canon-sent-mod? x)
(mp x (list (list 'id? 'ADV) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-A) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ADV-F) 'canon-pred-or-mod?+))
(mp x (list (list 'id? ':R) 'canon-pred-or-mod?+))
(mp x (list (list 'id? 'ATTR) 'canon-pred-or-mod?))
(mp x (list (list 'id? 'NN) 'canon-pred-or-mod?))
)
)
(ldefun plur? (x)
(and
(listp x)
(equal 2 (length x))
(or
(equal 'PLUR (car x))
(equal 'SET-OF (car x))
)
(canon-pred? (second x))
)
)
(ldefun pseudo-charstar? (x)
(and
(listp x)
(equal (length x) 3)
(equal (second x) '**)
)
)
(ldefun canon-charstar? (x)
(ll-cache #'u-canon-charstar?
(list x) 100 nil)
)
(ldefun u-canon-charstar? (x)
(and
; these speed everything up by ensuring we never
; call canon-prop on things without ** operators
(listp x)
(contains x '**)
(or
(mp x (list 'canon-prop? (list 'id? '*) 'canon-individual?))
(mp x (list 'canon-prop? (list 'id? '**) 'canon-individual?))
)
)
)
(ldefun canon-arg? (x)
(or
(canon-pred? x)
(canon-prop? x)
(canon-individual? x)
)
)
(ldefun canon-atomic-prop? (x)
(or
(mp x (list 'canon-individual?+ 'canon-pred?))
; Special case for * and **
(canon-charstar? x)
; Sentential modifiers
(mp x (list 'canon-sent-mod? 'canon-prop?))
; This is like the similar serial argument rule in canon-pred?, but
; it allows the pred to be flattened with the subject (prefixed) and "curried" args (postfixed).
(mp x (list 'canon-individual?+ 'canon-pred? 'canon-arg?+))
; CERTAIN-TO-DEGREE and NECESSARY-TO-DEGREE don't
; follow normal prop rules; best to add exceptions
; for now. (I think?)
(mp x (list 'exc-varp (id? 'CERTAIN-TO-DEGREE) (list (id? '/) #'numberp #'numberp)))
(mp x (list 'exc-varp (id? 'NECESSARY-TO-DEGREE) #'numberp))
)
)
(ldefun canon-prop? (x)
(ll-cache
#'u-canon-prop?
(list x)
100
nil
)
)
(ldefun u-canon-prop? (x)
(or
(canon-atomic-prop? x)
; Boolean combinations
(mp x (list (list 'id? 'NOT) 'canon-prop?))
(mp x (list (list 'id? 'OR) 'canon-prop?+))
(mp x (list (list 'id? 'AND) 'canon-prop?+))
(mp x (list (list 'id? 'IF) 'canon-prop? 'canon-prop?))
)
)
; Manipulation/normalization functions
(ldefun pred-mods (pred)
(progn
;(format t "in the main way~%")
(helper-pred-mods pred)
)
)
(ldefun helper-pred-mods (pred)
(block outer
(check #'canon-pred? pred)
(if (mp pred (list 'canon-pred? 'canon-individual?+))
; then
(progn
; (format t "here1 ~s~%" pred)
(return-from outer (helper-pred-mods (car pred)))
)
)
(if (mp pred (list 'canon-mod? 'canon-pred?))
; then
(progn
;(format t "here2 ~s~%" pred)
;(format t "car pred is ~s~%" (car pred))
;(format t "pmsec is ~s~%" (pred-mods (second pred)))
;(format t "append is ~s~%" (append (list (car pred)) (helper-pred-mods (second pred))))
(return-from outer (append (list (car pred)) (helper-pred-mods (second pred))))
)
)
(return-from outer nil)
)
)
(ldefun noun-pred? (pred)
(and
(canon-pred? pred)
(lex-noun? (pred-base pred))
)
)
(ldefun verb-pred? (pred)
(and
(canon-pred? pred)
(lex-verb? (pred-base pred))
)
)
(ldefun adj-pred? (pred)
(and
(canon-pred? pred)
(or
(lex-adj? (pred-base pred))
(mp (pred-base pred) (list (id? 'PASV) 'lex-verb?))
)
)
)
(ldefun pred-base (pred)
(check #'canon-pred? pred)
(block outer
(if (atomic-pred? pred)
(return-from outer pred)
)
(if (mp pred (list 'canon-pred? 'canon-individual?+))
; then
(return-from outer (pred-base (car pred)))
)
(if (mp pred (list 'canon-mod? 'canon-pred?))
; then
(return-from outer (pred-base (second pred)))
)
(if (mp pred (list 'canon-attr? 'canon-pred?))
; then
(return-from outer (pred-base (second pred)))
)
(return-from outer pred)
)
)
(ldefun apply-mods (mods pred)
(if (null mods)
; then
pred
; else
(list (car mods) (apply-mods (cdr mods) pred))
)
)
; for preds without modifiers (helper)
(ldefun naked-pred-without-post-args (naked-pred)
; TODO: handle or, and, not, etc.
(block outer
(if (atomic-pred? naked-pred)
(return-from outer naked-pred)
)
(if (mp naked-pred (list 'lex-p? 'canon-individual?))
(return-from outer naked-pred)
)
(if (not (listp naked-pred))
; then
(return-from outer naked-pred)
; else
(return-from outer (car naked-pred))
)
)
)
; for preds with modifiers
(ldefun pred-without-post-args (pred)
(check #'canon-pred? pred)
(let (mods base-pred)
(block outer
(if (atomic-pred? pred)
(return-from outer pred)
)
(setf mods (pred-mods pred))
(setf bp (naked-pred-without-post-args (pred-base pred)))
; (format t "bp: ~s~%" bp)
(return-from outer (apply-mods mods bp))
)
)
)
; pred-args returns postfixed args that are "fixed"
; / "curried into" a predicate to form a new predicate;
; most commonly, these would be the objects of a verb,
; with the "prefix" argument being the subject.
(ldefun pred-args (pred)
(check #'canon-pred? pred)
(block outer
; Lambdas are atomic; ignore "args"?
(if (canon-lambda? pred)
(return-from outer nil)
)
; Strip all modifiers & recurse, if applicable
(if (mp pred (list 'canon-mod? 'canon-pred?))
; then
(return-from outer (pred-args (second pred)))
)
; Non-lists have no postfix args
(if (not (listp pred))
(return-from outer nil))
; We'll assume lambdas have none, although we could
; tell in some cases by finding the variables of an
; underlying predicate that are "hardcoded" in the
; lambda body....
; TODO?
; Now loop to get all the args. We'll assume the base
; pred doesn't nest another set of them, because that'd
; be crazy.
(return-from outer (loop for e in (cdr pred)
if (canon-arg? e)
collect e
))
)
)
(ldefun prop-args-pred-mods (prop)
(ll-cache
#'u-prop-args-pred-mods
(list prop)
100
nil
)
)
(ldefun u-prop-args-pred-mods (prop)
(check #'canon-prop? prop)
(let (pred-idx pre-args pred embedded-post-args flat-post-args post-args mods)
(block outer
; special case: handle * and ** operator preds
(if (and
(equal 3 (length prop))
(or (equal '* (second prop)) (equal '** (second prop))))
; then
(return-from outer (list
(list 'THAT (car prop)) ; prefix args (reified prop)
(second prop) ; predicate (* or **)
(third prop) ; postfix args (char'd episode)
nil)) ; no mods
)
; strip propositional modifiers off first
(setf sent-mods (list))
(loop while (canon-sent-mod? (car prop))
do (setf sent-mods (append sent-mods (list (car prop))))
do (setf prop (second prop))
)
; special case: strip nots
(if (equal (car prop) 'NOT)
(return-from outer (prop-args-pred-mods (second prop)))
)
; default case: prefix args, pred, postfix args
(setf pred-idx (position-if #'canon-pred? prop :from-end t))
(if (null pred-idx)
; It's a conjunction
(return-from outer nil)
)
(if (> pred-idx 0)
(setf pre-args (subseq prop 0 pred-idx))
)
(setf arged-pred (nth pred-idx prop))
(setf pred (pred-without-post-args (nth pred-idx prop)))
; special case: if the pred has postfix args in it already, we won't allow
; "flat" postfix args
(if (not (null (pred-args arged-pred)))
; then
(progn
(setf embedded-post-args (pred-args arged-pred))
(setf post-args embedded-post-args)
)
)
(if (< pred-idx (- (length prop) 1))
; then
(progn
(setf flat-post-args (subseq prop (+ 1 pred-idx) (length prop)))
(setf post-args flat-post-args)
)
)
(if (and (not (null embedded-post-args)) (not (null flat-post-args)))
; then
(progn
(format t "WEIRDNESS ERROR: prop ~s (pred ~s) has both embedded postfix args and flat, serial ones!~%" prop arged-pred)
(return-from outer nil)
)
)
; group the pred and prop mods together, for now
; they'll get sorted out later
; TODO: sort them out explicitly and make a new
; breakdown field!
(setf mods (pred-mods pred))
(setf mods (append sent-mods mods))
(return-from outer (list pre-args (pred-base pred) post-args mods))
)
)
)
(ldefun prop-pre-args (prop)
(car (prop-args-pred-mods prop))
)
(ldefun prop-pre-arg (prop)
(car (prop-pre-args prop))
)
(ldefun prop-pred (prop)
(second (prop-args-pred-mods prop))
)
(ldefun prop-pred-with-post-args (prop)
(append
(list (prop-pred prop))
(prop-post-args prop)
)
)
(ldefun prop-pred-strip-charstars (prop)
(if (equal '** (prop-pred prop))
; then
(prop-pred (car prop))
; else
(prop-pred prop)
)
)
(ldefun prop-post-args (prop)
(third (prop-args-pred-mods prop))
)
(ldefun prop-all-args (prop)
(append (listify-nonlists (prop-pre-args prop)) (listify-nonlists (prop-post-args prop)))
)
(ldefun prop-mods (prop)
(fourth (prop-args-pred-mods prop))
)
(ldefun add-prop-mods (prop mods)
(render-prop
(prop-pre-args prop)
(prop-pred prop)
(prop-post-args prop)
(append mods (prop-mods prop))
)
)
(ldefun remove-prop-pre-args (prop pre-args)
(render-prop
(remove-if (lambda (x) (contains pre-args x))
(prop-pre-args prop))
(prop-pred prop)
(prop-post-args prop)
(prop-mods prop)
)
)
(ldefun remove-prop-post-args (prop post-args)
(render-prop
(prop-pre-args prop)
(prop-pred prop)
(remove-if (lambda (x) (contains post-args x))
(prop-post-args prop))
(prop-mods prop)
)
)
(ldefun render-prop (pre-args pred post-args mods)
(block outer
(setf wrapped-pred pred)
(setf pred-mods (loop for m in mods if (not (canon-sent-mod? m)) collect m))
(setf prop-mods (set-difference mods pred-mods :test #'equal))
(loop for m in (reverse pred-mods)
do (setf wrapped-pred (list m wrapped-pred))
)
(setf wrapped-prop (append
pre-args
;(unwrap-singletons (list (append
;(list wrapped-pred)
;post-args
;)))
(if (not (null post-args))
(list
(append
(list wrapped-pred)
post-args))
; else
(list wrapped-pred)
)
))
; (loop for m in (reverse prop-mods)
(loop for m in prop-mods
do (setf wrapped-prop (list m wrapped-prop))
)
; (format t "pred mods were ~s~%" pred-mods)
; (format t "prop mods were ~s~%" prop-mods)
; (format t "wrapped it up as ~s~%" wrapped-prop)
(return-from outer wrapped-prop)
)
)
(ldefun rerender-prop (prop)
(apply #'render-prop
(prop-args-pred-mods prop))
)
(ldefun render-pred (pred post-args mods)
(block outer
(setf wrapped-pred pred)
(loop for m in mods
do (setf wrapped-pred (list m wrapped-pred))
)
(if (> (length post-args) 1)
(return-from outer (append
(list wrapped-pred)
post-args
))
)
(return-from outer wrapped-pred)
)
)