]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Text/Metrics/Freq.hs
[OPTIM + FIX] TFICF
[gargantext.git] / src / Gargantext / Core / Text / Metrics / Freq.hs
1 {-|
2 Module : Gargantext.Core.Text.Metrics.Freq
3 Description : Some functions to count.
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12
13 module Gargantext.Core.Text.Metrics.Freq where
14
15 import Gargantext.Prelude
16 import Data.Bool (otherwise)
17 import Data.Map (empty, Map, insertWith, toList)
18 import qualified Data.List as L
19
20 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
21 countElem m e = Data.Map.insertWith (+) e 1 m
22
23 freq :: (Ord k) => [k] -> Data.Map.Map k Int
24 freq = foldl countElem Data.Map.empty
25
26 getMaxFromMap :: Ord a => Map a1 a -> [a1]
27 getMaxFromMap m = go [] Nothing (toList m)
28 where
29 go ks _ [] = ks
30 go ks Nothing ((k,v):rest) = go (k:ks) (Just v) rest
31 go ks (Just u) ((k,v):rest)
32 | v < u = go ks (Just u) rest
33 | v > u = go [k] (Just v) rest
34 | otherwise = go (k:ks) (Just v) rest
35
36
37 average :: [Double] -> Double
38 average x = L.sum x / L.genericLength x
39
40 average' :: [Int] -> Double
41 average' x = (L.sum y) / (L.genericLength y) where
42 y = L.map fromIntegral x
43
44