home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
aijournl
/
ai_dec88.arc
/
EXPTBX.CDE
< prev
next >
Wrap
Text File
|
1988-05-04
|
6KB
|
259 lines
Code to accompany Expert's Toolbox for December 1988
(defnode s
(cat noun s2
(setr subj *)))
(defnode s2
(cat verb s3
(setr v *)))
(defnode s3
(pop `(sentence
(subject ,(getr subj))
(verb ,(getr v)))))
(setq paths nil)
(defmacro choose (&rest choices)
`(progn
,@(mapcar #'(lambda (c)
`(push #'(lambda () ,c) paths))
(reverse (cdr choices)))
,(car choices)))
(defun fail ()
(if paths
(funcall (pop paths))
'no-more-choices))
Listings begin here
(defun nilregs ()
`(()))
(defmacro getr (key regs)
`(let ((result (cdr (assq ',key (car ,regs)))))
(if (> (length result) 1)
result
(car result))))
(defmacro setr (key val regs)
`(cons (cons (cons ',key (list ,val))
(remove (assq ',key (car ,regs)) (car ,regs) :test #'equal))
(cdr ,regs)))
(defmacro pushr (key val regs)
`(cons (cons (cons ',key (cons ,val (cdr (assq ',key (car ,regs)))))
(remove (assq ',key (car ,regs)) (car ,regs) :test #'equal))
(cdr ,regs)))
Listing 2
(defun compile-arc (arc)
(apply (case (car arc)
(push #'compile-push)
(cat #'compile-cat)
(jump #'compile-jump)
(pop #'compile-pop))
(cdr arc)))
(defun compile-push (sub next &rest cmds)
`(,sub pos
(cons nil regs)
#'(lambda (* newpos regs)
(,next newpos ,(compile-cmds cmds) cont))))
(defun compile-cat (cat next &rest cmds)
`(if (= (length sentence) pos)
(fail)
(let ((* (nth pos sentence)))
(if (memq ',cat (types *))
(,next (1+ pos) ,(compile-cmds cmds) cont)
(fail)))))
(defun compile-jump (next &rest cmds)
`(,next pos ,(compile-cmds cmds) cont))
(defun compile-cmds (cmds)
(if (null cmds)
'regs
`(,@(car cmds) ,(compile-cmds (cdr cmds)))))
Listing 3
(defun compile-pop (expr)
`(let ((* (nth pos sentence)))
(funcall cont ,(fix-getrs expr) pos (cdr regs))))
(defun fix-getrs (expr)
(cond ((atom expr) expr)
((eq (car expr) 'getr)
(append expr '(regs)))
(t (mapcar #'fix-getrs expr))))
Listing 4
(defun parse (sent)
(setq sentence sent)
(setq paths nil)
(do ((retval (s 0 (nilregs) #'(lambda (expr pos regs)
(list pos expr)))
(fail)))
((eq retval 'no-more-choices))
(when (= (car retval) (length sent))
(terpri)
(pprint (cadr retval)))))
Listing 5
(defnode s
(push np s/subj
(setr mood 'decl)
(setr subj *))
(cat v v
(setr mood 'imp)
(setr subj '(np (pron you)))
(setr aux nil)
(setr v *)))
(defnode s/subj
(cat v v
(setr aux nil)
(setr v *)))
(defnode pivot
(cat v v
(setr v *)))
(defnode v
(pop `(s (mood ,(getr mood))
(subj ,(getr subj))
(vcl (aux ,(getr aux))
(v ,(getr v)))))
(push np s/obj
(setr obj *)))
(defnode s/obj
(pop `(s (mood ,(getr mood))
(subj ,(getr subj))
(vcl (aux ,(getr aux))
(v ,(getr v)))
(obj ,(getr obj)))))
Listing 6
(defnode np
(cat det np/det
(setr det *))
(jump np/det
(setr det nil))
(cat pron pron
(setr n *)))
(defnode pron
(pop `(np (pronoun ,(getr n)))))
(defnode np/det
(push mods np/mods
(setr mods *))
(jump np/mods
(setr mods nil)))
(defnode np/mods
(cat n np/n
(setr n *)))
(defnode np/n
(pop `(np (det ,(getr det))
(modifiers ,(getr mods))
(noun ,(getr n))))
(push pp/ np/pp
(setr pp *)))
(defnode np/pp
(pop `(np (det ,(getr det))
(modifiers ,(getr mods))
(noun ,(getr n))
(pp ,(getr pp)))))
Listing 7
(defnode pp/
(cat prep pp/prep
(setr prep *)))
(defnode pp/prep
(push np pp/np
(setr op *)))
(defnode pp/np
(pop `(pp (prep ,(getr prep))
(obj ,(getr op)))))
Listing 8
(defnode mods
(cat n mods/n
(setr mods *)))
(defnode mods/n
(cat n mods/n
(pushr mods *))
(pop `(n-group ,(getr mods))))
Listing 9
(defun types (wrd)
(case wrd
((do does did) '(aux v))
((time times) '(n v))
((fly flies) '(n v))
((like) '(v prep))
((liked likes) '(v))
((a an the) '(det))
((arrow arrows) '(n))
((i you he she him her it) '(pron))))
Listing 10
> (parse '(time flies like an arrow))
(S (MOOD DECL)
(SUBJ (NP (DET NIL)
(MODIFIERS (N-GROUP TIME))
(NOUN FLIES)))
(VCL (AUX NIL)
(V LIKE))
(OBJ (NP (DET AN)
(MODIFIERS NIL)
(NOUN ARROW))))
(S (MOOD IMP)
(SUBJ (NP (PRON YOU)))
(VCL (AUX NIL)
(V TIME))
(OBJ (NP (DET NIL)
(MODIFIERS NIL)
(NOUN FLIES)
(PP (PREP LIKE)
(OBJ (NP (DET AN)
(MODIFIERS NIL)
(NOUN ARROW)))))))
NIL
(OBJ (NP (DET AN)