#!/usr/bin/env newlisp

(println)
(println "R.A. Fisher's Chi2:")

;; from Project Gutenberg: http://www.gutenberg.org/catalog/
;; The Adventures of Sherlock Holmes - Sir Arthur Conan Doyle
;; A Comedy of Masks - Ernest Dowson and Arthur Moore

(set 'docs '(
        ; A Comedy of Masks - Ernest Dowson and Arthur Moore, 547KB
        "http://www.gutenberg.org/files/16703/16703.txt"
        ; The Adventures of Sherlock Holmes - Conan Doyle, 576KB
        "http://www.gutenberg.org/cache/epub/1661/pg1661.txt"
        ; The tale of Beowulf - anonymous, 219KB
        "http://www.gutenberg.org/files/20431/20431-8.txt"
        ))

;if files are available locally
;(bayes-train '() (parse (lower-case (read-file "Comedy.txt")) "[^a-z]+" 0) 'DoyleDowson)
;(bayes-train (parse (lower-case (read-file "Sherlock.txt")) "[^a-z]+" 0) '() 'DoyleDowson)

; training with the Doyle novel
(println "Loading from Gutenberg project ...")
(set 'AdventuresOfSherlockHolmes (read-file (docs 1)))
(println "training: The Adventures of Sherlock Holmes (Doyle)")
(bayes-train (parse (lower-case AdventuresOfSherlockHolmes) "[^a-z]+" 0) '() 'DoyleDowson)

; training with the Dowson novel
(println "... Loading from Gutenberg project ...")
(set 'ComedyOfMasks (read-file (docs 0)))
(println "training: Comedy of Masks (Dowson)")
(bayes-train '() (parse (lower-case ComedyOfMasks) "[^a-z]+" 0) 'DoyleDowson)

(setf result (bayes-query (parse "he was putting the last touches to a picture") 'DoyleDowson))
(println "result: " result " " '(0.0359554723158327 0.964044527684167))
(set 'diff (apply add (map abs (map sub result '(0.0359554723158327 0.964044527684167)))))
(println (format "diff: %10.5f " diff) 
    (if (< diff 0.001) ">>>>> bayes SUCCESSFUL" ">>>>> ERROR in bayes"))

(setf result (bayes-query (parse "immense faculties and extraordinary powers of observation") 'DoyleDowson))
(println "result: " result " " ' (0.983569359827141 0.0164306401728594) )
(set 'diff (apply add (map abs (map sub result ' (0.983569359827141 0.0164306401728594) ))))
(println (format "diff: %10.5f " diff) 
    (if (< diff 0.001) ">>>>> bayes SUCCESSFUL" ">>>>> ERROR in bayes"))

(println)

(println "Chain Bayes:")

(set 'Data:test-positive '(8 18))
(set 'Data:test-negative '(2 72))
(set 'Data:total '(10 90))

(pretty-print 80 " " "%1.15f")
(setf result (bayes-query '(test-positive) Data true) )
(println "result: " result "  difference: " (map sub result '(0.3076923077 0.6923076923)))

(setf result (bayes-query '(test-positive test-positive) Data true) )
(println "result: " result " difference: " (map sub result '(0.64 0.36)))

(setf result (bayes-query '(test-positive test-positive test-positive) Data true) )
(println "result: " result " difference: " (map sub result '(0.8767123288 0.1232876712)))

(pretty-print 80 " " "%1.15g")
(exit)

; eof

