Add exercise 4.45
This commit is contained in:
parent
78bfc38b81
commit
cb17f567b8
|
@ -0,0 +1,153 @@
|
|||
(define nouns '(noun student professor cat class))
|
||||
|
||||
(define verbs '(verb studies lectures eats sleeps))
|
||||
|
||||
(define articles '(article the a))
|
||||
|
||||
;; output of parse
|
||||
'(sentence (noun-phrase (article the) (noun cat))
|
||||
(verb eats))
|
||||
|
||||
(define (parse-sentence)
|
||||
(list 'sentence
|
||||
(parse-noun-phrase)
|
||||
(parse-word verbs)))
|
||||
|
||||
(define (parse-noun-phrase)
|
||||
(list 'noun-phrase
|
||||
(parse-word articles)
|
||||
(parse-word nouns)))
|
||||
|
||||
(define (parse-word word-list)
|
||||
(require (not (null? *unparsed*)))
|
||||
(require (memq (car *unparsed*) (cdr word-list)))
|
||||
(let ((found-word (car *unparsed*)))
|
||||
(set! *unparsed* (cdr *unparsed*))
|
||||
(list (car word-list) found-word)))
|
||||
|
||||
(define *unparsed* '())
|
||||
|
||||
(define (parse input)
|
||||
(set! *unparsed* input)
|
||||
(let ((sent (parse-sentence)))
|
||||
(require (null? *unparsed*))
|
||||
sent))
|
||||
|
||||
|
||||
;: (parse '(the cat eats))
|
||||
;; output of parse
|
||||
'(sentence (noun-phrase (article the) (noun cat)) (verb eats))
|
||||
|
||||
(define prepositions '(prep for to in by with))
|
||||
|
||||
(define (parse-prepositional-phrase)
|
||||
(list 'prep-phrase
|
||||
(parse-word prepositions)
|
||||
(parse-noun-phrase)))
|
||||
|
||||
(define (parse-sentence)
|
||||
(list 'sentence
|
||||
(parse-noun-phrase)
|
||||
(parse-verb-phrase)))
|
||||
|
||||
(define (parse-verb-phrase)
|
||||
(define (maybe-extend verb-phrase)
|
||||
(amb verb-phrase
|
||||
(maybe-extend (list 'verb-phrase
|
||||
verb-phrase
|
||||
(parse-prepositional-phrase)))))
|
||||
(maybe-extend (parse-word verbs)))
|
||||
|
||||
(define (parse-simple-noun-phrase)
|
||||
(list 'simple-noun-phrase
|
||||
(parse-word articles)
|
||||
(parse-word nouns)))
|
||||
|
||||
(define (parse-noun-phrase)
|
||||
(define (maybe-extend noun-phrase)
|
||||
(amb noun-phrase
|
||||
(maybe-extend (list 'noun-phrase
|
||||
noun-phrase
|
||||
(parse-prepositional-phrase)))))
|
||||
(maybe-extend (parse-simple-noun-phrase)))
|
||||
|
||||
|
||||
;; Exercise 4.45: Output of (parse '(the professor lectures to the student in the class with the cat))
|
||||
|
||||
(sentence
|
||||
(simple-noun-phrase (article the) (noun professor))
|
||||
(verb-phrase
|
||||
(verb-phrase
|
||||
(verb-phrase (verb lectures)
|
||||
(prep-phrase (prep to)
|
||||
(simple-noun-phrase (article the) (noun student))))
|
||||
(prep-phrase (prep in) (simple-noun-phrase (article the) (noun class))))
|
||||
(prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))))
|
||||
|
||||
;; The cat is with the professor. The professor is in the class
|
||||
;; lecturing the student. The student may be elsewhere.
|
||||
|
||||
|
||||
(sentence
|
||||
(simple-noun-phrase (article the) (noun professor))
|
||||
(verb-phrase
|
||||
(verb-phrase
|
||||
(verb lectures)
|
||||
(prep-phrase (prep to)
|
||||
(simple-noun-phrase (article the) (noun student))))
|
||||
(prep-phrase (prep in)
|
||||
(noun-phrase (simple-noun-phrase (article the) (noun class))
|
||||
(prep-phrase (prep with)
|
||||
(simple-noun-phrase (article the) (noun cat)))))))
|
||||
|
||||
;; The cat is in the class in which the professor is lecturing the student
|
||||
|
||||
|
||||
(sentence
|
||||
(simple-noun-phrase (article the) (noun professor))
|
||||
(verb-phrase
|
||||
(verb-phrase
|
||||
(verb lectures)
|
||||
(prep-phrase (prep to)
|
||||
(noun-phrase (simple-noun-phrase (article the) (noun student))
|
||||
(prep-phrase (prep in)
|
||||
(simple-noun-phrase (article the)
|
||||
(noun class))))))
|
||||
(prep-phrase (prep with)
|
||||
(simple-noun-phrase (article the) (noun cat)))))
|
||||
|
||||
;; The student is in the class. The professor is with the cat and is
|
||||
;; lecturing to the student. The professor and cat may or may not be
|
||||
;; in the class.
|
||||
|
||||
|
||||
(sentence
|
||||
(simple-noun-phrase (article the) (noun professor))
|
||||
(verb-phrase
|
||||
(verb lectures)
|
||||
(prep-phrase (prep to)
|
||||
(noun-phrase
|
||||
(noun-phrase (simple-noun-phrase (article the) (noun student))
|
||||
(prep-phrase (prep in)
|
||||
(simple-noun-phrase (article the) (noun class))))
|
||||
(prep-phrase (prep with)
|
||||
(simple-noun-phrase (article the) (noun cat)))))))
|
||||
|
||||
;; The professor is lecturing the student. The student is in the
|
||||
;; class with the cat.
|
||||
|
||||
|
||||
(sentence
|
||||
(simple-noun-phrase (article the) (noun professor))
|
||||
(verb-phrase (verb lectures)
|
||||
(prep-phrase (prep to)
|
||||
(noun-phrase (simple-noun-phrase (article the) (noun student))
|
||||
(prep-phrase (prep in)
|
||||
(noun-phrase
|
||||
(simple-noun-phrase (article the)
|
||||
(noun class))
|
||||
(prep-phrase (prep with)
|
||||
(simple-noun-phrase
|
||||
(article the)
|
||||
(noun cat)))))))))
|
||||
;; The professor lectures to the student, who is in the class with the cat
|
Loading…
Reference in New Issue