]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Ngrams/FrequentItemSet.hs
[FIS][FIX] Frequent Item Set and fix ngrams extraction test.
[gargantext.git] / src / Gargantext / Ngrams / FrequentItemSet.hs
1 {-|
2 Module : Gargantext.Ngrams.FrequentItemSet
3 Description : Ngrams tools
4 Copyright : (c) CNRS, 2018
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Domain Specific Language to manage Frequent Item Set (FIS)
11
12 -}
13
14 module Gargantext.Ngrams.FrequentItemSet
15 ( Fis, Size
16 , occ, cooc
17 , all, between
18 , module HLCM
19 )
20 where
21
22 import Data.List (tail, filter)
23 import Data.Either
24
25 import HLCM
26
27 import Gargantext.Prelude
28
29 type Size = Either Int (Int, Int)
30
31
32 ------------------------------------------------------------------------
33 -- | Occurrence is Frequent Item Set of size 1
34 occ :: Frequency -> [[Item]] -> [Fis]
35 occ f is = fisWithSize (Left 1) f is
36
37 -- | Cooccurrence is Frequent Item Set of size 2
38 cooc :: Frequency -> [[Item]] -> [Fis]
39 cooc f is = fisWithSize (Left 2) f is
40
41 all :: Frequency -> [[Item]] -> [Fis]
42 all f is = fisWith Nothing f is
43
44 ------------------------------------------------------------------------
45 between :: (Int, Int) -> Frequency -> [[Item]] -> [Fis]
46 between (x,y) f is = fisWithSize (Right (x,y)) f is
47
48 --maximum :: Int -> Frequency -> [[Item]] -> [Fis]
49 --maximum m f is = between (0,m) f is
50
51
52 ------------------------------------------------------------------------
53 ------------------------------------------------------------------------
54 -- | Data type to type the Frequent Item Set
55 -- TODO replace List with Set in fisItemSet
56 -- be careful : risks to erase HLCM behavior
57 type Fis = Fis' Item
58 data Fis' a = Fis' { _fisCount :: Int
59 , _fisItemSet :: [a]
60 } deriving (Show)
61
62 -- | Sugar from items to FIS
63 items2fis :: [Item] -> Maybe Fis
64 items2fis is = case head is of
65 Nothing -> Nothing
66 Just h -> Just (Fis' h (tail is))
67
68 ------------------------------------------------------------------------
69 ------------------------------------------------------------------------
70
71 fisWithSize :: Size -> Frequency -> [[Item]] -> [Fis]
72 fisWithSize n f is = case n of
73 Left n' -> fisWith (Just (\x -> length x == (n'+1) )) f is
74 Right (a,b) -> fisWith (Just (\x -> cond1 a x && cond2 b x)) f is
75 where
76 cond1 a' x = length x >= a'
77 cond2 b' x = length x <= b'
78
79
80 fisWith :: Maybe ([Item] -> Bool) -> Frequency -> [[Item]] -> [Fis]
81 fisWith s f is = unMaybe $ map items2fis $ filter' $ runLCMmatrix is f
82 where
83 filter' = case s of
84 Nothing -> identity
85 Just fun -> filter fun
86
87 ------------------------------------------------------------------------
88 ------------------------------------------------------------------------