]> Git — Sourcephile - gargantext.git/blob - src/Data/Gargantext/Ngrams/TextMining.hs
[FIX] getNodesWithType 1 : unexpectedNull correction.
[gargantext.git] / src / Data / Gargantext / Ngrams / TextMining.hs
1 module Data.Gargantext.Ngrams.TextMining where
2
3 import Data.Map (empty, Map, insertWith, toList)
4 import Data.List (foldl, foldl')
5 import qualified Data.List as L
6
7 sortGT :: (Ord a, Ord b) => (a, b) -> (a, b) -> Ordering
8 sortGT (a1, b1) (a2, b2)
9 | a1 < a2 = GT
10 | a1 > a2 = LT
11 | a1 == a2 = compare b1 b2
12 sortGT (_, _) (_, _) = error "What is this case ?"
13
14
15 --histogram :: Ord a => [a] -> [(a, Int)]
16 --histogram = map (head &&& length) Prelude.. group Prelude.. sort Prelude.. words
17 --histogram = sortGT Prelude.. $ map (head &&& length) Prelude.. group Prelude.. sort Prelude.. words
18
19 countElem :: (Ord k) => Data.Map.Map k Int -> k -> Data.Map.Map k Int
20 countElem m e = Data.Map.insertWith (\n o -> n + o) e 1 m
21
22 freqList :: (Ord k) => [k] -> Data.Map.Map k Int
23 freqList = 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 merge :: [a] -> [a] -> [a]
36 merge [] ys = ys
37 merge (x:xs) ys = x:merge ys xs
38
39 average :: [Double] -> Double
40 average x = L.sum x / L.genericLength x
41
42 average' :: [Int] -> Double
43 average' x = (L.sum y) / (L.genericLength y) where
44 y = map fromIntegral x
45
46
47 countYear :: [Integer] -> Map Integer Integer
48 countYear [] = empty
49 countYear (x:xs) = insertWith (+) x 1 (countYear xs)
50
51 countYear' :: [Integer] -> Map Integer Integer
52 countYear' (xs) = foldl' (\x y -> insertWith (+) y 1 x) empty xs
53
54
55 textMiningMain :: IO ()
56 textMiningMain = do
57 print $ merge ["abc"::String] ["bcd" :: String]