Stop.hs 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{-|
Module      : Gargantext.Text.Terms.Stop
Description : Mono Terms module
Copyright   : (c) CNRS, 2017 - present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

Stop words and (how to learn it).

Main type here is String.

-}

{-# LANGUAGE NoImplicitPrelude #-}
Alexandre Delanoë's avatar
Alexandre Delanoë committed
17
{-# LANGUAGE OverloadedStrings #-}
18

Alexandre Delanoë's avatar
Alexandre Delanoë committed
19
module Gargantext.Text.Terms.Stop -- (detectLang, detectLangs, stopList)
20 21
  where

22
import GHC.Base (Functor)
23 24 25 26 27
import Numeric.Probability.Distribution ((??))
import qualified Numeric.Probability.Distribution as D

import Data.Char (toLower)
import qualified Data.List as DL
28 29

import Data.Maybe (maybe)
30
import Data.Map.Strict (Map, toList)
31 32 33 34
import qualified Data.Map.Strict as DM

import Data.String (String)

35
import Data.Text (Text)
36
import Data.Text (pack, unpack)
37
import Data.Tuple.Extra (both)
38 39

import Gargantext.Prelude
40
import Gargantext.Core (Lang(..), allLangs)
41 42
import Gargantext.Text.Terms.Mono (words)
import Gargantext.Text.Metrics.Count (occurrencesWith)
43

44 45 46 47 48
import qualified Gargantext.Text.Samples.FR as FR
import qualified Gargantext.Text.Samples.EN as EN
--import qualified Gargantext.Text.Samples.DE as DE
--import qualified Gargantext.Text.Samples.SP as SP
--import qualified Gargantext.Text.Samples.CH as CH
49

50
------------------------------------------------------------------------
51 52 53 54 55 56 57 58 59 60 61 62 63 64
data Candidate = Candidate { stop :: Double
                           , noStop :: Double
 } deriving (Show)

-- * String preparation

-- | String prepare
blanks :: String -> String
blanks [] = []
blanks xs = [' '] <> xs <> [' ']

-- | Blocks increase the size of the word to ease computations
-- some border and unexepected effects can happen, need to be tested
blockOf :: Int -> String -> String
Alexandre Delanoë's avatar
Alexandre Delanoë committed
65
blockOf n = DL.concat . DL.take n . DL.repeat
66 67 68 69

-- | Chunks is the same function as splitBy in Context but for Strings,
-- not Text (without pack and unpack operations that are not needed).
chunks :: Int -> Int -> String -> [String]
Alexandre Delanoë's avatar
Alexandre Delanoë committed
70 71 72 73 74 75
chunks n m = DL.take m . filter (not . all (== ' '))
                       . chunkAlong (n+1) 1
                       . DL.concat
                       . DL.take 1000
                       . DL.repeat
                       . blanks
76 77 78 79

allChunks :: [Int] -> Int -> String -> [String]
allChunks ns m st = DL.concat $ map (\n -> chunks n m st) ns

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
allChunks' :: [Int] -> Int -> String -> [[String]]
allChunks' ns m st = map (\n -> chunks n m st) ns

------------------------------------------------------------------------
-- * Analyze candidate
type StringSize = Int
type TotalFreq  = Int
type Freq       = Int
type Word       = String

data LangWord = LangWord Lang Word

type LangProba = Map Lang Double

------------------------------------------------------------------------
95 96 97 98 99 100 101 102
detectLang :: Text -> Maybe Lang
detectLang = head . map fst . detectLangs

detectLangs :: Text -> [(Lang, Double)]
detectLangs = detectLangs' . unpack

detectLangs' :: String -> [(Lang, Double)]
detectLangs' s =  DL.reverse $ DL.sortOn snd
103 104 105 106 107 108 109 110 111 112 113 114 115
                            $ toList
                            $ detect (wordsToBook [0..2] s) eventLang

part :: (Eq p, Fractional p) => p -> p -> p
part 0 _ = 0
part _ 0 = 0
part x y = x / y

toProba :: (Eq b, Fractional b, Functor t, Foldable t) =>
                 t (a, b) -> t (a, b)
toProba xs = map (\(a,b) -> (a, part b total)) xs
  where
    total = sum $ map snd xs
116

117 118 119 120 121 122
textSample :: Lang -> String
textSample EN = EN.textSample
textSample FR = FR.textSample
--textSample DE = DE.textSample
--textSample SP = SP.textSample
--textSample CH = CH.textSample
123 124

langWord :: Lang -> LangWord
125
langWord l = LangWord l (textSample l)
126

127 128
eventLang :: EventLang
eventLang = toEventLangs [0..2] [ langWord l | l <- allLangs ]
129

130
detect :: EventBook -> EventLang -> LangProba
131
detect (EventBook mapFreq _) el = 
Alexandre Delanoë's avatar
Alexandre Delanoë committed
132
  DM.unionsWith (+)
133 134 135 136
  $ map DM.fromList
  $ map (\(s,n) -> map (\(l,f) -> (l, (fromIntegral n) * f)) $ toPrior s el)
  $ filter (\x -> fst x /= "  ")
  $ DM.toList mapFreq
137 138 139 140

------------------------------------------------------------------------
-- | TODO: monoids
type EventLang = Map Lang EventBook
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
toEventLangs :: [Int] -> [LangWord] -> EventLang
toEventLangs ns = foldl' (opLang (+)) (emptyEventLang ns) . map (toLang ns)

emptyEventLang :: [Int] -> EventLang
emptyEventLang ns = toLang ns (LangWord FR "")

toLang :: [Int] -> LangWord -> EventLang
toLang ns (LangWord l txt) = DM.fromList [(l, wordsToBook ns txt)]

opLang :: (Freq -> Freq -> Freq) -> EventLang -> EventLang -> EventLang
opLang f = DM.unionWith (op f)

------------------------------------------------------------------------
-- | TODO: monoids (but proba >= 0)

peb :: String -> EventBook -> Double
peb st (EventBook mapFreq mapN) = (fromIntegral a) / (fromIntegral b)
  where
    a = maybe 0 identity $ DM.lookup st mapFreq
    b = maybe 1 identity $ DM.lookup (length st) mapN

163 164 165 166 167 168 169 170 171 172 173 174 175 176
peb' :: String -> EventBook -> (Freq, TotalFreq)
peb' st (EventBook mapFreq mapN) = (fromIntegral a, fromIntegral b)
  where
    a = maybe 0 identity $ DM.lookup st mapFreq
    b = maybe 1 identity $ DM.lookup (length st) mapN

------------------------------------------------------------------------
toPrior :: String -> EventLang -> [(Lang, Double)]
toPrior s el = prior $ pebLang s el

pebLang :: String -> EventLang -> [(Lang, (Freq,TotalFreq))]
pebLang st = map (\(l,eb) -> (l, peb' st eb)) .  DM.toList
------------------------------------------------------------------------
prior :: [(Lang, (Freq, TotalFreq))] -> [(Lang, Double)]
177
prior ps = zip ls $ zipWith (\x y -> x^(99::Int) * y) (map (\(a,_) -> part a (sum $ map fst ps')) ps') 
178 179 180 181 182 183
                                (map (\(a,b) -> a / b) ps')
  where
   
    (ls, ps'') = DL.unzip ps
    ps' = map (both fromIntegral) ps''
------------------------------------------------------------------------
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
data EventBook = EventBook { events_freq :: Map String     Freq
                           , events_n    :: Map StringSize TotalFreq
                           }
                             deriving (Show)

emptyEventBook :: [Int] -> EventBook
emptyEventBook ns = wordToBook ns " "

wordsToBook :: [Int] -> String -> EventBook
wordsToBook ns txt = foldl' (op (+)) (emptyEventBook ns) eventsBook
  where
    ws = map unpack $ words $ pack txt
    eventsBook = map (wordToBook ns) ws

wordToBook :: [Int] -> Word -> EventBook
wordToBook ns txt = EventBook ef en
  where
    chks = allChunks' ns 10 txt
202
    en = DM.fromList $ map (\(n,ns') -> (n, length ns')) $ zip ns chks
203 204 205 206 207 208 209 210
    ef = foldl' DM.union DM.empty $ map (occurrencesWith identity) chks

op :: (Freq -> Freq -> Freq) -> EventBook -> EventBook -> EventBook
op f (EventBook ef1 en1)
     (EventBook ef2 en2) = EventBook (DM.unionWith f ef1 ef2)
                                     (DM.unionWith f en1 en2)

------------------------------------------------------------------------
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
------------------------------------------------------------------------
-- * Make the distributions
makeDist :: [String] -> D.T Double String
makeDist = D.uniform . DL.concat . map (allChunks [0,2] 10)

stopDist :: D.T Double String
stopDist = makeDist stopList

candDist :: D.T Double String
candDist = makeDist candList

------------------------------------------------------------------------
sumProba :: Num a => D.T a String -> [Char] -> a
sumProba ds x = sum $ map ((~?) ds) $ allChunks [0,2] 10 $ map toLower x

-- | Get probability according a distribution
(~?) :: (Num prob, Eq a) => D.T prob a -> a -> prob
(~?) ds x = (==x) ?? ds

------------------------------------------------------------------------
231
candidate :: [Char] -> Candidate
232 233 234 235
candidate x = Candidate (sumProba stopDist x) (sumProba candDist x)

------------------------------------------------------------------------
candList :: [String]
236 237
candList = [ "france", "alexandre", "mael", "constitution"
           , "etats-unis", "associes", "car", "train", "spam"]
238 239 240


stopList :: [String]
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
stopList = map show ([0..9]::[Int]) <> stopListWords

stopListWords :: [String]
stopListWords = 
  ["a", "a's", "able", "about", "above", "according", "accordingly"
  , "across", "actually", "after", "afterwards", "again", "against"
  , "ain't", "all", "allow", "allows", "almost", "alone", "along"
  , "already", "also", "although", "always", "am", "among", "amongst", "an"
  , "analyze", "and", "another", "any", "anybody", "anyhow", "anyone"
  , "anything", "anyway", "anyways", "anywhere", "apart", "appear"
  , "apply", "appreciate", "appropriate", "are", "aren't", "around"
  , "as", "aside", "ask", "asking", "associated", "at", "available"
  , "away", "awfully", "b", "based", "be", "became", "because", "become"
  , "becomes", "becoming", "been", "before", "beforehand", "behind"
  , "being", "believe", "below", "beside", "besides", "best", "better"
  , "between", "beyond", "both", "brief", "but", "by", "c", "c'mon", "c's"
  , "came", "can", "can't", "cannot", "cant", "cause", "causes", "certain"
  , "certainly", "changes", "clearly", "co", "com", "come", "comes"
  , "common", "concerning", "consequently", "consider", "considering"
  , "contain", "containing", "contains", "corresponding", "could"
  , "couldn't", "course", "currently", "d", "definitely", "described"
  , "despite", "detecting", "detects", "did", "didn't", "different", "do"
  , "does", "doesn't", "doing", "don't", "done", "down", "downwards"
  , "during", "e", "each", "edu", "eg", "eight", "either", "else"
  , "elsewhere", "enough", "entirely", "especially", "et", "etc", "even"
  , "ever", "every", "everybody", "everyone", "everything", "everywhere"
  , "ex", "exactly", "example", "except", "f", "far", "few", "fifth"
  , "find", "first", "five", "followed", "following", "follows", "for"
  , "former", "formerly", "forth", "four", "from", "further", "furthermore"
  , "g", "get", "gets", "getting", "gif", "given", "gives", "go", "goes"
  , "going", "gone", "got", "gotten", "greetings", "h", "had", "hadn't"
  , "happens", "hardly", "has", "hasn't", "have", "haven't", "having"
  , "he", "he'd", "he'll", "he's", "hello", "help", "hence", "her"
  , "here", "here's", "hereafter", "hereby", "herein", "hereupon", "hers"
  , "herself", "hi", "him", "himself", "his", "hither", "hopefully", "how"
  , "how's", "howbeit", "however", "i", "i'd", "i'll", "i'm", "i've"
  , "identify", "ie", "if", "ignored", "immediate", "in", "inasmuch"
  , "inc", "indeed", "indicate", "indicated", "indicates", "inner"
  , "insofar", "instead", "into", "involves", "inward", "is", "isn't"
  , "it", "it'd", "it'll", "it's", "its", "itself", "j", "just", "k"
  , "keep", "keeps", "kept", "know", "known", "knows", "l", "last"
  , "late", "lately", "later", "latter", "latterly", "least", "less"
  , "lest", "let", "let's", "like", "liked", "likely", "little", "look"
  , "looking", "looks", "ltd", "m", "main", "mainly", "many", "may"
  , "maybe", "me", "mean", "meanwhile", "merely", "might", "min", "more"
  , "moreover", "most", "mostly", "much", "must", "mustn't", "my", "myself"
  , "n", "name", "namely", "nd", "near", "nearly", "necessary", "need"
  , "needs", "neither", "never", "nevertheless", "new", "next", "nine"
  , "no", "nobody", "non", "none", "noone", "nor", "normally", "not"
  , "nothing", "novel", "now", "nowhere", "o", "obviously", "of", "off"
  , "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones"
  , "only", "onto", "or", "other", "others", "otherwise", "ought", "our"
  , "ours", "ourselves", "out", "outside", "over", "overall", "own", "p"
  , "particular", "particularly", "per", "perhaps", "placed", "please"
  , "plus", "possible", "presents", "presumably", "probably", "provides"
  , "q", "que", "quite", "qv", "r", "rather", "rd", "re", "really"
  , "reasonably", "regarding", "regardless", "regards", "relatively"
  , "respectively", "right", "s", "said", "same", "saw", "say", "saying"
  , "says", "sds", "second", "secondly", "see", "seeing", "seem", "seemed"
  , "seeming", "seems", "seen", "self", "selves", "sensible", "sent"
  , "serious", "seriously", "seven", "several", "shall", "shan't"
  , "she", "she'd", "she'll", "she's", "should", "shouldn't", "since"
  , "six", "so", "some", "somebody", "somehow", "someone", "something"
  , "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry"
  , "specified", "specify", "specifying", "still", "sub", "such", "sup"
  , "sure", "t", "t's", "take", "taken", "tell", "tends", "th", "than"
  , "thank", "thanks", "thanx", "that", "that's", "thats", "the", "their"
  , "theirs", "them", "themselves", "then", "thence", "there", "there's"
  , "thereafter", "thereby", "therefore", "therein", "theres", "thereupon"
  , "these", "they", "they'd", "they'll", "they're", "they've", "think"
  , "third", "this", "thorough", "thoroughly", "those", "though", "three"
  , "through", "throughout", "thru", "thus", "to", "together", "too"
  , "took", "toward", "towards", "tried", "tries", "truly", "try"
  , "trying", "twice", "two", "u", "un", "under", "unfortunately"
  , "unless", "unlikely", "until", "unto", "up", "upon", "us", "use"
  , "used", "useful", "uses", "using", "usually", "uucp", "v", "value"
  , "various", "very", "via", "viz", "vs", "w", "want", "wants", "was"
  , "wasn't", "way", "we", "we'd", "we'll", "we're", "we've", "welcome"
  , "well", "went", "were", "weren't", "what", "what's", "whatever", "when"
  , "when's", "whence", "whenever", "where", "where's", "whereafter"
  , "whereas", "whereby", "wherein", "whereupon", "wherever", "whether"
  , "which", "while", "whither", "who", "who's", "whoever", "whole", "whom"
  , "whose", "why", "why's", "will", "willing", "wish", "with", "within"
  , "without", "won't", "wonder", "would", "wouldn't", "x", "y", "yes"
  , "yet", "you", "you'd", "you'll", "you're", "you've", "your", "yours"
  , "yourself", "yourselves", "z", "zero"]
327 328