module Text.Eros.Message (messageScore) where
import Data.List
import qualified Data.Map.Strict as M
import qualified Data.Text.Lazy as L
import Data.Tree
import Text.Eros.Phrase
import Text.Eros.Phraselist
type BadWord = L.Text
type Message = L.Text
type MessagePart = L.Text
type Restof = L.Text
type RestOf = L.Text
type Word = L.Text
type Score = Int
messageSplit :: Message -> PhraseMap -> [(Score, RestOf)]
messageSplit initialText sayingsMap = concat $ filter (/= [])
$ nub
$ map breakSaying potentialSayings
where
potentialSayings = M.keys sayingsMap
breakSaying saying = map (trimSaying saying . snd) $ broked saying
trimSaying saying txt = (sayScore saying, L.strip $ L.drop (L.length saying) txt)
sayScore saying = case maybeScore saying of
Just score -> score
Nothing -> 0
maybeScore saying = fmap score $ fmap rootLabel $ M.lookup saying sayingsMap
broked saying = L.breakOnAll saying lowerText
lowerText = L.toLower initialText
trimPair (a, b) = (L.strip a, L.strip b)
messageScore :: Message -> PhraseMap -> Score
messageScore msg pmap
| L.empty == msg = 0
| otherwise = (sum topScores) + (sum lowerScores)
where
msgSplit = messageSplit msg pmap
topScores = map fst msgSplit
lowerScores = map (\m -> messageScore m pmap) $ map snd msgSplit