]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Metrics/Freq.hs
Merge branch 'master' into lang-parser
[gargantext.git] / src / Gargantext / Text / Metrics / Freq.hs
1 {-|
2 Module : Gargantext.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 {-# LANGUAGE NoImplicitPrelude #-}
13
14 module Gargantext.Text.Metrics.Freq where
15
16 import Gargantext.Prelude
17 import Data.Bool (otherwise)
18 import Data.Map (empty, Map, insertWith, toList)
19 import qualified Data.List as L
20
21 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
22 countElem m e = Data.Map.insertWith (+) e 1 m
23
24 freq :: (Ord k) => [k] -> Data.Map.Map k Int
25 freq = foldl countElem Data.Map.empty
26
27 getMaxFromMap :: Ord a => Map a1 a -> [a1]
28 getMaxFromMap m = go [] Nothing (toList m)
29 where
30 go ks _ [] = ks
31 go ks Nothing ((k,v):rest) = go (k:ks) (Just v) rest
32 go ks (Just u) ((k,v):rest)
33 | v < u = go ks (Just u) rest
34 | v > u = go [k] (Just v) rest
35 | otherwise = go (k:ks) (Just v) rest
36
37
38 average :: [Double] -> Double
39 average x = L.sum x / L.genericLength x
40
41 average' :: [Int] -> Double
42 average' x = (L.sum y) / (L.genericLength y) where
43 y = L.map fromIntegral x
44
45