Commit c9512dee authored by Quentin Lobbé's avatar Quentin Lobbé

refactoring

parent f6f6d304
...@@ -138,14 +138,28 @@ data PhyloBranch = ...@@ -138,14 +138,28 @@ data PhyloBranch =
} }
deriving (Generic, Show) deriving (Generic, Show)
-- | PhyloPeriodId : A period of time framed by a starting Date and an ending Date
type PhyloPeriodId = (Start, End) type PhyloPeriodId = (Start, End)
type PhyloLevelId = (PhyloPeriodId, Int) -- | Level : A level of aggregation (-1 = Txt, 0 = Ngrams, 1 = Fis, [2..] = Cluster)
type PhyloGroupId = (PhyloLevelId, Int) type Level = Int
-- | Index : A generic index of an element (PhyloGroup, PhyloBranch, etc) in a given List
type Index = Int
type PhyloLevelId = (PhyloPeriodId, Level)
type PhyloGroupId = (PhyloLevelId, Index)
type PhyloBranchId = (Level, Index)
type Pointer = (PhyloGroupId, Weight) type Pointer = (PhyloGroupId, Weight)
type Weight = Double type Weight = Double
type PhyloBranchId = (Int, Int)
-- | Ngrams : a contiguous sequence of n terms -- | Ngrams : a contiguous sequence of n terms
...@@ -159,24 +173,9 @@ type Clique = Set Ngrams ...@@ -159,24 +173,9 @@ type Clique = Set Ngrams
-- | Support : Number of Documents where a Clique occurs -- | Support : Number of Documents where a Clique occurs
type Support = Int type Support = Int
-- | Fis : Frequent Items Set (ie: the association between a Clique and a Support) -- | Fis : Frequent Items Set (ie: the association between a Clique and a Support)
type Fis = Map Clique Support type Fis = (Clique,Support)
data Direction = From | To
deriving (Show, Eq)
data LevelLabel = Level_m1 | Level_0 | Level_1 | Level_mN | Level_N | Level_pN
deriving (Show, Eq, Enum, Bounded)
data Level =
Level { _levelLabel :: LevelLabel
, _levelValue :: Int
} deriving (Show, Eq)
data LevelLink =
LevelLink { _levelFrom :: Level
, _levelTo :: Level
} deriving (Show)
-- | Document : a piece of Text linked to a Date -- | Document : a piece of Text linked to a Date
data Document = Document data Document = Document
...@@ -184,6 +183,9 @@ data Document = Document ...@@ -184,6 +183,9 @@ data Document = Document
, text :: Text , text :: Text
} deriving (Show) } deriving (Show)
data PhyloError = LevelDoesNotExist data PhyloError = LevelDoesNotExist
| LevelUnassigned | LevelUnassigned
deriving (Show) deriving (Show)
...@@ -209,8 +211,6 @@ makeLenses ''Software ...@@ -209,8 +211,6 @@ makeLenses ''Software
makeLenses ''PhyloGroup makeLenses ''PhyloGroup
makeLenses ''PhyloLevel makeLenses ''PhyloLevel
makeLenses ''PhyloPeriod makeLenses ''PhyloPeriod
makeLenses ''Level
makeLenses ''LevelLink
makeLenses ''PhyloBranch makeLenses ''PhyloBranch
-- | JSON instances -- | JSON instances
......
...@@ -78,7 +78,7 @@ graphToClusters (clust,param) (nodes,edges) = case clust of ...@@ -78,7 +78,7 @@ graphToClusters (clust,param) (nodes,edges) = case clust of
phyloToClusters :: Level -> (Proximity,[Double]) -> (Clustering,[Double]) -> Phylo -> Map (Date,Date) [[PhyloGroup]] phyloToClusters :: Level -> (Proximity,[Double]) -> (Clustering,[Double]) -> Phylo -> Map (Date,Date) [[PhyloGroup]]
phyloToClusters lvl (prox,param) (clus,param') p = Map.fromList phyloToClusters lvl (prox,param) (clus,param') p = Map.fromList
$ zip (getPhyloPeriods p) $ zip (getPhyloPeriods p)
(map (\prd -> let graph = groupsToGraph (prox,param) (getGroupsWithFilters (getLevelValue lvl) prd p) p (map (\prd -> let graph = groupsToGraph (prox,param) (getGroupsWithFilters lvl prd p) p
in if null (fst graph) in if null (fst graph)
then [] then []
else graphToClusters (clus,param') graph) else graphToClusters (clus,param') graph)
...@@ -86,7 +86,7 @@ phyloToClusters lvl (prox,param) (clus,param') p = Map.fromList ...@@ -86,7 +86,7 @@ phyloToClusters lvl (prox,param) (clus,param') p = Map.fromList
-- | To transform a Cluster into a Phylogroup -- | To transform a Cluster into a Phylogroup
clusterToGroup :: PhyloPeriodId -> Int -> Int -> Text -> [PhyloGroup] -> PhyloGroup clusterToGroup :: PhyloPeriodId -> Level -> Int -> Text -> [PhyloGroup] -> PhyloGroup
clusterToGroup prd lvl idx lbl groups = PhyloGroup ((prd, lvl), idx) clusterToGroup prd lvl idx lbl groups = PhyloGroup ((prd, lvl), idx)
lbl lbl
((sort . nub . concat) $ map getGroupNgrams groups) ((sort . nub . concat) $ map getGroupNgrams groups)
...@@ -103,14 +103,12 @@ clustersToPhyloLevel lvl m p = over (phylo_periods . traverse) ...@@ -103,14 +103,12 @@ clustersToPhyloLevel lvl m p = over (phylo_periods . traverse)
clusters = zip [1..] (m ! periodId) clusters = zip [1..] (m ! periodId)
in over (phylo_periodLevels) in over (phylo_periodLevels)
(\levels -> (\levels ->
let groups = map (\cluster -> clusterToGroup periodId (getLevelValue lvl) (fst cluster) "" (snd cluster)) clusters let groups = map (\cluster -> clusterToGroup periodId lvl (fst cluster) "" (snd cluster)) clusters
in levels ++ [PhyloLevel (periodId, (getLevelValue lvl)) groups] in levels ++ [PhyloLevel (periodId, lvl) groups]
) period) p ) period) p
phyloWithGroups2 = clustersToPhyloLevel (initLevel 2 Level_N) phyloWithGroups2 = clustersToPhyloLevel 2 (phyloToClusters 1 (WeightedLogJaccard,[0]) (RelatedComponents, []) phyloWithBranches_1) phyloWithBranches_1
(phyloToClusters (initLevel 1 Level_1) (WeightedLogJaccard,[0]) (RelatedComponents, []) phyloWithBranches_1)
phyloWithBranches_1
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | STEP 12 | -- Find the Branches -- | STEP 12 | -- Find the Branches
...@@ -143,7 +141,7 @@ relatedComp idx curr (nodes,edges) next memo ...@@ -143,7 +141,7 @@ relatedComp idx curr (nodes,edges) next memo
-- | To transform a PhyloGraph into a list of PhyloBranches by using the relatedComp clustering -- | To transform a PhyloGraph into a list of PhyloBranches by using the relatedComp clustering
graphToBranches :: Int -> PhyloGraph -> Phylo -> [PhyloBranch] graphToBranches :: Level -> PhyloGraph -> Phylo -> [PhyloBranch]
graphToBranches lvl (nodes,edges) p = map (\(idx,c) -> PhyloBranch (lvl,idx) "" (map getGroupId c)) $ zip [0..] clusters graphToBranches lvl (nodes,edges) p = map (\(idx,c) -> PhyloBranch (lvl,idx) "" (map getGroupId c)) $ zip [0..] clusters
where where
-------------------------------------- --------------------------------------
...@@ -172,16 +170,10 @@ setPhyloBranches :: Level -> Phylo -> Phylo ...@@ -172,16 +170,10 @@ setPhyloBranches :: Level -> Phylo -> Phylo
setPhyloBranches lvl p = alterPhyloBranches setPhyloBranches lvl p = alterPhyloBranches
(\branches -> branches (\branches -> branches
++ ++
(graphToBranches (getLevelValue lvl) (groupsToGraph (FromPairs,[]) groups p) p) (graphToBranches lvl (groupsToGraph (FromPairs,[]) (getGroupsWithLevel lvl p)p)p))p
) p
where
--------------------------------------
groups :: [PhyloGroup]
groups = getGroupsWithLevel (getLevelValue lvl) p
--------------------------------------
phyloWithBranches_1 = setPhyloBranches (initLevel 1 Level_1) phyloWithPair_1_Childs phyloWithBranches_1 = setPhyloBranches 1 phyloWithPair_1_Childs
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -290,7 +282,7 @@ pairGroupsToGroups :: PairTo -> Level -> Double -> (Proximity,[Double]) -> Phylo ...@@ -290,7 +282,7 @@ pairGroupsToGroups :: PairTo -> Level -> Double -> (Proximity,[Double]) -> Phylo
pairGroupsToGroups to lvl thr (prox,param) p = alterPhyloGroups pairGroupsToGroups to lvl thr (prox,param) p = alterPhyloGroups
(\groups -> (\groups ->
map (\group -> map (\group ->
if (getGroupLevel group) == (getLevelValue lvl) if (getGroupLevel group) == lvl
then then
let let
-------------------------------------- --------------------------------------
...@@ -304,53 +296,31 @@ pairGroupsToGroups to lvl thr (prox,param) p = alterPhyloGroups ...@@ -304,53 +296,31 @@ pairGroupsToGroups to lvl thr (prox,param) p = alterPhyloGroups
phyloWithPair_1_Childs :: Phylo phyloWithPair_1_Childs :: Phylo
phyloWithPair_1_Childs = pairGroupsToGroups Childs (initLevel 1 Level_1) 0.01 (WeightedLogJaccard,[0]) phyloWithPair_1_Parents phyloWithPair_1_Childs = pairGroupsToGroups Childs 1 0.01 (WeightedLogJaccard,[0]) phyloWithPair_1_Parents
phyloWithPair_1_Parents :: Phylo phyloWithPair_1_Parents :: Phylo
phyloWithPair_1_Parents = pairGroupsToGroups Parents (initLevel 1 Level_1) 0.01 (WeightedLogJaccard,[0]) phyloLinked_0_1 phyloWithPair_1_Parents = pairGroupsToGroups Parents 1 0.01 (WeightedLogJaccard,[0]) phyloLinked_0_1
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | STEP 10 | -- Build the coocurency Matrix of the Phylo -- | STEP 10 | -- Build the coocurency Matrix of the Phylo
-- | Are two PhyloGroups sharing at leats one Ngrams
shareNgrams :: PhyloGroup -> PhyloGroup -> Bool
shareNgrams g g' = (not . null) $ intersect (getGroupNgrams g) (getGroupNgrams g')
-- | To get the good pair of keys (x,y) or (y,x) in a given Map (a,b) c
getKeyPair :: (Int,Int) -> Map (Int,Int) a -> (Int,Int)
getKeyPair (x,y) m = case findPair (x,y) m of
Nothing -> panic "[ERR][Viz.Phylo.Example.getKeyPair] Nothing"
Just i -> i
where
--------------------------------------
findPair :: (Int,Int) -> Map (Int,Int) a -> Maybe (Int,Int)
findPair (x,y) m
| member (x,y) m = Just (x,y)
| member (y,x) m = Just (y,x)
| otherwise = Nothing
--------------------------------------
-- | To transform the Fis into a coocurency Matrix in a Phylo -- | To transform the Fis into a coocurency Matrix in a Phylo
fisToCooc :: Map (Date, Date) Fis -> Phylo -> Map (Int, Int) Double fisToCooc :: Map (Date, Date) [Fis] -> Phylo -> Map (Int, Int) Double
fisToCooc m p = map (/docs) fisToCooc m p = map (/docs)
$ foldl (\mem x -> adjust (+1) (getKeyPair x mem) mem) cooc $ foldl (\mem x -> adjust (+1) (getKeyPair x mem) mem) cooc
$ concat $ concat
$ map (\x -> listToUnDirectedCombiWith (\x -> ngramsToIdx x p) $ (Set.toList . fst) x) fis $ map (\x -> listToUnDirectedCombiWith (\x -> ngramsToIdx x p) $ (Set.toList . fst) x)
$ (concat . elems) m
where where
--------------------------------------
fis :: [(Clique,Support)]
fis = concat $ map (\x -> Map.toList x) (elems m)
-------------------------------------- --------------------------------------
fisNgrams :: [Ngrams] fisNgrams :: [Ngrams]
fisNgrams = foldl (\mem x -> union mem $ (Set.toList . fst) x) [] fis fisNgrams = foldl (\mem x -> union mem $ (Set.toList . fst) x) [] $ (concat . elems) m
-------------------------------------- --------------------------------------
docs :: Double docs :: Double
docs = fromIntegral $ foldl (\mem x -> mem + (snd x)) 0 fis docs = fromIntegral $ foldl (\mem x -> mem + (snd x)) 0 $ (concat . elems) m
-------------------------------------- --------------------------------------
cooc :: Map (Int, Int) (Double) cooc :: Map (Int, Int) (Double)
cooc = Map.fromList $ map (\x -> (x,0)) (listToUnDirectedCombiWith (\x -> ngramsToIdx x p) fisNgrams) cooc = Map.fromList $ map (\x -> (x,0)) (listToUnDirectedCombiWith (\x -> ngramsToIdx x p) fisNgrams)
...@@ -366,13 +336,9 @@ phyloCooc = fisToCooc phyloFisFiltered phyloLinked_0_1 ...@@ -366,13 +336,9 @@ phyloCooc = fisToCooc phyloFisFiltered phyloLinked_0_1
-- | To Cliques into Groups -- | To Cliques into Groups
cliqueToGroup :: PhyloPeriodId -> Int -> Int -> Ngrams -> (Clique,Support) -> Map (Date, Date) Fis -> Phylo -> PhyloGroup cliqueToGroup :: PhyloPeriodId -> Level -> Int -> Ngrams -> (Clique,Support) -> Map (Date, Date) [Fis] -> Phylo -> PhyloGroup
cliqueToGroup period lvl idx label fis m p = PhyloGroup ((period, lvl), idx) cliqueToGroup period lvl idx label fis m p =
label PhyloGroup ((period, lvl), idx) label ngrams (singleton "support" (fromIntegral $ snd fis)) cooc [] [] [] []
ngrams
(singleton "support" (fromIntegral $ snd fis))
cooc
[] [] [] []
where where
-------------------------------------- --------------------------------------
ngrams :: [Int] ngrams :: [Int]
...@@ -382,41 +348,33 @@ cliqueToGroup period lvl idx label fis m p = PhyloGroup ((period, lvl), idx) ...@@ -382,41 +348,33 @@ cliqueToGroup period lvl idx label fis m p = PhyloGroup ((period, lvl), idx)
-------------------------------------- --------------------------------------
cooc :: Map (Int, Int) Double cooc :: Map (Int, Int) Double
cooc = filterWithKey (\k _ -> elem (fst k) ngrams && elem (snd k) ngrams) cooc = filterWithKey (\k _ -> elem (fst k) ngrams && elem (snd k) ngrams)
$ fisToCooc (restrictKeys m $ Set.fromList [period]) p $ fisToCooc (restrictKeys m $ Set.fromList [period]) p
-------------------------------------- --------------------------------------
-- | To transform Fis into PhyloLevels -- | To transform Fis into PhyloLevels
fisToPhyloLevel :: Map (Date, Date) Fis -> Phylo -> Phylo fisToPhyloLevel :: Map (Date, Date) [Fis] -> Phylo -> Phylo
fisToPhyloLevel m p = over (phylo_periods . traverse) fisToPhyloLevel m p = over (phylo_periods . traverse)
(\period -> (\period ->
let periodId = _phylo_periodId period let periodId = _phylo_periodId period
fisList = zip [1..] (Map.toList (m ! periodId)) fisList = zip [1..] (m ! periodId)
in over (phylo_periodLevels) in over (phylo_periodLevels)
(\levels -> (\phyloLevels ->
let groups = map (\fis -> cliqueToGroup periodId 1 (fst fis) "" (snd fis) m p) fisList let groups = map (\fis -> cliqueToGroup periodId 1 (fst fis) "" (snd fis) m p) fisList
in levels ++ [PhyloLevel (periodId, 1) groups] in phyloLevels ++ [PhyloLevel (periodId, 1) groups]
) period ) p ) period ) p
phyloLinked_0_1 :: Phylo phyloLinked_0_1 :: Phylo
phyloLinked_0_1 = alterLevelLinks lvl_0_1 phyloLinked_1_0 phyloLinked_0_1 = alterLevelLinks (0,1) phyloLinked_1_0
lvl_0_1 :: LevelLink
lvl_0_1 = initLevelLink (initLevel 0 Level_0) (initLevel 1 Level_1)
phyloLinked_1_0 :: Phylo phyloLinked_1_0 :: Phylo
phyloLinked_1_0 = alterLevelLinks lvl_1_0 phyloWithGroups1 phyloLinked_1_0 = alterLevelLinks (1,0) phyloWithGroups1
lvl_1_0 :: LevelLink
lvl_1_0 = initLevelLink (initLevel 1 Level_1) (initLevel 0 Level_0)
phyloWithGroups1 :: Phylo phyloWithGroups1 :: Phylo
phyloWithGroups1 = updatePhyloByLevel (initLevel 1 Level_1) phyloLinked_m1_0 phyloWithGroups1 = updatePhyloByLevel 1 phyloLinked_m1_0
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -424,50 +382,34 @@ phyloWithGroups1 = updatePhyloByLevel (initLevel 1 Level_1) phyloLinked_m1_0 ...@@ -424,50 +382,34 @@ phyloWithGroups1 = updatePhyloByLevel (initLevel 1 Level_1) phyloLinked_m1_0
-- | To Filter Fis by support -- | To Filter Fis by support
filterFisBySupport :: Bool -> Int -> Map (Date, Date) Fis -> Map (Date, Date) Fis filterFisBySupport :: Bool -> Int -> Map (Date, Date) [Fis] -> Map (Date, Date) [Fis]
filterFisBySupport empty min m = case empty of filterFisBySupport empty min m = case empty of
True -> Map.map (\fis -> filterMinorFis min fis) m True -> Map.map (\l -> filterMinorFis min l) m
False -> Map.map (\fis -> filterMinorFisNonEmpty min fis) m False -> Map.map (\l -> keepFilled (filterMinorFis) min l) m
-- | To filter Fis with small Support, to preserve nonempty periods please use : filterFisBySupport False -- | To filter Fis with small Support, to preserve nonempty periods please use : filterFisBySupport False
filterMinorFis :: Int -> Fis -> Fis filterMinorFis :: Int -> [Fis] -> [Fis]
filterMinorFis min fis = Map.filter (\s -> s > min) fis filterMinorFis min l = filter (\fis -> snd fis > min) l
-- | To filter Fis with small Support but by keeping non empty Periods
filterMinorFisNonEmpty :: Int -> Fis -> Fis
filterMinorFisNonEmpty min fis = if (Map.null fis') && (not $ Map.null fis)
then filterMinorFisNonEmpty (min - 1) fis
else fis'
where
--------------------------------------
fis' :: Fis
fis' = filterMinorFis min fis
--------------------------------------
-- | To filter nested Fis -- | To filter nested Fis
filterFisByNested :: Map (Date, Date) Fis -> Map (Date, Date) Fis filterFisByNested :: Map (Date, Date) [Fis] -> Map (Date, Date) [Fis]
filterFisByNested = map (\fis -> restrictKeys fis filterFisByNested = map (\l -> let cliqueMax = filterNestedSets (head $ map fst l) (map fst l) []
$ Set.fromList in filter (\fis -> elem (fst fis) cliqueMax) l)
$ filterNestedSets (head (keys fis)) (keys fis) []
)
-- | To transform a list of Documents into a Frequent Items Set -- | To transform a list of Documents into a Frequent Items Set
docsToFis :: Map (Date, Date) [Document] -> Map (Date, Date) Fis docsToFis :: Map (Date, Date) [Document] -> Map (Date, Date) [Fis]
docsToFis docs = map (\d -> fisWithSizePolyMap docsToFis docs = map (\d -> Map.toList
(Segment 1 20) $ fisWithSizePolyMap (Segment 1 20) 1 (map (words . text) d)) docs
1
(map (words . text) d)) docs
phyloFisFiltered :: Map (Date, Date) Fis phyloFisFiltered :: Map (Date, Date) [Fis]
phyloFisFiltered = filterFisBySupport True 1 (filterFisByNested phyloFis) phyloFisFiltered = filterFisBySupport True 1 (filterFisByNested phyloFis)
phyloFis :: Map (Date, Date) Fis phyloFis :: Map (Date, Date) [Fis]
phyloFis = docsToFis phyloPeriods phyloFis = docsToFis phyloPeriods
...@@ -476,11 +418,7 @@ phyloFis = docsToFis phyloPeriods ...@@ -476,11 +418,7 @@ phyloFis = docsToFis phyloPeriods
phyloLinked_m1_0 :: Phylo phyloLinked_m1_0 :: Phylo
phyloLinked_m1_0 = alterLevelLinks lvl_m1_0 phyloLinked_0_m1 phyloLinked_m1_0 = alterLevelLinks ((-1),0) phyloLinked_0_m1
lvl_m1_0 :: LevelLink
lvl_m1_0 = initLevelLink (initLevel (-1) Level_m1) (initLevel 0 Level_0)
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -488,10 +426,10 @@ lvl_m1_0 = initLevelLink (initLevel (-1) Level_m1) (initLevel 0 Level_0) ...@@ -488,10 +426,10 @@ lvl_m1_0 = initLevelLink (initLevel (-1) Level_m1) (initLevel 0 Level_0)
-- | To set the LevelLinks between a given PhyloGroup and a list of childs/parents PhyloGroups -- | To set the LevelLinks between a given PhyloGroup and a list of childs/parents PhyloGroups
linkGroupToGroups :: LevelLink -> PhyloGroup -> [PhyloGroup] -> PhyloGroup linkGroupToGroups :: (Level,Level) -> PhyloGroup -> [PhyloGroup] -> PhyloGroup
linkGroupToGroups lvl current targets linkGroupToGroups (lvl,lvl') current targets
| getLevelLinkValue From lvl < getLevelLinkValue To lvl = setLevelParents current | lvl < lvl' = setLevelParents current
| getLevelLinkValue From lvl > getLevelLinkValue To lvl = setLevelChilds current | lvl > lvl' = setLevelChilds current
| otherwise = current | otherwise = current
where where
-------------------------------------- --------------------------------------
...@@ -503,32 +441,29 @@ linkGroupToGroups lvl current targets ...@@ -503,32 +441,29 @@ linkGroupToGroups lvl current targets
-------------------------------------- --------------------------------------
addPointers :: [Pointer] -> [Pointer] addPointers :: [Pointer] -> [Pointer]
addPointers lp = lp ++ Maybe.mapMaybe (\target -> addPointers lp = lp ++ Maybe.mapMaybe (\target ->
if shouldLink lvl (_phylo_groupNgrams current) if shouldLink (lvl,lvl')
(_phylo_groupNgrams target ) (_phylo_groupNgrams current)
(_phylo_groupNgrams target )
then Just ((getGroupId target),1) then Just ((getGroupId target),1)
else Nothing) targets else Nothing) targets
-------------------------------------- --------------------------------------
-- | To set the LevelLinks between two lists of PhyloGroups -- | To set the LevelLinks between two lists of PhyloGroups
linkGroupsByLevel :: LevelLink -> Phylo -> [PhyloGroup] -> [PhyloGroup] linkGroupsByLevel :: (Level,Level) -> Phylo -> [PhyloGroup] -> [PhyloGroup]
linkGroupsByLevel lvl p groups = map (\group -> linkGroupsByLevel (lvl,lvl') p groups = map (\group ->
if getGroupLevel group == getLevelLinkValue From lvl if getGroupLevel group == lvl
then linkGroupToGroups lvl group (getGroupsWithFilters (getLevelLinkValue To lvl) (getGroupPeriod group) p) then linkGroupToGroups (lvl,lvl') group (getGroupsWithFilters lvl' (getGroupPeriod group) p)
else group ) groups else group) groups
-- | To set the LevelLink of all the PhyloGroups of a Phylo -- | To set the LevelLink of all the PhyloGroups of a Phylo
alterLevelLinks :: LevelLink -> Phylo -> Phylo alterLevelLinks :: (Level,Level) -> Phylo -> Phylo
alterLevelLinks lvl p = alterPhyloGroups (linkGroupsByLevel lvl p) p alterLevelLinks (lvl,lvl') p = alterPhyloGroups (linkGroupsByLevel (lvl,lvl') p) p
phyloLinked_0_m1 :: Phylo phyloLinked_0_m1 :: Phylo
phyloLinked_0_m1 = alterLevelLinks lvl_0_m1 phyloWithGroups0 phyloLinked_0_m1 = alterLevelLinks (0,(-1)) phyloWithGroups0
lvl_0_m1 :: LevelLink
lvl_0_m1 = initLevelLink (initLevel 0 Level_0) (initLevel (-1) Level_m1)
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -536,14 +471,12 @@ lvl_0_m1 = initLevelLink (initLevel 0 Level_0) (initLevel (-1) Level_m1) ...@@ -536,14 +471,12 @@ lvl_0_m1 = initLevelLink (initLevel 0 Level_0) (initLevel (-1) Level_m1)
-- | To clone the last PhyloLevel of each PhyloPeriod and update it with a new LevelValue -- | To clone the last PhyloLevel of each PhyloPeriod and update it with a new LevelValue
clonePhyloLevel :: Int -> Phylo -> Phylo clonePhyloLevel :: Level -> Phylo -> Phylo
clonePhyloLevel lvl p = alterPhyloLevels (\l -> addPhyloLevel clonePhyloLevel lvl p = alterPhyloLevels (\l -> addPhyloLevel (setPhyloLevelId lvl $ head l) l) p
(setPhyloLevelId lvl $ head l)
l) p
phyloWithGroups0 :: Phylo phyloWithGroups0 :: Phylo
phyloWithGroups0 = updatePhyloByLevel (initLevel 0 Level_0) phyloWithGroupsm1 phyloWithGroups0 = updatePhyloByLevel 0 phyloWithGroupsm1
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -551,7 +484,7 @@ phyloWithGroups0 = updatePhyloByLevel (initLevel 0 Level_0) phyloWithGroupsm1 ...@@ -551,7 +484,7 @@ phyloWithGroups0 = updatePhyloByLevel (initLevel 0 Level_0) phyloWithGroupsm1
-- | To transform a list of Documents into a PhyloLevel -- | To transform a list of Documents into a PhyloLevel
docsToPhyloLevel :: Int ->(Date, Date) -> [Document] -> Phylo -> PhyloLevel docsToPhyloLevel :: Level -> (Date, Date) -> [Document] -> Phylo -> PhyloLevel
docsToPhyloLevel lvl (d, d') docs p = initPhyloLevel docsToPhyloLevel lvl (d, d') docs p = initPhyloLevel
((d, d'), lvl) ((d, d'), lvl)
(map (\(f,s) -> initGroup [s] s f lvl d d' p) (map (\(f,s) -> initGroup [s] s f lvl d d' p)
...@@ -561,42 +494,24 @@ docsToPhyloLevel lvl (d, d') docs p = initPhyloLevel ...@@ -561,42 +494,24 @@ docsToPhyloLevel lvl (d, d') docs p = initPhyloLevel
-- | To transform a Map of Periods and Documents into a list of PhyloPeriods -- | To transform a Map of Periods and Documents into a list of PhyloPeriods
docsToPhyloPeriods :: Int -> Map (Date,Date) [Document] -> Phylo -> [PhyloPeriod] docsToPhyloPeriods :: Level -> Map (Date,Date) [Document] -> Phylo -> [PhyloPeriod]
docsToPhyloPeriods lvl docs p = map (\(id,l) -> initPhyloPeriod id l) docsToPhyloPeriods lvl docs p = map (\(id,l) -> initPhyloPeriod id l)
$ Map.toList levels $ Map.toList
where $ mapWithKey (\k v -> [docsToPhyloLevel lvl k v p]) docs
--------------------------------------
levels :: Map (Date,Date) [PhyloLevel]
levels = mapWithKey (\k v -> [docsToPhyloLevel lvl k v p]) docs
--------------------------------------
-- | To update a Phylo for a given Levels -- | To update a Phylo for a given Levels
updatePhyloByLevel :: Level -> Phylo -> Phylo updatePhyloByLevel :: Level -> Phylo -> Phylo
updatePhyloByLevel lvl p updatePhyloByLevel lvl p
= case getLevelLabel lvl of | lvl < 0 = appendPhyloPeriods (docsToPhyloPeriods lvl phyloPeriods p) p
| lvl == 0 = clonePhyloLevel lvl p
Level_m1 -> appendPhyloPeriods (docsToPhyloPeriods (getLevelValue lvl) lvlData p) p | lvl == 1 = fisToPhyloLevel phyloFisFiltered p
where | lvl > 1 = undefined
-------------------------------------- | otherwise = panic ("[ERR][Viz.Phylo.Example.updatePhyloByLevel] Level not defined")
lvlData :: Map (Date,Date) [Document]
lvlData = phyloPeriods
--------------------------------------
Level_0 -> clonePhyloLevel (getLevelValue lvl) p
Level_1 -> fisToPhyloLevel lvlData p
where
--------------------------------------
lvlData :: Map (Date, Date) Fis
lvlData = phyloFisFiltered
--------------------------------------
_ -> panic ("[ERR][Viz.Phylo.Example.updatePhyloByLevel] Level not defined")
phyloWithGroupsm1 :: Phylo phyloWithGroupsm1 :: Phylo
phyloWithGroupsm1 = updatePhyloByLevel (initLevel (-1) Level_m1) phylo phyloWithGroupsm1 = updatePhyloByLevel (-1) phylo
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -624,15 +539,15 @@ docsToPeriods f g s es = Map.fromList $ zip hs $ map (inPeriode f es) hs ...@@ -624,15 +539,15 @@ docsToPeriods f g s es = Map.fromList $ zip hs $ map (inPeriode f es) hs
-- | To parse a list of Documents by filtering on a Vector of Ngrams -- | To parse a list of Documents by filtering on a Vector of Ngrams
parseDocs :: PhyloNgrams -> [Document] -> [Document] parseDocs :: PhyloNgrams -> [Document] -> [Document]
parseDocs l docs = map (\(Document d t) -> Document d (unwords parseDocs l docs = map (\(Document d t)
$ filter (\x -> Vector.elem x l) -> Document d ( unwords
$ monoTexts t)) docs $ filter (\x -> Vector.elem x l)
$ monoTexts t)) docs
-- | To group a list of Documents by fixed periods -- | To group a list of Documents by fixed periods
groupDocsByPeriod :: Grain -> Step -> [Document] -> Phylo -> Map (Date, Date) [Document] groupDocsByPeriod :: Grain -> Step -> [Document] -> Phylo -> Map (Date, Date) [Document]
groupDocsByPeriod g s docs p = docsToPeriods date g s groupDocsByPeriod g s docs p = docsToPeriods date g s $ parseDocs (getPhyloNgrams p) docs
$ parseDocs (getPhyloNgrams p) docs
phyloPeriods :: Map (Date, Date) [Document] phyloPeriods :: Map (Date, Date) [Document]
...@@ -643,16 +558,6 @@ phyloPeriods = groupDocsByPeriod 5 3 phyloDocs phylo ...@@ -643,16 +558,6 @@ phyloPeriods = groupDocsByPeriod 5 3 phyloDocs phylo
-- | STEP 2 | -- Init an initial list of Ngrams and a Phylo -- | STEP 2 | -- Init an initial list of Ngrams and a Phylo
-- | To init a Phylomemy
initPhylo :: [Document] -> PhyloNgrams -> Phylo
initPhylo docs ngrams = Phylo (both date $ (last &&& head) docs) ngrams [] []
-- | To init a PhyloNgrams as a Vector of Ngrams
initNgrams :: [Ngrams] -> PhyloNgrams
initNgrams l = Vector.fromList $ map toLower l
phylo :: Phylo phylo :: Phylo
phylo = initPhylo phyloDocs (initNgrams actants) phylo = initPhylo phyloDocs (initNgrams actants)
......
...@@ -21,15 +21,16 @@ import Control.Lens hiding (both, Level) ...@@ -21,15 +21,16 @@ import Control.Lens hiding (both, Level)
import Data.List (filter, intersect, (++), sort, null, head, tail, last, tails, delete, nub) import Data.List (filter, intersect, (++), sort, null, head, tail, last, tails, delete, nub)
import Data.Map (Map, mapKeys, member) import Data.Map (Map, mapKeys, member)
import Data.Set (Set) import Data.Set (Set)
import Data.Text (Text) import Data.Text (Text, toLower)
import Data.Tuple.Extra import Data.Tuple.Extra
import Data.Vector (Vector,elemIndex) import Data.Vector (Vector,elemIndex)
import Gargantext.Prelude hiding (head) import Gargantext.Prelude hiding (head)
import Gargantext.Viz.Phylo import Gargantext.Viz.Phylo
import qualified Data.List as List import qualified Data.List as List
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
import qualified Data.Vector as Vector
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -201,30 +202,19 @@ getIdx x v = case (elemIndex x v) of ...@@ -201,30 +202,19 @@ getIdx x v = case (elemIndex x v) of
Just i -> i Just i -> i
-- | To get the label of a Level -- | To get the good pair of keys (x,y) or (y,x) in a given Map (a,b) c
getLevelLabel :: Level -> LevelLabel getKeyPair :: (Int,Int) -> Map (Int,Int) a -> (Int,Int)
getLevelLabel lvl = _levelLabel lvl getKeyPair (x,y) m = case findPair (x,y) m of
Nothing -> panic "[ERR][Viz.Phylo.Example.getKeyPair] Nothing"
Just i -> i
-- | To get the value of a Level where
getLevelValue :: Level -> Int --------------------------------------
getLevelValue lvl = _levelValue lvl findPair :: (Int,Int) -> Map (Int,Int) a -> Maybe (Int,Int)
findPair (x,y) m
| member (x,y) m = Just (x,y)
-- | To get the label of a LevelLink based on a Direction | member (y,x) m = Just (y,x)
getLevelLinkLabel :: Direction -> LevelLink -> LevelLabel | otherwise = Nothing
getLevelLinkLabel dir link = case dir of --------------------------------------
From -> view (levelFrom . levelLabel) link
To -> view (levelTo . levelLabel) link
_ -> panic "[ERR][Viz.Phylo.Tools.getLevelLinkLabel] Wrong direction"
-- | To get the value of a LevelLink based on a Direction
getLevelLinkValue :: Direction -> LevelLink -> Int
getLevelLinkValue dir link = case dir of
From -> view (levelFrom . levelValue) link
To -> view (levelTo . levelValue) link
_ -> panic "[ERR][Viz.Phylo.Tools.getLevelLinkValue] Wrong direction"
-- | To get the neighbours (directed/undirected) of a PhyloGroup from a list of PhyloEdges -- | To get the neighbours (directed/undirected) of a PhyloGroup from a list of PhyloEdges
...@@ -268,14 +258,14 @@ initGroup ngrams lbl idx lvl from to p = PhyloGroup ...@@ -268,14 +258,14 @@ initGroup ngrams lbl idx lvl from to p = PhyloGroup
[] [] [] [] [] [] [] []
-- | To create a Level -- | To init a PhyloNgrams as a Vector of Ngrams
initLevel :: Int -> LevelLabel -> Level initNgrams :: [Ngrams] -> PhyloNgrams
initLevel lvl lbl = Level lbl lvl initNgrams l = Vector.fromList $ map toLower l
-- | To create a LevelLink -- | To init a Phylomemy
initLevelLink :: Level -> Level -> LevelLink initPhylo :: [Document] -> PhyloNgrams -> Phylo
initLevelLink lvl lvl' = LevelLink lvl lvl' initPhylo docs ngrams = Phylo (both date $ (last &&& head) docs) ngrams [] []
-- | To create a PhyloLevel -- | To create a PhyloLevel
...@@ -288,6 +278,13 @@ initPhyloPeriod :: PhyloPeriodId -> [PhyloLevel] -> PhyloPeriod ...@@ -288,6 +278,13 @@ initPhyloPeriod :: PhyloPeriodId -> [PhyloLevel] -> PhyloPeriod
initPhyloPeriod id l = PhyloPeriod id l initPhyloPeriod id l = PhyloPeriod id l
-- | To filter Fis with small Support but by keeping non empty Periods
keepFilled :: (Int -> [a] -> [a]) -> Int -> [a] -> [a]
keepFilled f thr l = if (null $ f thr l) && (not $ null l)
then keepFilled f (thr - 1) l
else f thr l
-- | To get all combinations of a list -- | To get all combinations of a list
listToDirectedCombi :: Eq a => [a] -> [(a,a)] listToDirectedCombi :: Eq a => [a] -> [(a,a)]
listToDirectedCombi l = [(x,y) | x <- l, y <- l, x /= y] listToDirectedCombi l = [(x,y) | x <- l, y <- l, x /= y]
...@@ -322,16 +319,11 @@ setPhyloLevelId lvl' (PhyloLevel (id, lvl) groups) ...@@ -322,16 +319,11 @@ setPhyloLevelId lvl' (PhyloLevel (id, lvl) groups)
-- | To choose a LevelLink strategy based an a given Level -- | To choose a LevelLink strategy based an a given Level
shouldLink :: LevelLink -> [Int] -> [Int] -> Bool shouldLink :: (Level,Level) -> [Int] -> [Int] -> Bool
shouldLink lvl l l' shouldLink (lvl,lvl') l l'
| from <= 1 = doesContainsOrd l l' | lvl <= 1 = doesContainsOrd l l'
| from > 1 = undefined | lvl > 1 = undefined
| otherwise = panic ("[ERR][Viz.Phylo.Tools.shouldLink] LevelLink not defined") | otherwise = panic ("[ERR][Viz.Phylo.Tools.shouldLink] LevelLink not defined")
where
--------------------------------------
from :: Int
from = getLevelLinkValue From lvl
--------------------------------------
-- | To unify the keys (x,y) that Map 1 share with Map 2 such as: (x,y) <=> (y,x) -- | To unify the keys (x,y) that Map 1 share with Map 2 such as: (x,y) <=> (y,x)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment