In parse-word
, remove the restriction that the first item of *unparsed*
is a member of word-list will succeed for any non-empty phrase. In order to return a valid word, just make found-word
a random item from word-list
.
(interpret '(define (parse-word word-list) (require (not (null? *unparsed*))) (let ((found-word (random-word (cdr word-list)))) (set! *unparsed* (cdr *unparsed*)) (list (car word-list) found-word)))) (interpret '(define (random-word word-list) (list-ref word-list (random (length word-list)))))
Note the original program generates all possible sentence structures for the given input and so this version only generates a random example sentence for each of those structures. It doesn’t generate all possible sentences that have the same structure as the input.
;;; Amb-Eval input: (parse '(the professor lectures to the cat in the class)) ;;; Starting a new problem ;;; Amb-Eval value: (sentence (simple-noun-phrase (article a) (noun professor)) (verb-phrase (verb-phrase (verb eats) (prep-phrase (prep by) (simple-noun-phrase (article the) (noun cat)))) (prep-phrase (prep to) (simple-noun-phrase (article a) (noun class))))) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (sentence (simple-noun-phrase (article a) (noun professor)) (verb-phrase (verb eats) (prep-phrase (prep by) (noun-phrase (simple-noun-phrase (article the) (noun cat)) (prep-phrase (prep in) (simple-noun-phrase (article the) (noun professor))))))) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (sentence (noun-phrase (simple-noun-phrase (article a) (noun professor)) (prep-phrase (prep in) (simple-noun-phrase (article a) (noun professor)))) (verb-phrase (verb sleeps) (prep-phrase (prep with) (simple-noun-phrase (article a) (noun cat))))) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (sentence (noun-phrase (noun-phrase (simple-noun-phrase (article a) (noun professor)) (prep-phrase (prep in) (simple-noun-phrase (article a) (noun professor)))) (prep-phrase (prep for) (simple-noun-phrase (article a) (noun class)))) (verb sleeps)) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (sentence (noun-phrase (simple-noun-phrase (article a) (noun professor)) (prep-phrase (prep in) (noun-phrase (simple-noun-phrase (article a) (noun professor)) (prep-phrase (prep to) (simple-noun-phrase (article the) (noun class)))))) (verb sleeps)) ;;; Amb-Eval input: try-again ;;; There are no more values of (parse '(the professor lectures to the cat in the class))
Update:
Having looked at the Exercise 4.50, I think there is another answer, which is probably what the authors were looking for:
(interpret '(define (remove-item item items) (cond ((null? items) null) ((equal? item (car items)) (cdr items)) (else (cons (car items) (remove-item item (cdr items))))))) (interpret '(define (random-element-of items) (require (not (null? items))) (let ((item (list-ref items (random (length items))))) (amb item (random-element-of (remove-item item items)))))) (interpret '(define (parse-word word-list) (require (not (null? *unparsed*))) (let ((found-word (random-element-of (cdr word-list)))) (set! *unparsed* (cdr *unparsed*)) (list (car word-list) found-word))))
This gives a more comprehensive result than last time as all possible sentences that match all possible sentence structures derived from the given sentence.