2 module Gargantext.Ngrams.TextMining where
4 import Gargantext.Prelude
5 import Data.Ord(Ordering(LT,GT), compare)
6 import Data.Text (pack)
7 import Data.Bool (otherwise)
8 import Data.Map (empty, Map, insertWith, toList)
9 import Data.List (foldl, foldl')
10 import qualified Data.List as L
12 sortGT :: (Ord a, Ord b) => (a, b) -> (a, b) -> Ordering
13 sortGT (a1, b1) (a2, b2)
16 | a1 == a2 = compare b1 b2
17 sortGT (_, _) (_, _) = panic (pack "What is this case ?")
20 --histogram :: Ord a => [a] -> [(a, Int)]
21 --histogram = map (head &&& length) Prelude.. group Prelude.. sort Prelude.. words
22 --histogram = sortGT Prelude.. $ map (head &&& length) Prelude.. group Prelude.. sort Prelude.. words
24 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
25 countElem m e = Data.Map.insertWith (\n o -> n + o) e 1 m
27 freqList :: (Ord k) => [k] -> Data.Map.Map k Int
28 freqList = foldl countElem Data.Map.empty
30 getMaxFromMap :: Ord a => Map a1 a -> [a1]
31 getMaxFromMap m = go [] Nothing (toList m)
34 go ks Nothing ((k,v):rest) = go (k:ks) (Just v) rest
35 go ks (Just u) ((k,v):rest)
36 | v < u = go ks (Just u) rest
37 | v > u = go [k] (Just v) rest
38 | otherwise = go (k:ks) (Just v) rest
40 merge :: [a] -> [a] -> [a]
42 merge (x:xs) ys = x:merge ys xs
44 average :: [Double] -> Double
45 average x = L.sum x / L.genericLength x
47 average' :: [Int] -> Double
48 average' x = (L.sum y) / (L.genericLength y) where
49 y = map fromIntegral x
52 countYear :: [Integer] -> Map Integer Integer
54 countYear (x:xs) = insertWith (+) x 1 (countYear xs)
56 countYear' :: [Integer] -> Map Integer Integer
57 countYear' (xs) = foldl' (\x y -> insertWith (+) y 1 x) empty xs