2014年10月21日火曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の4(超言語的抽象)、4.3(Schemeの変形 - 非決定性計算)、4.3.3(非決定性プログラムの例)、自然言語の構文解析、問題 4.49.を解いてみる。

その他参考書籍

問題 4.49.

コード(BBEdit, Emacs)

sample49.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

 ;; random-integer手続きのため必要
(use srfi-27)

;; 形容詞を追加
(define nouns '(noun cat))

(define verbs '(verb run seats))

(define articles '(article the))

(define prepositions '(prep for to in by with))

(define adjectives '(adj pretty))

(define (parse-sentence)
  (list 'sentence)
  (parse-noun-phrase)
  (parse-verb-phrase))

(define (parse-article-phrase)
  (define (maybe-extend article-phrase)
    (amb article-phrase
         (maybe-extend (list 'article-phrase
                             article-phrase
                             (parse-word adjectives)))))
  (maybe-extend (parse-word articles)))

(define (parse-simple-noun-phrase)
  (list 'simple-noun-phrase
        (parse-article-phrase)
        (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)))

(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-prepositional-phrase)
  (list 'prep-phrase
        (parse-word prepositions)
        (parse-noun-phrase)))
                             
(define *unparsed* '())

;; 問題 4.49
(define (parse-word word-list)
  (set! *unparsed* '())
  (list (car word-list)
        (list-ref (cdr word-list)
                  (random-integer (- (length word-list)
                                     2)))))

(define (parse input)
  (set! *unparsed* input)
  (let ((sent (parse-sentence)))
    (require (null? *unparsed*))
    sent))

;; 入力 (the cat eats)
(parse '(the cat eats))

;; *unparsed* '(the cat eats)
(parse-sentence)

(list 'sentence
      (parse-noun-phrase)
      (parse-verb-phrase))

(parse-noun-phrase)

(maybe-extend (simple-noun-phrase))

(simple-noun-phrase)

(list 'simple-noun-phrase
      (parse-article-phrase)
      (parse-word nouns))

(parse-article-phrase)

(maybe-extend (parse-word articles))

(parse-word articles)

'(article the)

(parse-word nouns)

'(noun cat)

(maybe-extend '(simple-noun-phrase (article the) (noun cat)))

'(simple-noun-phrase (article the) (noun cat))

(parse-verb-phrase)

(maybe-extend (parse-word verbs))

(parse-word  verbs)

;; *unparsed* '()
'(verb run)

'(sentence (simple-noun-phrase (article the) (noun cat))
           (verb run))

try-again

(maybe-extend (list 'verb-phrase
                    '(verb run)
                    (parse-prepositional-phrase)))

(parse-prepositional-phrase)

(list 'prep-phrase
      (parse-word prepositions)
      (parse-noun-phrase))

(parse-word prepotions)

'(prep for)

(parse-noun-phrase)

(maybe-extend (parse-simple-noun-phrase))

(parse-simple-noun-phrase)

(list 'simple-noun-phrase
      (parse-article-phrase)
      (parse-word nouns))

(parse-article-phrase)

(maybe-extend (parse-word articles))

(parse-word articles)

'(article the)

(parse-word nouns)

'(noun cat)

'(prep-phrase (prep for)
              (simple-noun-phrase (article the)
                                  (noun cat)))

'(verb-phrzse (verb run)
              (prep-phrase (prep for)
                           (simple-noun-phrase (article the)
                                               (noun cat))))

'(sentence
  (simple-noun-phrase (article the) (noun cat))
  (verb-phrase (verb run)
               (prep-phrase (prep for)
                            (simple-noun-phrase (article the)
                                                (noun cat)))))

0 コメント:

コメントを投稿