hext-0.1.0.3: a text classification library

Safe HaskellSafe
LanguageHaskell2010

NLP.Hext.NaiveBayes

Contents

Synopsis

Documentation

type FrequencyList = HashMap Text Int Source #

A hash representing frequency list of words

data Labeled a Source #

A frequency list of words that has been assigned a class

Constructors

Labeled

the class label for a piece of text

Fields

data Classified a Source #

A class which has a specific probability of occuring

Constructors

Classified 

Fields

data BayesModel a Source #

A model representing the knowledge that has been given

Constructors

BayesModel 

Fields

Instances

emptyModel :: BayesModel a Source #

an empty model to begin teaching

teach Source #

Arguments

:: Ord a 
=> Text

the sample

-> a

sample's class

-> BayesModel a

the current model

-> BayesModel a

the new model

teaches the model

runBayes Source #

Arguments

:: (Ord a, Eq a) 
=> BayesModel a

a model that has been taught using learn

-> String

the sample string to be classified

-> a

a datatype representing a class to classify text

Runs a sample string through the Naive Bayes algorithm using a model containing all knowledge from previous learning

Example: Simple Usage

In this example a list of sample reviews and their corresponding classes are zipped into an association list to be passed into the makeMaterial function. This newly created material is then passed into the runBayes function, along with a new review. This will classify the new review based on the training material that has been given.

data Class = Positive | Negative deriving (Eq, Show)

doc1 = "I loved the movie"
doc2 = "I hated the movie"
doc3 = "a great movie. good movie"
doc4 = "poor acting"
doc5 = "great acting. a good movie"

docs = [doc1, doc2, doc3, doc4, doc5]
correspondingClasses = [Positive, Negative, Positive, Negative, Positive]
classifiedDocs = zip docs correspondingClasses

main :: IO ()
main = do
    -- teachMultiple returns a BayesModel Class
    let teachMultiple = foldl (\m (sample, cl) -> teach (T.pack sample) cl m) emptyModel

    let review = "I hated the poor acting"
    let result = runBayes (teachMultiple classifiedDocs) review
    
    putStrLn $ "The review '" ++ review ++ "' is " ++ show result -- Negative