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
10 Domain Specific Language to manage Frequent Item Set (FIS)
14 module Gargantext.Ngrams.FrequentItemSet
22 import Data.List (tail, filter)
27 import Gargantext.Prelude
29 type Size = Either Int (Int, Int)
31 --data Size = Point | Segment
33 ------------------------------------------------------------------------
34 -- | Occurrence is Frequent Item Set of size 1
35 occ :: Frequency -> [[Item]] -> [Fis]
36 occ f is = fisWithSize (Left 1) f is
38 -- | Cooccurrence is Frequent Item Set of size 2
39 cooc :: Frequency -> [[Item]] -> [Fis]
40 cooc f is = fisWithSize (Left 2) f is
42 all :: Frequency -> [[Item]] -> [Fis]
43 all f is = fisWith Nothing f is
45 ------------------------------------------------------------------------
46 between :: (Int, Int) -> Frequency -> [[Item]] -> [Fis]
47 between (x,y) f is = fisWithSize (Right (x,y)) f is
49 --maximum :: Int -> Frequency -> [[Item]] -> [Fis]
50 --maximum m f is = between (0,m) f is
53 ------------------------------------------------------------------------
54 ------------------------------------------------------------------------
55 -- | Data type to type the Frequent Item Set
56 -- TODO replace List with Set in fisItemSet
57 -- be careful : risks to erase HLCM behavior
59 data Fis' a = Fis' { _fisCount :: Int
63 -- | Sugar from items to FIS
64 items2fis :: [Item] -> Maybe Fis
65 items2fis is = case head is of
67 Just h -> Just (Fis' h (tail is))
69 ------------------------------------------------------------------------
70 ------------------------------------------------------------------------
72 fisWithSize :: Size -> Frequency -> [[Item]] -> [Fis]
73 fisWithSize n f is = case n of
74 Left n' -> fisWith (Just (\x -> length x == (n'+1) )) f is
75 Right (a,b) -> fisWith (Just (\x -> cond1 a x && cond2 b x)) f is
77 cond1 a' x = length x >= a'
78 cond2 b' x = length x <= b'
81 fisWith :: Maybe ([Item] -> Bool) -> Frequency -> [[Item]] -> [Fis]
82 fisWith s f is = unMaybe $ map items2fis $ filter' $ runLCMmatrix is f
86 Just fun -> filter fun
88 ------------------------------------------------------------------------
89 ------------------------------------------------------------------------