Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- type FrequencyList = HashMap Text Int
- data Labeled a = Labeled {
- hash :: FrequencyList
- label :: a
- data Classified a = Classified {
- _class :: a
- probability :: Double
- data BayesModel a = BayesModel {}
- emptyModel :: BayesModel a
- teach :: Ord a => Text -> a -> BayesModel a -> BayesModel a
- runBayes :: (Ord a, Eq a) => BayesModel a -> String -> a
Documentation
A frequency list of words that has been assigned a class
Labeled | the class label for a piece of text |
|
data Classified a Source #
A class which has a specific probability of occuring
Classified | |
|
Eq a => Eq (Classified a) Source # | |
Eq a => Ord (Classified a) Source # | |
Show a => Show (Classified a) Source # | |
data BayesModel a Source #
A model representing the knowledge that has been given
Show a => Show (BayesModel a) Source # | |
emptyModel :: BayesModel a Source #
an empty model to begin teaching
:: Ord a | |
=> Text | the sample |
-> a | sample's class |
-> BayesModel a | the current model |
-> BayesModel a | the new model |
teaches the model
:: (Ord a, Eq a) | |
=> BayesModel a | a model that has been taught using |
-> 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