]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Phylo/Aggregates/Fis.hs
add a filter for fis with too few ngrams
[gargantext.git] / src / Gargantext / Viz / Phylo / Aggregates / Fis.hs
1 {-|
2 Module : Gargantext.Viz.Phylo.Tools
3 Description : Phylomemy Tools to build/manage it
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10
11 -}
12
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE OverloadedStrings #-}
16
17 module Gargantext.Viz.Phylo.Aggregates.Fis
18 where
19
20 import Data.List (null)
21 import Data.Map (Map, empty)
22 import Data.Tuple (fst, snd)
23 import Data.Set (size)
24 import Gargantext.Prelude
25 import Gargantext.Text.Metrics.FrequentItemSet (fisWithSizePolyMap, Size(..))
26 import Gargantext.Viz.Phylo
27 import Gargantext.Viz.Phylo.Tools
28 import qualified Data.Map as Map
29
30
31 -- | To Filter Fis by support
32 filterFisBySupport :: Bool -> Int -> Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
33 filterFisBySupport keep min' m = case keep of
34 False -> Map.map (\l -> filterMinorFis min' l) m
35 True -> Map.map (\l -> keepFilled (filterMinorFis) min' l) m
36
37
38 filterFisByNgrams :: Int -> Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
39 filterFisByNgrams thr m = Map.map(\lst -> filter (\fis -> (size $ getClique fis) > thr) lst) m
40
41
42 -- | To filter Fis with small Support, to preserve nonempty periods please use : filterFisBySupport true
43 filterMinorFis :: Int -> [PhyloFis] -> [PhyloFis]
44 filterMinorFis min' l = filter (\fis -> getSupport fis > min') l
45
46
47 -- | To filter nested Fis
48 filterFisByNested :: Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
49 filterFisByNested = map (\l -> let cliqueMax = filterNestedSets (head' "Fis" $ map getClique l) (map getClique l) []
50 in filter (\fis -> elem (getClique fis) cliqueMax) l)
51
52
53 -- | To transform a list of Documents into a Frequent Items Set
54 docsToFis :: Map (Date, Date) [Document] -> Map (Date, Date) [PhyloFis]
55 docsToFis docs = map (\d -> let fs = Map.toList $ fisWithSizePolyMap (Segment 1 20) 1 (map text d)
56 in map (\f -> PhyloFis (fst f) (snd f) empty) fs) docs
57
58
59 -- | To process a list of Filters on top of the PhyloFis
60 processFilters :: [Filter] -> Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
61 processFilters filters phyloFis
62 | null filters = phyloFis
63 | otherwise = panic "[ERR][Viz.Phylo.LevelMaker.processFilters] please add some filters for the Fis"
64
65
66 -- | To process a list of Metrics on top of the PhyloFis
67 processMetrics :: [Metric] -> Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
68 processMetrics metrics phyloFis
69 | null metrics = phyloFis
70 | otherwise = panic "[ERR][Viz.Phylo.LevelMaker.processMetrics] please add some metrics for the Fis"
71
72
73 -- | To transform some Documents into PhyloFis and apply a List of Metrics and Filters
74 toPhyloFis :: Map (Date, Date) [Document] -> Bool -> Support -> Int -> [Metric] -> [Filter] -> Map (Date, Date) [PhyloFis]
75 toPhyloFis ds k s t ms fs = processFilters fs
76 $ processMetrics ms
77 $ filterFisByNgrams t
78 $ filterFisByNested
79 $ filterFisBySupport k s
80 $ docsToFis ds