]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Metrics/Freq.hs
[FIX] French Grammar.
[gargantext.git] / src / Gargantext / Text / Metrics / Freq.hs
1 {-|
2 Module : Gargantext.Text.Metrics.Freq
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
12 -}
13
14 {-# LANGUAGE NoImplicitPrelude #-}
15
16 module Gargantext.Text.Metrics.Freq where
17
18 import Gargantext.Prelude
19 import Data.Bool (otherwise)
20 import Data.Map (empty, Map, insertWith, toList)
21 import qualified Data.List as L
22
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
25
26 freq :: (Ord k) => [k] -> Data.Map.Map k Int
27 freq = foldl countElem Data.Map.empty
28
29 getMaxFromMap :: Ord a => Map a1 a -> [a1]
30 getMaxFromMap m = go [] Nothing (toList m)
31 where
32 go ks _ [] = ks
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
38
39
40 average :: [Double] -> Double
41 average x = L.sum x / L.genericLength x
42
43 average' :: [Int] -> Double
44 average' x = (L.sum y) / (L.genericLength y) where
45 y = L.map fromIntegral x
46
47