-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-el-lex.lisp
585 lines (498 loc) · 9.34 KB
/
schema-el-lex.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
; This file specifies a context-free grammar for limited EL formulas
; in the form of recursive descent rules.
; OPT: a cache for all preds, similar to the cache in (ldefun mp)
(load "ll-load.lisp")
(ll-load "ll-util.lisp")
(ll-load "ll-cache.lisp")
(defparameter *KEYWORD-PREDS* '(
=
BEFORE
AT-ABOUT
AFTER
IMPINGES-ON
ORIENTS
DURING
PERTAIN-TO
AT-OR-BEFORE
CONSEC
CAUSE-OF
POSTCOND-OF
PRECOND-OF
SAME-TIME
RIGHT-AFTER
STRICTLY-BEFORE
CERTAIN-TO-DEGREE
NECESSARY-TO-DEGREE
HAS-EFFECT
MEMBER-OF
SMALLER-THAN
))
; TODO: give these extensions
(defparameter *KEYWORDS* (append *KEYWORD-PREDS*
'(
K
KA
KE
KJ
THAT
THT
ANS-TO
NOT
OR
AND
IF
ATTR
NN
PASV
PLUR
PERF
PROG
N+PREDS
L
:R
IND
ADV
SET-OF
)))
(defparameter *KEYWORDS-MAP* (make-hash-table :test #'equal))
(loop for kw in *KEYWORDS*
do (setf (gethash kw *KEYWORDS-MAP*) t)
)
(ldefun has-ext? (x e)
(ll-cache #'u-has-ext? (list x e) 100 nil)
)
(ldefun u-has-ext? (x e)
(let (
(strx (if (symbolp x) (string x) nil))
)
(and
(symbolp x)
(stringp e)
(>= (length strx) (length e))
(equal
e
(subseq
strx
(- (length strx) (length e))
(length strx)
)
)
)
)
)
(ldefun remove-ext (x e)
(if (not (has-ext? x e))
x
;else
(intern (subseq
(string x)
0
(- (length (string x)) (length e))
))
)
)
(ldefun lex-attr-pred? (x)
(or
(has-ext? x ".NN")
(has-ext? x ".ATTR")
)
)
(ldefun lex-removable-for-english? (x)
(member x '(
IND
ADV
ADV-A
ADV-E
ADV-F
ADV-S
KE
K
KJ
) :test #'equal)
)
(ldefun lex-verb? (x)
(has-ext? x ".V")
)
(ldefun retag-as (x ext)
(let ((pre (car (split-str (string x) "."))))
(intern (format nil "~a.~a" pre (string ext)))
)
)
(ldefun lex-metapred? (x)
(has-ext? x ".?")
)
(ldefun lex-var? (x)
(varp x)
)
(ldefun exc-varp (s)
(and
(symbolp s)
(> (length (string s)) 1)
(equal "!" (subseq (string s) 0 1))
)
)
(ldefun lex-gensym? (x)
(and
(symbolp x)
(has-prefix? (string x) "G")
(is-num-str? (remove-prefix (string x) "G"))
)
)
(ldefun lex-naked-var? (x)
(and
(symbolp x)
(not (special-str x))
(alphanum-str? (string x))
)
)
(ldefun lex-sk-fn? (x)
(has-ext? x "<-")
)
(ldefun lex-fn? (x)
(or
(has-ext? x ".F")
(lex-sk-fn? x)
)
)
(ldefun lex-number? (x)
(numberp x)
)
(ldefun lex-noun? (x)
(has-ext? x ".N")
)
(ldefun lex-p? (x)
(has-ext? x ".P")
)
(ldefun lex-ps? (x)
(has-ext? x ".PS")
)
(ldefun lex-pred? (x)
(has-ext? x ".PR")
)
(ldefun lex-p-arg? (x)
(has-ext? x ".P-ARG")
)
(ldefun lex-pronoun? (x)
(has-ext? x ".PRO")
)
(ldefun lex-name? (x)
(has-ext? x ".NAME")
)
(ldefun lex-skolem? (x)
(has-ext? x ".SK")
)
(ldefun lex-modal? (x)
(or
(has-ext? x ".AUX-V")
(has-ext? x ".AUX")
(has-ext? x ".MD")
)
)
(ldefun lex-adj? (x)
(has-ext? x ".A")
)
(ldefun lex-sentprep? (x)
(has-ext? x ".PS")
)
(ldefun lex-det? (x)
(or
(has-ext? x ".D")
(has-ext? x ".DET")
)
)
(ldefun lex-adv-a? (x)
(has-ext? x ".ADV-A")
)
(ldefun lex-adv-e? (x)
(has-ext? x ".ADV-E")
)
(ldefun lex-coord? (x)
(has-ext? x ".CC")
)
(ldefun lex-adv-f? (x)
(has-ext? x ".ADV-F")
)
(ldefun lex-adv? (x)
(or
(has-ext? x ".ADV")
(has-ext? x ".PRT")
(lex-adv-a? x)
(lex-adv-e? x)
(lex-adv-f? x)
)
)
(ldefun lex-speech? (x)
(or
(has-ext? x ".X")
)
)
(ldefun lex-const? (x)
(or
(lex-name? x)
(lex-pronoun? x)
(lex-skolem? x)
(and
(not (member x *KEYWORDS* :test #'equal))
(symbolp x)
(not (keywordp x))
(alphanum-str? (string x))
)
)
)
(ldefun lex-ent? (x)
(and
(symbolp x)
(alphanum-str? (string x))
)
)
(ldefun lex-ep-var? (x)
(and
(symbolp x)
(has-prefix? (string x) "E")
(> (length (string x)) 1)
(is-num-str? (subseq (string x) 1 (length (string x))))
)
)
(ldefun lex-schema-ep-var? (x)
(and
(has-prefix? (string x) "?E")
(if (> (length (string x)) 2)
; then
(is-num-str? (subseq (string x) 2 (length (string x))))
; else
t)
)
)
(ldefun lex-goal-var? (x)
(and
(has-prefix? (string x) "?G")
(if (> (length (string x)) 2)
; then
(is-num-str? (subseq (string x) 2 (length (string x))))
; else
t)
)
)
(ldefun lex-schema-ep-ref? (x)
(or
(lex-schema-ep-var? x)
(and
(symbolp x)
(has-suffix? (string x) ".SK")
(lex-ep-var? (intern (remove-suffix (string x) ".SK")))
)
)
)
(ldefun lex-ep-ref? (x)
(or
(lex-ep-var? x)
(and
(symbolp x)
(has-suffix? (string x) ".SK")
(lex-ep-var? (intern (remove-suffix (string x) ".SK")))
)
)
)
(ldefun lex-ep-const? (x)
(and
(symbolp x)
(has-suffix? (string x) ".SK")
(lex-ep-var? (intern (remove-suffix (string x) ".SK")))
)
)
; Equal-to-value predicate generator
(ldefun id? (e)
(list 'id? e)
)
(setf id-pred-counter 0)
(setf id-pred-map (make-hash-table :test #'equal))
(setf id-pred-rev-map (make-hash-table :test #'equal))
(ldefun old-id? (e)
(progn
(if (null (gethash e id-pred-map))
(progn
(setf (gethash e id-pred-map) (+ 1 id-pred-counter))
(setf (gethash (+ 1 id-pred-counter) id-pred-rev-map) e)
(setf id-pred-counter (+ 1 id-pred-counter))
)
)
(setf (fdefinition (intern (format nil "ID-PRED-~d" (gethash e id-pred-map))))
(lambda (x) (equal x e))
)
(intern (format nil "ID-PRED-~d" (gethash e id-pred-map)))
)
)
(ldefun uncached-id? (e)
(block outer
(setf id-pred-name (intern (format nil "ID-PRED-~d" id-pred-counter)))
(setf id-pred-counter (+ 1 id-pred-counter))
(setf (fdefinition id-pred-name) (lambda (x) (equal x e)))
)
)
; Use to short-circuit evaluation
(ldefun any? (x)
t
)
(ldefun plus (pred)
(if (plussed? pred)
nil
; else
(intern (format nil "~s+" pred))
)
)
(ldefun star (pred)
(if (starred? pred)
nil
; else
; (intern (format nil "~s*" pred))
(intern (concatenate 'string (string pred) "*"))
)
)
(ldefun plussed? (pred)
(and
(has-ext? pred "+")
)
)
(ldefun starred? (pred)
(and
(has-ext? pred "*")
)
)
(ldefun unplus (pluspred)
(if (not (plussed? pluspred))
nil
;else
(remove-ext pluspred "+")
)
)
(ldefun unstar (starpred)
(if (not (starred? starpred))
nil
;else
(remove-ext starpred "*")
)
)
(setf pred-cache (make-hash-table :test #'equal))
(ldefun cached-funcall (cache func arg)
(block outer
; special case for 'id'
; (if (listp func) (format t "func list ~s~%" func))
(if (and (listp func) (equal 2 (length func)) (equal 'id? (car func)))
; then
(progn
; (format t "direct equality comparison for ~s and ~s~%" (second func) arg)
(return-from outer (equal (second func) arg))
)
)
; (return-from outer (funcall func arg))
(if (null (gethash (list func arg) cache))
(setf (gethash (list func arg) cache)
(funcall func arg))
)
(return-from outer (gethash (list func arg) cache))
)
)
(ldefun id-pred-list? (x)
(and
(listp x)
(equal 2 (length x))
(equal 'id? (car x))
)
)
; recursive multi-predicate application
(ldefun mp (x preds)
(ll-cache #'u-mp (list x preds) 100 nil)
)
(ldefun u-mp (x preds)
(block outer
(if (and (null x) (null preds))
(return-from outer t)
)
(if (null preds)
; no preds left to consume values
(return-from outer nil)
)
; even if we're out of tokens, we can still remove a starred
; pred and recurse
(if (and (null x) (starred? (car preds)))
(return-from outer
(mp x (cdr preds))
)
)
; But if we're not out of tokens and the top pred isn't
; starred, we're toast.
(if (null x)
; (return-from outer (explain-nil "~s ~s~%" x preds))
(return-from outer nil)
)
(if (not (listp x))
; someone passed a naked atom into an MP in a desperate
; attempt to find a match; nothing to see here
(return-from outer nil)
)
(if (not (listp preds))
; this really shouldn't happen; it's probably the programmer's fault
; haha, never mind, it can happen legitimately now that there's tree recursion
(progn
; (format t "why did you pass non-list pred ~s with pattern ~s into mp?~%" preds x)
(return-from outer nil)
)
)
; check to see if we have a nested list of predicates; if we do, we'll try to tree-recurse
(if (and (listp (car preds))
(not (id-pred-list? (car preds)))
)
; tree-recurse with the head token.
; if it's not also a list, the recursive case will handle that
; (as well as consuming any stars from this list, on the offchance)
(return-from outer (mp (car x) (car preds)))
)
; (format t "checking tok list ~s~%against pred list ~s~%" x preds)
; if the first pred is plussed, test the head token as always
(if (plussed? (car preds))
(if (cached-funcall pred-cache (unplus (car preds)) (car x))
; the head token passes, so star the pred and recurse
(progn ;(format t "passes~%")
(return-from outer
(mp
(cdr x)
(append
(list (star (unplus (car preds))))
(cdr preds)
)
)
)
)
; the head token didn't pass; no match
(progn ;(format t "didn't pass~%")
(return-from outer nil)
)
)
)
; if the first pred is starred, test the head token as always
(if (starred? (car preds))
(if (cached-funcall pred-cache (unstar (car preds)) (car x))
; the head token passes, so leave the star pred and recurse
(return-from outer (mp (cdr x) preds))
; the head token didn't pass; remove the star, but continue on
(return-from outer (mp x (cdr preds)))
)
)
; if the first pred isn't plussed or starred, just do what comes naturally
(if (cached-funcall pred-cache (car preds) (car x))
; the head token passes; recurse
(return-from outer (mp (cdr x) (cdr preds)))
; the head token didn't pass; no match
(return-from outer nil)
)
)
)
(ldefun ent-list? (x)
(or
(lex-ent? x)
(and
(listp x)
(loop for e in x always (lex-ent? e))
)
)
)