1 / 10

# AI: Exercise 5

AI: Exercise 5. Simple Lisp Programs . References: http://www-itolab.ics.nitech.ac.jp/LISP/text-contents.html http://grimpeur.tamu.edu/~colin/lp/lp.html http://www-2.cs.cmu.edu/Groups/AI/html/cltl/clm/#R http://www.cs.utsa.edu/research/AI/cltl/clm/clm.html.

Télécharger la présentation

## AI: Exercise 5

E N D

### Presentation Transcript

1. AI: Exercise 5 Simple Lisp Programs References: http://www-itolab.ics.nitech.ac.jp/LISP/text-contents.html http://grimpeur.tamu.edu/~colin/lp/lp.html http://www-2.cs.cmu.edu/Groups/AI/html/cltl/clm/#R http://www.cs.utsa.edu/research/AI/cltl/clm/clm.html

2. e.g. 3: A tree structure of nodes for data base A data base called *db*. The data base is organized into a tree structure of nodes. Each node has three fields: the name of the object it represents, a node to go to if the answer is yes, and a node for when the answer is no. We traverse the nodes until we either get an “it” reply or have to give up. (defstruct node name (yes nil) (no nil)) NODE (defvar *db* (make-node :name 'animal :yes (make-node :name 'mammal) :no (make-node :name 'vegetable :no (make-node :name 'mineral)))) *DB*

3. (defun give-up () (format t "~&I give up - what is it?") (make-node :name (first (read)))) GIVE-UP (defun questions (&optional (node *db*)) (format t "~&Is it a ~a? " (node-name node)) (case (first (read)) ((y yes) (if (not (null (node-yes node))) (questions (node-yes node)) (setf (node-yes node) (give-up)))) ((n no) (if (not (null (node-no node))) (questions (node-no node)) (setf (node-no node) (give-up)))) (it 'aha!) (t (format t "reply with YES, NO, or IT if I have guessed it.") (questions node)))) QUESTIONS

4. Execution results: (questions) Is it a ANIMAL? (yes) Is it a MAMMAL? (yes) I give up - what is it? (whale) #S( NODE :NAME WHALE :YES NIL :NO NIL ) (questions) Is it a ANIMAL? (no) Is it a VEGETABLE? (no) Is it a MINERAL? (no) I give up - what is it? (fruit) #S( NODE :NAME FRUIT :YES NIL :NO NIL ) (questions) Is it a ANIMAL? (no) Is it a VEGETABLE? (no) Is it a MINERAL? (no) Is it a FRUIT? (it) AHA!

5. e.g. 4: A simple lisp program of generating English sentences (defun random-elt (choices) "Choose an element from a list at random" (elt choices (random (length choices)))) RANDOM-ELT (defun one-of (set) "pick one element of set and make a list of it" (list (random-elt set))) ONE-OF (defun Verb () (one-of '(hit took saw liked))) VERB (defun Noun () (one-of '(man ball woman table))) NOUN (defun Article () (one-of '(the a))) ARTICLE

6. (defun verb-phrase () (append (Verb) (noun-phrase) )) VERB-PHRASE (defun noun-phrase () (append (Article) (Noun))) NOUN-PHRASE (defun sentence () (append (noun-phrase ) (verb-phrase))) SENTENCE (sentence) (THE MAN SAW THE TABLE) (sentence) (A TABLE SAW THE BALL) (sentence) (A MAN SAW THE BALL)

7. e.g. 5: A rule-based solution of generating English sentences (defparameter *simple-grammar* '((sentence -> (noun-phrase verb-phrase)) (noun-phrase -> (ar Noun)) (verb-phrase -> (Verb noun-phrase)) (ar -> the a) (Noun -> man ball woman table) (Verb -> hit took saw liked)) "A grammar for a trivial subset of English.") *SIMPLE-GRAMMAR* (defvar *grammar* *simple-grammar* "The grammar used by generate. Initially, this is *simple-grammar*, but we can switch to other grammars.") *GRAMMAR* (defun rule-lhs (rule) "The left-hand side of a rule" (first rule)) RULE-LHS

8. (defun rule-rhs (rule) "The right-hand side of a rule" (rest (rest rule))) RULE-RHS (defun rewrite(category) "return a list of the possible rewritees for this category" (rule-rhs (assoc category *grammar*))) REWRITE (defun random-elt (choices) "choose an element from a list at random" (elt choices (random (length choices)))) RANDOM-ELT (defun mappend (fn the-list) "Apply fn to each element of list and append the result" (apply #'append (mapcar fn the-list))) MAPPEND

9. (defun generate (phrase) "Generate a random sentence or a phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((rewrite phrase) (generate (random-elt (rewrite phrase)))) (t (list phrase)))) GENERATE (generate 'sentence) (THE WOMAN LIKED A TABLE) (generate 'noun-phrase) (A WOMAN) (generate 'verb-phrase) (TOOK A WOMAN) (generate 'Verb) (HIT) (generate 'Noun) (BALL) (generate 'Article) (THE)

10. Exercise Problems If you have time, try to do the following exercise problems. • Modify e.g.3 and make a data base of English words: verb (intransitive verb, transitive verb), noun, article, adjective, adverb, preposition, …, etc. • Write a version of generate function that explicitly differentiates between terminal symbols (those with no rewrite rules) and non terminal symbols. • Defining a new grammar that includes adjectives, prepositional phrases, proper names, and pronouns.

More Related