]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Text/Metrics/Utils.hs
[refactoring] add StrictData option, refactor Data.Map to Strict
[gargantext.git] / src / Gargantext / Core / Text / Metrics / Utils.hs
1 {-|
2 Module : Gargantext.Core.Text.Metrics.Utils
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.Utils where
14
15 import Gargantext.Prelude
16 import Data.Map.Strict (Map, toList)
17 import qualified Data.List as L
18 import qualified Data.Map.Strict as DM
19
20 countElem :: (Ord k) => DM.Map k Int -> k -> DM.Map k Int
21 countElem m e = DM.insertWith (+) e 1 m
22
23 freq :: (Ord k) => [k] -> DM.Map k Int
24 freq = foldl countElem DM.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