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
13 module Gargantext.Core.Text.Metrics.Freq where
15 import Gargantext.Prelude
16 import Data.Bool (otherwise)
17 import Data.Map (empty, Map, insertWith, toList)
18 import qualified Data.List as L
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
23 freq :: (Ord k) => [k] -> Data.Map.Map k Int
24 freq = foldl countElem Data.Map.empty
26 getMaxFromMap :: Ord a => Map a1 a -> [a1]
27 getMaxFromMap m = go [] Nothing (toList m)
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
37 average :: [Double] -> Double
38 average x = L.sum x / L.genericLength x
40 average' :: [Int] -> Double
41 average' x = (L.sum y) / (L.genericLength y) where
42 y = L.map fromIntegral x