2 Module : Gargantext.Text.Metrics.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 {-# LANGUAGE NoImplicitPrelude #-}
16 module Gargantext.Text.Metrics.FrequentItemSet
29 import Prelude (Functor(..)) -- TODO
30 import qualified Data.Map.Strict as Map
31 import Data.Map.Strict (Map)
32 import qualified Data.Set as Set
34 import qualified Data.Vector as V
36 import Data.List (filter, concat, null)
37 import Data.Maybe (catMaybes)
41 import Gargantext.Prelude
43 data Size = Point Int | Segment Int Int
45 ------------------------------------------------------------------------
46 -- | Occurrence is Frequent Item Set of size 1
47 occ_hlcm :: Frequency -> [[Item]] -> [Fis]
48 occ_hlcm = fisWithSize (Point 1)
50 -- | Cooccurrence is Frequent Item Set of size 2
51 cooc_hlcm :: Frequency -> [[Item]] -> [Fis]
52 cooc_hlcm = fisWithSize (Point 2)
54 allFis :: Frequency -> [[Item]] -> [Fis]
55 allFis = fisWith Nothing
57 ------------------------------------------------------------------------
58 between :: (Int, Int) -> Frequency -> [[Item]] -> [Fis]
59 between (x,y) = fisWithSize (Segment x y)
61 --maximum :: Int -> Frequency -> [[Item]] -> [Fis]
62 --maximum m = between (0,m)
65 ------------------------------------------------------------------------
66 ------------------------------------------------------------------------
67 -- | Data type to type the Frequent Item Set
68 -- TODO replace List with Set in fisItemSet
69 -- be careful : risks to erase HLCM behavior
71 data Fis' a = Fis' { _fisCount :: Int
75 instance Functor Fis' where
76 fmap f (Fis' c is) = Fis' c (fmap f is)
78 -- | Sugar from items to FIS
79 items2fis :: [Item] -> Maybe Fis
80 items2fis [] = Nothing
81 items2fis (i:is) = Just $ Fis' i is
83 ------------------------------------------------------------------------
84 ------------------------------------------------------------------------
86 fisWithSize :: Size -> Frequency -> [[Item]] -> [Fis]
87 fisWithSize n f is = case n of
88 Point n' -> fisWith (Just (\x -> length x == (n'+1) )) f is
89 Segment a b -> fisWith (Just (\x -> cond a (length x) b)) f is
91 cond a' x b' = a' <= x && x <= b'
94 --- Filter on Fis and not on [Item]
95 fisWith :: Maybe ([Item] -> Bool) -> Frequency -> [[Item]] -> [Fis]
96 fisWith s f is = case filter (not . null) is of
98 js -> catMaybes $ map items2fis $ filter' $ runLCMmatrix js f
103 Just fun -> filter fun
105 -- Here the sole purpose to take the keys as a Set is tell we do not want
107 fisWithSizePoly :: Ord a => Size -> Frequency -> Set a -> [[a]] -> [Fis' a]
108 fisWithSizePoly n f ks = map (fmap fromItem) . fisWithSize n f . map (map toItem)
110 ksv = V.fromList $ Set.toList ks
111 ksm = Map.fromList . flip zip [0..] $ V.toList ksv
115 fisWithSizePoly2 :: Ord a => Size -> Frequency -> [[a]] -> [Fis' a]
116 fisWithSizePoly2 n f is = fisWithSizePoly n f ks is
118 ks = Set.fromList $ concat is
120 fisWithSizePolyMap :: Ord a => Size -> Frequency -> [[a]] -> Map (Set a) Int
121 fisWithSizePolyMap n f is =
122 Map.fromList $ (\i -> (Set.fromList (_fisItemSet i), _fisCount i)) <$> fisWithSizePoly2 n f is
125 ------------------------------------------------------------------------
126 ------------------------------------------------------------------------
130 ---- | /!\ indexes are not the same:
132 ---- | Index ngrams from Map
133 ----indexNgram :: Ord a => Map a Occ -> Map Index a
134 ----indexNgram m = fromList (zip [1..] (keys m))
136 ---- | Index ngrams from Map
137 ----ngramIndex :: Ord a => Map a Occ -> Map a Index
138 ----ngramIndex m = fromList (zip (keys m) [1..])
140 --indexWith :: Ord a => Map a Occ -> [a] -> [Int]
141 --indexWith m xs = unMaybe $ map (\x -> lookupIndex x m) xs
143 --indexIt :: Ord a => [[a]] -> (Map a Int, [[Int]])
144 --indexIt xs = (m, is)
146 -- m = sumOcc (map occ xs)
147 -- is = map (indexWith m) xs
149 --list2fis :: Ord a => FIS.Frequency -> [[a]] -> (Map a Int, [FIS.Fis])
150 --list2fis n xs = (m', fs)
152 -- (m, is) = indexIt xs
153 -- m' = M.filter (>50000) m
156 --text2fis :: FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
157 --text2fis n xs = list2fis n (map terms xs)
159 ----text2fisWith :: FIS.Size -> FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
160 ----text2fisWith = undefined