Hatena::Groupcadr

bulbのLisp日記 RSSフィード

2008-01-08

[][] 21:22 
 - bulbのLisp日記 を含むブックマーク

前の関数generateは、(rewrites phrase) を2回使用していて冗長なため、

次のように変更。

ちなみに let を使用しますよ。

(defun generate (phrase)
  "Generate a random sentence or phrase"
  (if (listp phrase)
      (mappend #'generate phrase)
      (let ((choices (rewrites phrase)))
	(if (null choices)
	    (list phrase)
	    (generate (random-elt choices))))))
#|
CL-USER> (generate 'sentence)
(A MAN LIKED THE TABLE)
CL-USER> (generate 'sentence)
(THE WOMAN TOOK THE BALL)
CL-USER> (generate 'sentence)
(A WOMAN SAW THE TABLE)
|#

setf を使用して代入なんかしちゃだめ。

[][][]2.1 21:29 2.1 - bulbのLisp日記 を含むブックマーク はてなブックマーク - 2.1 - bulbのLisp日記

generate を cond を使用して書きます。

;; Exercise 2.1

(defun generate (phrase)
  (let ((choices nil))
    (cond ((listp phrase)
	   (mappend #'generate phrase))
	  ((setf choices (rewrites phrase))
	   (generate (random-elt choices)))
	  (t
	   (list phrase)))))
#|
CL-USER> (generate 'sentence)
(THE WOMAN HIT A TABLE)
CL-USER> (generate 'sentence)
(THE TABLE LIKED A MAN)
|#



[][][]2.2 22:45 2.2 - bulbのLisp日記 を含むブックマーク はてなブックマーク - 2.2 - bulbのLisp日記

いきなり意味がわからなかったため答えを見る。

あぁ、明示的に終端と非終端を区別せよか。

答えは以下。

(defun generate (phrase)
  (cond ((listp phrase)
	 (mappend #'generate phrase))
	((non-terminal-p phrase)
	 (generate (random-elt (rewrites phrase))))
	(t
	 (list phrase))))

(defun non-terminal-p (category)
  (not (null (rewrites category))))

#|
CL-USER> (generate 'sentence)
(THE WOMAN TOOK A WOMAN)
CL-USER> (generate 'sentence)
(A WOMAN HIT THE TABLE)
|#



[][]2.5 22:46 2.5 - bulbのLisp日記 を含むブックマーク はてなブックマーク - 2.5 - bulbのLisp日記

grammar を増やしますよ。

(defparameter *bigger-grammar*
  '((sentence -> (noun-phrase verb-phrase))
    (noun-phrase -> (Article Adj* Noun PP*) (Name) (Pronoun))
    (verb-phrase -> (Verb noun-phrase PP*))
    (PP* -> () (PP PP*))
    (Adj* -> () (Adj Adj*))
    (PP -> (Prep noun-phrase))
    (Prep -> to in by with on)
    (Adj -> big little bule green adiabatic)
    (Article -> the a)
    (Name -> Peter Paul Seibel Guy Steel)
    (Noun -> man ball woman table)
    (Verb -> hit took saw liked)
    (Pronoun -> he she it these those that)))

(setf *grammar* *bigger-grammar*)

#|
CL-USER> (generate 'sentence)
(THE ADIABATIC LITTLE GREEN WOMAN SAW THE MAN ON HE)
CL-USER> (generate 'sentence)
(THE MAN TO A GREEN TABLE WITH STEEL ON A LITTLE MAN TO THE TABLE BY SEIBEL IN
     THAT TOOK THE ADIABATIC LITTLE TABLE IN SHE WITH A BULE GREEN WOMAN TO GUY
     BY PAUL ON STEEL)
CL-USER> (generate 'sentence)
(PETER TOOK SHE)
|#


英文の結果がまだ変ですね。

でもこれがAIへの入り口?