2 Module : Gargantext.Text.Metrics.Freq
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE NoImplicitPrelude #-}
16 module Gargantext.Text.Metrics.Freq where
18 import Gargantext.Prelude
19 import Data.Bool (otherwise)
20 import Data.Map (empty, Map, insertWith, toList)
21 import qualified Data.List as L
23 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
24 countElem m e = Data.Map.insertWith (+) e 1 m
26 freq :: (Ord k) => [k] -> Data.Map.Map k Int
27 freq = foldl countElem Data.Map.empty
29 getMaxFromMap :: Ord a => Map a1 a -> [a1]
30 getMaxFromMap m = go [] Nothing (toList m)
33 go ks Nothing ((k,v):rest) = go (k:ks) (Just v) rest
34 go ks (Just u) ((k,v):rest)
35 | v < u = go ks (Just u) rest
36 | v > u = go [k] (Just v) rest
37 | otherwise = go (k:ks) (Just v) rest
40 average :: [Double] -> Double
41 average x = L.sum x / L.genericLength x
43 average' :: [Int] -> Double
44 average' x = (L.sum y) / (L.genericLength y) where
45 y = L.map fromIntegral x