
(deftest language 
  "Test the chart parser on some grammars."
  "First the simple E0 grammar from page 665."
  ((chart-parses '(I smell a stench) *E0*)
   '((S (NP (PRONOUN I)) 
        (VP (VP (VERB SMELL)) (NP (ARTICLE A) (NOUN STENCH))))))
  ((chart-parses '(the gold is in 2 2) *E0*)
   '((S (NP (ARTICLE THE) (NOUN GOLD)) 
        (VP (VP (VERB IS)) (PP (PREPOSITION IN) (NP (DIGIT 2) (DIGIT 2)))))))
  "Now the E1 grammar to show how pronoun case is handled."
  "It is grammatical to use 'I' as a subject, but not 'me'."
  ((chart-parses '(I shot the wumpus) *E1*)
   (renaming? * '((S ((NP SUBJECTIVE) ((PRONOUN SUBJECTIVE) I)) 
                     (VP (VERB SHOT) 
                         ((NP $CASE.10) (ARTICLE THE) (NOUN WUMPUS)))))))
   ((chart-parses '(Me shot the wumpus) *E1*)
    'NIL)
   "The E0 grammar allows anything (including 'me') as a subject:"
   ((chart-parses '(Me shot the wumpus) *E0*)
    '((S (NP (PRONOUN ME)) 
         (VP (VP (VERB SHOT)) (NP (ARTICLE THE) (NOUN WUMPUS))))))
   "Now for a longer sentence"
   ((chart-parses '(I see the wumpus in 2 3 and it is smelly ) *e1*)
    (renaming?
     *
     '((S (S ((NP SUBJECTIVE) ((PRONOUN SUBJECTIVE) I)) 
             (VP (VERB SEE) 
                 ((NP $CASE.218) ((NP $CASE.220) (ARTICLE THE) (NOUN WUMPUS)) 
                  (PP (PREPOSITION IN) ((NP $CASE.225) (DIGIT 2) (DIGIT 3)))))) 
          (CONJUNCTION AND) 
          (S ((NP $CASE.234) ((PRONOUN $CASE) IT)) 
             (VP (VERB IS) (ADJECTIVE SMELLY)))))))
   "An example from the simple arithmetic grammar."
   ((chart-parses '([ 1 + 2 ] * 3 0) *arithmetic-grammar*)
    '(((EXP (* (+ 1 2) (+ (* 10 3) 0))) 
       ((EXP (+ 1 2)) 
        ([ [) 
        ((EXP (+ 1 2)) 
         ((EXP 1) ((NUMBER 1) ((DIGIT 1) 1)))
         ((OPERATOR +) +)
         ((EXP 2) ((NUMBER 2) ((DIGIT 2) 2))))
        (] ]))
       ((OPERATOR *) *)
       ((EXP (+ (* 10 3) 0)) 
        ((NUMBER (+ (* 10 3) 0)) ((NUMBER 3) ((DIGIT 3) 3)) ((DIGIT 0) 0))))))
   "The function MEANINGS picks out just the semantics"
   ((meanings '([ 1 + 2 ] * 3 0) *arithmetic-grammar*)
    '((* (+ 1 2) (+ (* 10 3) 0))))
   "Note that strings can be ambiguous, yielding two or more parses."
   ((meanings '(1 + 2 * 3) *arithmetic-grammar*)
    '((* (+ 1 2) 3) (+ 1 (* 2 3))))
   ((chart-parses '(1 + 2 * 3) *arithmetic-grammar*)
    '(((EXP (* (+ 1 2) 3)) 
       ((EXP (+ 1 2)) ((EXP 1) ((NUMBER 1) ((DIGIT 1) 1)))
        ((OPERATOR +) +) ((EXP 2) ((NUMBER 2) ((DIGIT 2) 2))))
       ((OPERATOR *) *) ((EXP 3) ((NUMBER 3) ((DIGIT 3) 3))))
      ((EXP (+ 1 (* 2 3))) 
       ((EXP 1) ((NUMBER 1) ((DIGIT 1) 1))) 
       ((OPERATOR +) +) 
       ((EXP (* 2 3)) ((EXP 2) ((NUMBER 2) ((DIGIT 2) 2))) ((OPERATOR *) *) 
        ((EXP 3) ((NUMBER 3) ((DIGIT 3) 3)))))))
   ((chart-parses '(i shot the wumpus that stinks) *e2*)
    (renaming?
     *
     '(((S ((SHOT (THE $X.648 (AND (WUMPUS $X.648) (STINKS $X.648)))) I))
        ((NP I) ((PRONOUN I) I))
        ((VP (SHOT (THE $X.648 (AND (WUMPUS $X.648) (STINKS $X.648)))))
         ((VP SHOT) ((VERB SHOT) SHOT))
         ((NP (THE $X.648 (AND (WUMPUS $X.648) (STINKS $X.648))))
          ((NP (THE $X.655 (WUMPUS $X.655))) ((ARTICLE THE) THE)
           ((NOUN WUMPUS) WUMPUS))
          ((RELCLAUSE STINKS) (THAT THAT)
           ((VP STINKS) ((VERB STINKS) STINKS)))))))))
   ((meanings '(i shoot the wumpus that stinks and i grab the gold) *e2*)
    (renaming?
     *
     '((AND ((SHOOT (THE $X.746 (AND (WUMPUS $X.746) (STINKS $X.746)))) I)
            ((GRAB (THE $X.851 (GOLD $X.851))) I)))))

   "Probabilistic parsing with the CYK algorithm (Figure 23.5)."
   ((multiple-value-list (cyk-best-parse '(the robot is good) *e-prob-chomsky*))
    (approximately-equal * '((S (NP (Article the) (Noun robot))
                                (VP (Verb is) (Adjective good)))
                             0.015)
                         1e-6))
   ((cyk-best-parse '(robot the is good) *e-prob-chomsky*) 'nil)
   ((subspans 3) '((0 0 1) (1 1 2) (0 0 2) (0 1 2)))
   "The treebank tree of Figure 23.8: its words, and the antecedents of its traces."
   ((format nil "~{~A~^ ~}" (tree-words *sentence-tree*))
    "Her eyes were glazed as if she did n't hear or even see him .")
   ((tree-traces *sentence-tree*) '((1 "him") (2 "Her" "eyes")))
   "N-gram models of a small text, and segmentation of a text without spaces."
   ((setq corpus-words (text-words *small-corpus*)))
   ((length corpus-words) 77)
   ((first (ngram-top (make-ngram-model 1 corpus-words) 1)) '(8 "a"))
   ((first (ngram-top (make-ngram-model 2 corpus-words) 1)) '(3 "of" "the"))
   ((ngram-conditional-probability (make-ngram-model 2 corpus-words) '("it") "is") 1)
   ((length (ngram-samples (make-ngram-model 2 corpus-words) 10)) 10)
   ((viterbi-segment "itiseasytoreadwordswithoutspaces" (make-ngram-model 1 corpus-words))
    '("it" "is" "easy" "to" "read" "words" "without" "spaces"))
   "Information retrieval on five short Unix manual pages."
   ((setq unix-consultant (make-ir-system "how do i the a of")))
   ((dolist (page *unix-manual-pages*)
      (index-document unix-consultant (first page) (second page))))
   ((mapcar #'(lambda (result) (ir-document-url unix-consultant (cdr result)))
            (ir-query unix-consultant "how do I remove a file"))
    '("rm" "cp"))
   ((mapcar #'(lambda (result) (ir-document-url unix-consultant (cdr result)))
            (ir-query unix-consultant "count the lines of a file"))
    '("grep" "wc"))
   ((ir-query unix-consultant "delete") 'nil)
   "Hubs and authorities (HITS) for the query 'inherit': D has the most"
   "inlinks, and E the most outlinks."
   ((setq web (make-web *web-pages*)))
   ((mapcar #'page-address (relevant-pages "his dad" web)) '("A" "C" "E"))
   ((mapcar #'page-address (expand-pages (relevant-pages "his dad" web) web))
    '("A" "B" "C" "D" "E" "F"))
   ((hits "inherit" web)
    (and (equal (page-address (the-biggest #'page-authority *)) "D")
         (equal (page-address (the-biggest #'page-hub *)) "E")))

   "The chart (Figure 23.4) of a sentence of E0 has one complete S edge"
   "spanning it, and a non-sentence has none."
   ((setq chart (chart-parse '(I smell a stench) *E0*)))
   ((let ((*grammar* *E0*)) (mapcar #'edge->tree (spanning-edges chart)))
    '((S (NP (PRONOUN I))
         (VP (VP (VERB SMELL)) (NP (ARTICLE A) (NOUN STENCH))))))
   ((let ((*grammar* *E0*)) (spanning-edges (chart-parse '(smell I) *E0*))) 'nil)
   "The CYK table of 'the robot is good': the best NP spans words 0..1"
   "with probability 0.6 x 0.5 x 0.4, and the S the whole sentence."
   ((multiple-value-list (cyk-parse '(the robot is good) *e-prob-chomsky*))
    (destructuring-bind (tt p) *
      (and (equal (gethash '(NP 0 1) tt) '(NP (Article the) (Noun robot)))
           (approximately-equal (gethash '(NP 0 1) p) 0.12 1e-6)
           (approximately-equal (gethash '(VP 2 3) p) 0.125 1e-6)
           (approximately-equal (gethash '(S 0 3) p) 0.015 1e-6)
           (null (gethash '(S 0 1) tt)))))
   "A probabilistic grammar: the lexical rules of a word, and a new grammar."
   ((pcfg-lexical-rules *e-prob-chomsky* 'robot) '((Noun . 0.4)))
   ((pcfg-p (make-pcfg :lexicon '((A x 1.0)) :rules nil)) *)
   ((multiple-value-list
     (cyk-best-parse '(x y) (make-pcfg :lexicon '((A x 1) (B y 1/2) (B x 1/2))
                                       :rules '((S A B 1/2) (S B B 1/2)))))
    '((S (A x) (B y)) 1/4))
   "Parsing as search: bottom-up A* and beam search with the E0 grammar."
   ((astar-search-parsing '(I smell a stench) *E0*)
    (and (equal (first *) '((I smell a stench)))
         (equal (car (last *)) '(nil S))))
   ((astar-search-parsing '(smell I) *E0*) 'nil)
   ((beam-search-parsing '(the wumpus is in 2 2) *E0*)
    (and (equal (first *) '((the wumpus is in 2 2)))
         (equal (car (last *)) '(nil S))))
   ((beam-search-parsing '(smell I) *E0*) 'nil)
   "A character n-gram model: each word is preceded by a space."
   ((setq char-model (make-ngram-char-model 2 '("the" "then"))))
   ((ngram-model-total char-model) 7)
   ((ngram-probability char-model (list #\t #\h)) 2/7)
   ((ngram-conditional-probability char-model (list #\h) #\e) 1)
   ((ngram-probability char-model (list #\Space #\t)) 2/7)
   "Decoding ciphers with the letter bigrams of Flatland (from aima-data)."
   ((setq flatland (make-bigram-letter-model (read-aima-data-text "EN-text/flatland.txt"))))
   ((shift-encode "abc z" 1) "bcd a")
   ((rot13 (rot13 "Hello")) "Hello")
   ((shift-decode (shift-encode "The quick brown fox jumps over the lazy dog" 7) flatland)
    "The quick brown fox jumps over the lazy dog")
   ((setq plaintext (canonical-text "It is a truth universally acknowledged, that a single
man in possession of a good fortune, must be in want of a wife. However little known
the feelings or views of such a man may be on his first entering a neighbourhood,
this truth is so well fixed in the minds of the surrounding families, that he is
considered the rightful property of some one or other of their daughters.")))
   ((permutation-decode (substitute-letters plaintext "gqxobfvkjnzmdiwuacyptlersh") flatland)
    (> (/ (count t (map 'list #'char= * plaintext)) (length plaintext)) 0.95))
   )
