|
| 1 | +;;; bottom-up-parser.scm |
| 2 | + |
| 3 | +;;; PAIP 19.1 |
| 4 | + |
| 5 | +(define grammar1 |
| 6 | + '((s (np vp) (aux np vp) (vp)) |
| 7 | + (np (proper-noun) (det nom)) |
| 8 | + (nom (noun) (noun nom) (nom pp)) |
| 9 | + (vp (verb) (verb np)) |
| 10 | + (pp (prep np)) |
| 11 | + (det "that" "this" "a") |
| 12 | + (noun "book" "flight" "meal" "money") |
| 13 | + (verb "book" "include" "prefer") |
| 14 | + (aux "does") |
| 15 | + (prep "from" "to" "on") |
| 16 | + (proper-noun "Houston" "TWA") |
| 17 | + )) |
| 18 | + |
| 19 | +(define grammar2 |
| 20 | + `((s (np vp)) |
| 21 | + (np (art noun)) |
| 22 | + (vp (verb np)) |
| 23 | + (art "the" "a") |
| 24 | + (noun "man" "ball" "woman" "table" "noun" "verb") |
| 25 | + (verb "hit" "took" "saw" "liked") |
| 26 | + )) |
| 27 | + |
| 28 | +(define (expand-grammar grammar) |
| 29 | + ;; expand grammar shorthand: ((head body1 body2 ...) ...) |
| 30 | + ;; to ((head body1) (head body2) ...) |
| 31 | + (define (process rule) |
| 32 | + (let ((head (car rule)) |
| 33 | + (bodies (cdr rule))) |
| 34 | + (map (lambda (body) |
| 35 | + (if (string? body) |
| 36 | + (list head body) |
| 37 | + (cons head body))) |
| 38 | + bodies))) |
| 39 | + (append-map process grammar)) |
| 40 | + |
| 41 | + |
| 42 | +(define-unit-test 'expand-grammar |
| 43 | + (check (expand-grammar grammar1) |
| 44 | + => '((s np vp) (s aux np vp) (s vp) |
| 45 | + (np proper-noun) (np det nom) |
| 46 | + (nom noun) (nom noun nom) (nom nom pp) |
| 47 | + (vp verb) (vp verb np) (pp prep np) |
| 48 | + (det "that") (det "this") (det "a") |
| 49 | + (noun "book") (noun "flight") (noun "meal") (noun "money") |
| 50 | + (verb "book") (verb "include") (verb "prefer") |
| 51 | + (aux "does") (prep "from") (prep "to") (prep "on") |
| 52 | + (proper-noun "Houston") (proper-noun "TWA"))) |
| 53 | + (check (expand-grammar grammar2) |
| 54 | + => '((s np vp) (np art noun) (vp verb np) |
| 55 | + (art "the") (art "a") |
| 56 | + (noun "man") (noun "ball") (noun "woman") (noun "table") |
| 57 | + (noun "noun") (noun "verb") |
| 58 | + (verb "hit") (verb "took") (verb "saw") (verb "liked")))) |
| 59 | + |
| 60 | + |
| 61 | +;; rule = (head cat1 ...) or (head "word") |
| 62 | +(define (lexical? rule) (string? (cadr rule))) |
| 63 | +(define (head rule) (car rule)) |
| 64 | +(define (body rule) (cdr rule)) |
| 65 | +(define (body1 rule) (cadr rule)) |
| 66 | +(define (body2 rule) (caddr rule)) |
| 67 | +(define (word rule) (cadr rule)) |
| 68 | + |
| 69 | +(define (create-lexicon grammar) |
| 70 | + ;; expanded grammar |
| 71 | + ;; lexicon is (("word" (cat1 ...)) ...) |
| 72 | + (partition lexical? grammar |
| 73 | + (lambda (rules _) |
| 74 | + (let ((f (lambda (rule lexicon) |
| 75 | + (alist-add-value (word rule) (head rule) lexicon)))) |
| 76 | + (reverse (fold f '() rules)))))) |
| 77 | + |
| 78 | + |
| 79 | +(define-unit-test 'create-lexicon |
| 80 | + (check (create-lexicon (expand-grammar grammar1)) |
| 81 | + => '(("that" det) ("this" det) ("a" det) |
| 82 | + ("book" verb noun) ("flight" noun) ("meal" noun) ("money" noun) |
| 83 | + ("include" verb) ("prefer" verb) ("does" aux) |
| 84 | + ("from" prep) ("to" prep) ("on" prep) |
| 85 | + ("Houston" proper-noun) ("TWA" proper-noun)))) |
| 86 | + |
| 87 | +(define grammar (expand-grammar grammar2)) |
| 88 | +(define lexicon (create-lexicon grammar)) |
| 89 | + |
| 90 | +;; '("the" "man" "took" "the" "ball") |
| 91 | +;; "the" '("man" "took" "the" "ball") |
| 92 | +;; "the" '(art) '("man" "took" "the" "ball") |
| 93 | +;; "the" 'art '("man" "took" "the" "ball") |
| 94 | +;; '(art "the") '(np art noun) '("man" "took" "the" "ball") |
| 95 | +;; "man" '("took" "the" "ball") noun? |
| 96 | +;; "man" '(noun) '("took" "the" "ball") noun? |
| 97 | +;; '((art "the") (noun "man")) '((s np vp)) '("took" "the" "ball") |
| 98 | +;; '((art "the") (noun "man")) '(s np vp) '("took" "the" "ball") |
| 99 | +;; "took" vp? '((art "the") (noun "man")) '(s np vp) '("the" "ball") |
| 100 | + |
| 101 | +(define (parse words) |
| 102 | + (define (parsem nt) |
| 103 | + (parse/main 's (list nt (car words)) (cdr words))) |
| 104 | + (let* ((possible-nts (alist-lookup (car words) lexicon)) |
| 105 | + (parses (map parsem possible-nts))) |
| 106 | + (list-remove '() parses)) |
| 107 | + ) |
| 108 | + |
| 109 | +(define (parse/main looking-for tree words) #f) |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +(define (bottom-up-parse words) |
| 115 | + (map parse-tree (complete-parses (parse words)))) |
| 116 | + |
| 117 | +(define (complete-parses parses) |
| 118 | + #f) |
| 119 | + |
| 120 | +(define (parse words) |
| 121 | + (if (null? words) |
| 122 | + '() |
| 123 | + (let ((nts (alist-lookup (car words) lexicon))) |
| 124 | + (map (lambda (nt) |
| 125 | + (extend-parse nt (list (car words)) (cdr words) '())) |
| 126 | + nts)))) |
| 127 | + |
| 128 | +(define (parse-tree l) #f) |
| 129 | + |
| 130 | +(define (extend-parse h b rem needed) |
| 131 | + (if (null? needed) |
| 132 | + (parse-up h b rem) |
| 133 | + (parse-right h b rem needed))) |
| 134 | + |
| 135 | +(define (parse-up h b rem) |
| 136 | + (let ((tree (list h b)) |
| 137 | + (rules (find-rules/left-body h))) |
| 138 | + (cons 1;tree+rem |
| 139 | + (map (lambda (rule) |
| 140 | + (extend-parse (head rule) (list tree) rem (cddr rule))) |
| 141 | + rules)))) |
| 142 | + |
| 143 | +(define (parse-right h b rem needed) |
| 144 | + (map (lambda (t) |
| 145 | + (if (eq? (car t) (car needed)) |
| 146 | + (extend-parse h (append b t) rem (cdr needed)) |
| 147 | + '())) |
| 148 | + (parse rem))) |
| 149 | + |
| 150 | +(define (find-rules/left-body h grammar) |
| 151 | + (filter (lambda (rule) (eq? h (body1 rule))) grammar)) |
| 152 | + |
| 153 | + |
| 154 | +(define (sr-parse words) |
| 155 | + (let loop ((stack '()) (words words)) |
| 156 | + (cond ((null? words) |
| 157 | + #f)))) |
| 158 | + |
| 159 | +;;; the dog saw a man in the park |
| 160 | +;;; the | dog saw a man in the park |
| 161 | +;;; (det the) | dog saw a man in the park |
| 162 | +;;; (det the) dog | saw a man in the park |
| 163 | +;;; (det the) (n dog) | saw a man in the park |
| 164 | +;;; (np (det the) (n dog)) | saw a man in the park |
| 165 | +;;; (np (det the) (n dog)) saw | a man in the park |
| 166 | +;;; (np (det the) (n dog)) (v saw) | a man in the park |
| 167 | +;;; (np (det the) (n dog)) (v saw) a | man in the park |
| 168 | +;;; (np (det the) (n dog)) (v saw) (det a) | man in the park |
| 169 | +;;; (np (det the) (n dog)) (v saw) (det a) man | in the park |
| 170 | +;;; (np (det the) (n dog)) (v saw) (det a) (n man) | in the park |
| 171 | +;;; (np (det the) (n dog)) (v saw) (np (det a) (n man)) | in the park |
| 172 | +;;; (np (det the) (n dog)) (vp (v saw) (np (det a) (n man))) | in the park |
| 173 | + |
| 174 | + |
| 175 | +(unit-tests 'expand-grammar 'create-lexicon) |
0 commit comments