sibe/examples/naivebayes-doc-classifier.hs
Mahdi Dibaiee 26eb4531fa feat(naivebayes): implement NaiveBayes algorithm
feat(example): a document classifier using NaiveBayes over reuters data
2016-07-29 16:16:44 +04:30

30 lines
994 B
Haskell

module Main
where
import Sibe
import Sibe.NaiveBayes
import Text.Printf
import Data.List
import Data.Maybe
import Debug.Trace
main = do
dataset <- readFile "examples/naivebayes-doc-classifier/data-reuters"
test <- readFile "examples/naivebayes-doc-classifier/data-reuters-test"
classes <- map (filter (/= ' ')) . lines <$> readFile "examples/naivebayes-doc-classifier/data-classes"
let intClasses = [0..length classes - 1]
documents = createDocuments classes dataset
testDocuments = createDocuments classes test
nb = initialize documents
let testResults (Document text c) =
let r = determine text nb intClasses documents
in trace (classes !! c ++ " ~ " ++ classes !! r) c == r
let results = map testResults testDocuments
putStr "Accuracy: "
putStr . show . round $ (genericLength (filter (==True) results) / genericLength results) * 100
putStrLn "%"