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.Map (empty, Map, insertWith, toList)
17 import qualified Data.List as L
19 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
20 countElem m e = Data.Map.insertWith (+) e 1 m
22 freq :: (Ord k) => [k] -> Data.Map.Map k Int
23 freq = foldl countElem Data.Map.empty
25 getMaxFromMap :: Ord a => Map a1 a -> [a1]
26 getMaxFromMap m = go [] Nothing (toList m)
29 go ks Nothing ((k,v):rest) = go (k:ks) (Just v) rest
30 go ks (Just u) ((k,v):rest)
31 | v < u = go ks (Just u) rest
32 | v > u = go [k] (Just v) rest
33 | otherwise = go (k:ks) (Just v) rest
36 average :: [Double] -> Double
37 average x = L.sum x / L.genericLength x
39 average' :: [Int] -> Double
40 average' x = (L.sum y) / (L.genericLength y) where
41 y = L.map fromIntegral x