]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude.hs
[FEAT] CSV ngrams extraction, engine and search.
[gargantext.git] / src / Gargantext / Prelude.hs
1 {-|
2 Module : Gargantext.Prelude
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 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
16
17 {-# LANGUAGE NoImplicitPrelude #-}
18
19 module Gargantext.Prelude
20 ( module Gargantext.Prelude
21 , module Protolude
22 , headMay
23 , module Text.Show
24 , module Text.Read
25 , cs
26 , module Data.Maybe
27 )
28 where
29
30 import Data.Maybe (isJust, fromJust, maybe)
31 import Protolude ( Bool(True, False), Int, Double, Integer
32 , Fractional, Num, Maybe(Just,Nothing)
33 , Enum, Bounded, Float
34 , Floating, Char, IO
35 , pure, (<*>), (<$>), panic
36 , putStrLn
37 , head, flip
38 , Ord, Integral, Foldable, RealFrac, Monad, filter
39 , reverse, map, zip, drop, take, zipWith
40 , sum, fromIntegral, length, fmap, foldl, foldl'
41 , takeWhile, sqrt, undefined, identity
42 , abs, min, max, maximum, minimum, return, snd, truncate
43 , (+), (*), (/), (-), (.), ($), (**), (^), (<), (>), log
44 , Eq, (==), (>=), (<=), (<>), (/=)
45 , (&&), (||), not
46 , fst, snd, toS
47 , elem, die, mod, div, const, either
48 , curry, uncurry
49 , otherwise, when
50 )
51
52 -- TODO import functions optimized in Utils.Count
53 -- import Protolude hiding (head, last, all, any, sum, product, length)
54 -- import Gargantext.Utils.Count
55 import qualified Data.List as L hiding (head, sum)
56 import qualified Control.Monad as M
57
58 import Data.Map (Map)
59 import qualified Data.Map as M
60
61 import Data.Map.Strict (insertWith)
62 import qualified Data.Vector as V
63 import Safe (headMay)
64 import Text.Show (Show(), show)
65 import Text.Read (Read())
66 import Data.String.Conversions (cs)
67
68 --pf :: (a -> Bool) -> [a] -> [a]
69 --pf = filter
70
71 pr :: [a] -> [a]
72 pr = reverse
73
74 --pm :: (a -> b) -> [a] -> [b]
75 --pm = map
76
77 map2 :: (t -> b) -> [[t]] -> [[b]]
78 map2 fun = map (map fun)
79
80 -- Exponential Average
81 eavg :: [Double] -> Double
82 eavg (x:xs) = a*x + (1-a)*(eavg xs)
83 where a = 0.70
84 eavg [] = 0
85
86 -- Simple Average
87 mean :: Fractional a => [a] -> a
88 mean xs = if L.null xs then 0.0
89 else sum xs / fromIntegral (length xs)
90
91 sumMaybe :: Num a => [Maybe a] -> Maybe a
92 sumMaybe = fmap sum . M.sequence
93
94 variance :: Floating a => [a] -> a
95 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
96 m = mean xs
97
98 deviation :: [Double] -> Double
99 deviation = sqrt . variance
100
101 movingAverage :: Fractional b => Int -> [b] -> [b]
102 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
103
104 ma :: [Double] -> [Double]
105 ma = movingAverage 3
106
107
108 -- | Function to split a range into chunks
109 chunkAlong :: Int -> Int -> [a] -> [[a]]
110 chunkAlong a b l = only (while dropAlong)
111 where
112 only = map (take a)
113 while = takeWhile (\x -> length x >= a)
114 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
115
116 -- | Optimized version (Vector)
117 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
118 chunkAlong' a b l = only (while dropAlong)
119 where
120 only = V.map (V.take a)
121 while = V.takeWhile (\x -> V.length x >= a)
122 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
123
124 -- | TODO Inverse of chunk ? unchunkAlong ?
125 unchunkAlong :: Int -> Int -> [[a]] -> [a]
126 unchunkAlong = undefined
127
128
129 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
130 splitAlong :: [Int] -> [Char] -> [[Char]]
131 splitAlong _ [] = [] -- No list? done
132 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
133 splitAlong (x:xs) ys = take x ys : splitAlong xs (drop x ys) -- take until our split spot, recurse with next split spot and list remainder
134
135 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
136 takeWhileM _ [] = return []
137 takeWhileM p (a:as) = do
138 v <- a
139 if p v
140 then do
141 vs <- takeWhileM p as
142 return (v:vs)
143 else return []
144
145 -- SUMS
146 -- To select the right algorithme according to the type:
147 -- https://github.com/mikeizbicki/ifcxt
148
149 sumSimple :: Num a => [a] -> a
150 sumSimple = L.foldl' (+) 0
151
152 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
153 sumKahan :: Num a => [a] -> a
154 sumKahan = snd . L.foldl' go (0,0)
155 where
156 go (c,t) i = ((t'-t)-y,t')
157 where
158 y = i-c
159 t' = t+y
160
161 -- | compute part of the dict
162 count2map :: (Ord k, Foldable t) => t k -> Map k Double
163 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
164
165 -- | insert in a dict
166 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
167 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
168
169
170 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
171 trunc n = truncate . (* 10^n)
172
173 trunc' :: Int -> Double -> Double
174 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
175
176
177 bool2int :: Num a => Bool -> a
178 bool2int b = case b of
179 True -> 1
180 False -> 0
181
182 bool2double :: Bool -> Double
183 bool2double bool = case bool of
184 True -> 1.0
185 False -> 0.0
186
187
188
189 -- Normalizing && scaling data
190 scale :: [Double] -> [Double]
191 scale = scaleMinMax
192
193 scaleMinMax :: [Double] -> [Double]
194 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
195 where
196 ma = maximum xs'
197 mi = minimum xs'
198 xs' = map abs xs
199
200 scaleNormalize :: [Double] -> [Double]
201 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
202 where
203 v = variance xs'
204 m = mean xs'
205 xs' = map abs xs
206
207
208
209 normalize :: [Double] -> [Double]
210 normalize as = normalizeWith identity as
211
212 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
213 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
214 where
215 bs' = map extract bs
216
217 -- Zip functions to add
218 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
219 zipFst f xs = zip (f xs) xs
220
221 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
222 zipSnd f xs = zip xs (f xs)
223
224 -- Just
225 unMaybe :: [Maybe a] -> [a]
226 unMaybe = map fromJust . L.filter isJust
227