2016-07-29 11:46:44 +00:00
|
|
|
module Main
|
|
|
|
where
|
2016-08-05 19:24:36 +00:00
|
|
|
-- import Sibe
|
2016-09-09 20:06:15 +00:00
|
|
|
import Sibe.NLP
|
2016-07-29 11:46:44 +00:00
|
|
|
import Sibe.NaiveBayes
|
|
|
|
import Text.Printf
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import Debug.Trace
|
2016-07-29 13:25:59 +00:00
|
|
|
import Data.List.Split
|
2016-08-05 19:24:36 +00:00
|
|
|
import Control.Arrow ((&&&))
|
2016-08-20 20:29:42 +00:00
|
|
|
import Control.Monad (when, unless)
|
|
|
|
import Data.Function (on)
|
2016-08-08 08:05:26 +00:00
|
|
|
import System.Environment
|
2016-07-29 11:46:44 +00:00
|
|
|
|
|
|
|
main = do
|
2016-08-08 08:05:26 +00:00
|
|
|
args <- getArgs
|
2016-07-29 13:25:59 +00:00
|
|
|
dataset <- readFile "examples/doc-classifier-data/data-reuters"
|
|
|
|
test <- readFile "examples/doc-classifier-data/data-reuters-test"
|
2016-07-29 11:46:44 +00:00
|
|
|
|
2016-07-29 13:25:59 +00:00
|
|
|
classes <- map (filter (/= ' ')) . lines <$> readFile "examples/doc-classifier-data/data-classes"
|
2016-08-08 05:32:26 +00:00
|
|
|
sws <- lines <$> readFile "examples/stopwords"
|
2016-07-29 11:46:44 +00:00
|
|
|
|
2016-08-20 20:29:42 +00:00
|
|
|
let verbose = elem "-v" args || elem "--verbose" args
|
|
|
|
topten = elem "-10" args || elem "--top-ten" args
|
|
|
|
unless verbose $ putStrLn "use --verbose to print more information"
|
2016-08-08 08:05:26 +00:00
|
|
|
|
2016-07-29 11:46:44 +00:00
|
|
|
let intClasses = [0..length classes - 1]
|
2016-08-20 20:29:42 +00:00
|
|
|
documents = cleanDocuments . removeWords sws $ createDocuments classes dataset
|
2016-08-05 19:24:36 +00:00
|
|
|
testDocuments = cleanDocuments $ createDocuments classes test
|
2016-08-20 20:29:42 +00:00
|
|
|
|
2016-09-09 20:06:15 +00:00
|
|
|
nb = initialize documents intClasses
|
2016-07-29 11:46:44 +00:00
|
|
|
|
2016-08-20 20:29:42 +00:00
|
|
|
-- top-ten
|
|
|
|
topClasses = take 10 . reverse $ sortBy (compare `on` (length . snd)) (cd nb)
|
|
|
|
filtered = map (\(c, ds) -> (c, take 100 ds)) topClasses
|
|
|
|
filteredClasses = map fst filtered
|
|
|
|
ttDocs = concatMap snd filtered
|
2016-09-09 20:06:15 +00:00
|
|
|
ttNB = initialize ttDocs filteredClasses
|
2016-08-20 20:29:42 +00:00
|
|
|
|
|
|
|
ttTestDocuments = filter ((`elem` filteredClasses) . c) . cleanDocuments $ createDocuments classes test
|
|
|
|
|
|
|
|
ttResults = session ttTestDocuments ttNB
|
|
|
|
normalResults = session testDocuments nb
|
|
|
|
results = if topten then ttResults else normalResults
|
|
|
|
|
|
|
|
iClasses = if topten then filteredClasses else intClasses
|
2016-08-08 08:05:26 +00:00
|
|
|
-- results = session devTestDocuments nb
|
2016-08-05 19:24:36 +00:00
|
|
|
|
2016-08-20 20:29:42 +00:00
|
|
|
when verbose . putStrLn $ "# Example of cleaned document:\n" ++ (show . text $ head documents)
|
2016-07-29 11:46:44 +00:00
|
|
|
|
2016-08-08 05:32:26 +00:00
|
|
|
let showResults (c, (r, confidence)) = putStrLn (classes !! c ++ " ~ " ++ classes !! r)
|
2016-08-08 08:05:26 +00:00
|
|
|
when verbose $ mapM_ showResults results
|
|
|
|
|
2016-08-20 20:29:42 +00:00
|
|
|
when (verbose && not topten) .
|
2016-08-08 08:05:26 +00:00
|
|
|
putStrLn $ "The training data is imbalanced which causes the classifier to be biased towards\n"
|
|
|
|
++ "some classes, `earn` is an example, the class alone has around 90% accuracy while\n"
|
|
|
|
++ "the rest of classes have a much lower accuracy and it's commonly seen that most inputs\n"
|
|
|
|
++ "are incorrectly classified as `earn`.\n"
|
2016-08-20 20:29:42 +00:00
|
|
|
++ "Try running with --top-ten to classify top 10 classes by using evenly split documents\n"
|
2016-08-08 08:05:26 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
accuracies =
|
2016-08-20 20:29:42 +00:00
|
|
|
let as = zip iClasses $ map (\c -> filter ((==c) . fst) results) iClasses
|
2016-08-08 08:05:26 +00:00
|
|
|
av = filter (not . null . snd) as
|
|
|
|
calculated = map (fst &&& accuracy . snd) av
|
|
|
|
in sortBy (\(_, a) (_, b) -> b `compare` a) calculated
|
|
|
|
|
|
|
|
when verbose $
|
|
|
|
mapM_ (\(c, a) -> putStrLn $ "Accuracy(" ++ classes !! c ++ ") = " ++ show a) accuracies
|
2016-07-29 11:46:44 +00:00
|
|
|
|
2016-08-08 08:05:26 +00:00
|
|
|
putStrLn $ "\nAverages: "
|
|
|
|
putStrLn $ "Recall = " ++ show (recall results)
|
|
|
|
putStrLn $ "Precision = " ++ show (precision results)
|
|
|
|
putStrLn $ "F Measure = " ++ show (fmeasure results)
|
|
|
|
putStrLn $ "Accuracy = " ++ show (accuracy results)
|
2016-08-05 19:24:36 +00:00
|
|
|
|
|
|
|
createDocuments classes content =
|
|
|
|
let splitted = splitOn (replicate 10 '-' ++ "\n") content
|
|
|
|
pairs = map ((head . lines) &&& (unwords . tail . lines)) splitted
|
|
|
|
documents = map (\(topic, text) -> Document text (fromJust $ elemIndex topic classes)) pairs
|
|
|
|
in documents
|