]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Text/Metrics/Utils.hs
[FIX] Ngrams List size with candidates
[gargantext.git] / src / Gargantext / Core / Text / Metrics / Utils.hs
1 {-|
2 Module : Gargantext.Core.Text.Metrics.Utils
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
13 module Gargantext.Core.Text.Metrics.Utils where
14
15 import Gargantext.Prelude
16 import Data.Map (empty, Map, insertWith, toList)
17 import qualified Data.List as L
18
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
21
22 freq :: (Ord k) => [k] -> Data.Map.Map k Int
23 freq = foldl countElem Data.Map.empty
24
25 getMaxFromMap :: Ord a => Map a1 a -> [a1]
26 getMaxFromMap m = go [] Nothing (toList m)
27 where
28 go ks _ [] = ks
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
34
35
36 average :: [Double] -> Double
37 average x = L.sum x / L.genericLength x
38
39 average' :: [Int] -> Double
40 average' x = (L.sum y) / (L.genericLength y) where
41 y = L.map fromIntegral x
42
43