#!/usr/bin/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/dirs/etext99/advsh12.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 Dowson novel
(print "... Loading from Gutenberg project ...")
(bayes-train '() (parse (lower-case (read-file (docs 0))) "[^a-z]+" 0) 'DoyleDowson)

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

(setf result (bayes-query (parse "he was putting the last touches to a picture") 'DoyleDowson))
(if (< (apply add (map sub result '(0.03802079132 0.9619792087))) 0.001)
	(println ">>>>> 1. Doyle/Dowson SUCCESSFUL")
	(println ">>>>> PROBLEM in 1. Doyle/Dowson")) 

(setf result (bayes-query (parse "immense faculties and extraordinary powers of observation") 'DoyleDowson))
(if (< (apply add (map sub result '(0.985108793 0.01489120699))) 0.001)
	(println ">>>>> 2. Doyle/Dowson SUCCESSFUL")
	(println ">>>>> PROBLEM in 2. Doyle/Dowson")) 

(println)

(println "Chain Bayes:")

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

(setf result (bayes-query '(test-positive) Data true) )
(if (< (apply add (map sub result '(0.3076923077 0.6923076923))) 0.001)
	(println ">>>>> 1. Chain Bayes SUCCESSFULL")
	(println "PROBLEM in 1. Chain Bayes")) 
(setf result (bayes-query '(test-positive test-positive) Data true) )
(if (< (apply add (map sub result '(0.64 0.36))) 0.001)
	(println ">>>>> 2. Chain Bayes SUCCESSFULL")
	(println "PROBLEM in 2. Chain Bayes")) 

(setf result (bayes-query '(test-positive test-positive test-positive) Data true) )
(if (< (apply add (map sub result '(0.8767123288 0.1232876712))) 0.001)
	(println ">>>>> 3. Chain Bayes SUCCESSFULL")
	(println "PROBLEM in 3. Chain Bayes")) 

(exit)

; eof

