Commit b9de7802 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[REFACT] FlowCont for Group with Scores (WIP).

parent 962046fb
{-|
Module : Gargantext.Core.Text.List.Group.Prelude
Description :
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
-}
{-# LANGUAGE TemplateHaskell #-}
module Gargantext.Core.Text.List.Group.Prelude
where
import Control.Lens (makeLenses, view, set)
import Data.Semigroup
import Data.Set (Set)
import Data.Map (Map)
import Data.Maybe (catMaybes)
import Data.Text (Text)
import Gargantext.Core.Types (ListType(..))
import Gargantext.Database.Admin.Types.Node (NodeId)
import Gargantext.Core.Text.List.Social.Prelude
import Gargantext.Prelude
import qualified Data.Set as Set
import qualified Data.Map as Map
------------------------------------------------------------------------
-- | Group With Scores Main Types
-- Tree of GroupedTextScores
-- Target : type FlowCont Text GroupedTextScores'
data GroupedTextScores' score =
GroupedTextScores' { _gts'_listType :: !(Maybe ListType)
, _gts'_score :: score
, _gts'_children :: !(Set (GroupedTextScores' score))
} deriving (Show, Ord, Eq)
makeLenses 'GroupedTextScores'
instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where
(<>) (GroupedTextScores' l1 s1 c1)
(GroupedTextScores' l2 s2 c2)
= GroupedTextScores' (l1 <> l2) (s1 <> s2) (c1 <> c2)
-- | Intermediary Type
data GroupedWithListScores =
GroupedWithListScores { _gwls_children :: !(Set Text)
, _gwls_listType :: !(Maybe ListType)
} deriving (Show)
makeLenses ''GroupedWithListScores
instance Semigroup GroupedWithListScores where
(<>) (GroupedWithListScores c1 l1)
(GroupedWithListScores c2 l2) =
GroupedWithListScores (c1 <> c2)
(l1 <> l2)
------------------------------------------------------------------------
-- | Group With Stem Main Types
...@@ -20,28 +20,64 @@ import Data.Set (Set) ...@@ -20,28 +20,64 @@ import Data.Set (Set)
import Data.Map (Map) import Data.Map (Map)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import Data.Text (Text) import Data.Text (Text)
import Gargantext.Core.Types (ListType(..)) -- (MasterCorpusId, UserCorpusId) import Gargantext.Core.Types (ListType(..))
import Gargantext.Database.Admin.Types.Node (NodeId) import Gargantext.Database.Admin.Types.Node (NodeId)
import Gargantext.Core.Text.List.Social.Prelude import Gargantext.Core.Text.List.Social.Prelude
import Gargantext.Core.Text.List.Group.Prelude
import Gargantext.Prelude import Gargantext.Prelude
import qualified Data.Set as Set import qualified Data.Set as Set
import qualified Data.Map as Map import qualified Data.Map as Map
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Main Types -- | Main function
data GroupedWithListScores = groupWithScores' :: FlowCont Text FlowListScores
GroupedWithListScores { _gwls_children :: !(Set Text) -> (Text -> Set NodeId) -- Map Text (Set NodeId)
, _gwls_listType :: !(Maybe ListType) -> FlowCont Text (GroupedTextScores' (Set NodeId))
} deriving (Show) groupWithScores' flc scores = FlowCont groups orphans
makeLenses ''GroupedWithListScores where
instance Semigroup GroupedWithListScores where groups = toGroupedTextScores' $ view flc_scores flc
(<>) (GroupedWithListScores c1 l1) orphans = (view flc_cont flc)
(GroupedWithListScores c2 l2) =
GroupedWithListScores (c1 <> c2)
(l1 <> l2) toGroupedTextScores' :: Map Text FlowListScores
-> Map Parent (GroupedTextScores' (Set NodeId))
------ toGroupedTextScores' = toGroupedScores' . fromListScores'
-- To be removed
------------------------------------------------------------------------
fromListScores' :: Map Text FlowListScores
-> Map Parent GroupedWithListScores
fromListScores' = Map.fromListWith (<>) . (map fromScores') . Map.toList
where
fromScores' :: (Text, FlowListScores) -> (Text, GroupedWithListScores)
fromScores' (t, fs) = case (keyWithMaxValue $ view fls_parents fs) of
Nothing -> (t, GroupedWithListScores Set.empty (keyWithMaxValue $ view fls_listType fs))
-- Parent case: taking its listType, for now children Set is empty
Just parent -> (parent, GroupedWithListScores (Set.singleton t) Nothing)
-- We ignore the ListType of children for the parents' one
-- added after and winner of semigroup actions
toGroupedScores' :: Map Parent GroupedWithListScores
-> Map Parent (GroupedTextScores' (Set NodeId))
toGroupedScores' = undefined -- Map.map (\(GroupedWithListScores c l) -> GroupedTextScores l Set.empty c)
--8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<--
-- TODO To be removed
data GroupedTextScores score = data GroupedTextScores score =
GroupedTextScores { _gts_listType :: !(Maybe ListType) GroupedTextScores { _gts_listType :: !(Maybe ListType)
, _gts_score :: score , _gts_score :: score
...@@ -49,25 +85,12 @@ data GroupedTextScores score = ...@@ -49,25 +85,12 @@ data GroupedTextScores score =
} deriving (Show) } deriving (Show)
makeLenses 'GroupedTextScores makeLenses 'GroupedTextScores
instance Semigroup a => Semigroup (GroupedTextScores a) where instance Semigroup a => Semigroup (GroupedTextScores a) where
(<>) (GroupedTextScores l1 s1 c1) (<>) (GroupedTextScores l1 s1 c1)
(GroupedTextScores l2 s2 c2) (GroupedTextScores l2 s2 c2)
= GroupedTextScores (l1 <> l2) (s1 <> s2) (c1 <> c2) = GroupedTextScores (l1 <> l2)
(s1 <> s2)
------ (c1 <> c2)
-- | Tree of GroupedTextScores
data GroupedTextScores' score =
GroupedTextScores' { _gts'_listType :: !(Maybe ListType)
, _gts'_score :: score
, _gts'_children :: !(Set (GroupedTextScores' score))
} deriving (Show, Ord, Eq)
makeLenses 'GroupedTextScores'
instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where
(<>) (GroupedTextScores' l1 s1 c1)
(GroupedTextScores' l2 s2 c2)
= GroupedTextScores' (l1 <> l2) (s1 <> s2) (c1 <> c2)
------------------------------------------------------------------------
-- | Main function -- | Main function
groupWithScores :: Map Text FlowListScores groupWithScores :: Map Text FlowListScores
-> Map Text (Set NodeId) -> Map Text (Set NodeId)
...@@ -79,20 +102,6 @@ groupWithScores scores ms = orphans <> groups ...@@ -79,20 +102,6 @@ groupWithScores scores ms = orphans <> groups
$ fromListScores scores $ fromListScores scores
orphans = addIfNotExist scores ms orphans = addIfNotExist scores ms
{-
groupWithScores :: Map Text FlowListScores
-> Map Text (Set NodeId)
-> Map Text (GroupedTextScores (Set NodeId))
groupWithScores scores ms = orphans <> groups
where
groups = addScore ms
$ fromGroupedScores
$ fromListScores scores
orphans = addIfNotExist scores ms
-}
------------------------------------------------------------------------ ------------------------------------------------------------------------
addScore :: Map Text (Set NodeId) addScore :: Map Text (Set NodeId)
-> Map Text (GroupedTextScores (Set NodeId)) -> Map Text (GroupedTextScores (Set NodeId))
...@@ -121,11 +130,6 @@ addIfNotExist mapSocialScores mapScores = ...@@ -121,11 +130,6 @@ addIfNotExist mapSocialScores mapScores =
add _ _ = Nothing -- should not be present add _ _ = Nothing -- should not be present
------------------------------------------------------------------------ ------------------------------------------------------------------------
toGroupedTextScores' :: Map Parent GroupedWithListScores
-- -> Map Text (Set NodeId)
-> Map Parent (GroupedTextScores' (Set NodeId))
toGroupedTextScores' par = undefined
------------------------------------------------------------------------ ------------------------------------------------------------------------
fromGroupedScores :: Map Parent GroupedWithListScores fromGroupedScores :: Map Parent GroupedWithListScores
-> Map Parent (GroupedTextScores (Set NodeId)) -> Map Parent (GroupedTextScores (Set NodeId))
...@@ -144,3 +148,6 @@ fromListScores = Map.fromListWith (<>) . (map fromScores') . Map.toList ...@@ -144,3 +148,6 @@ fromListScores = Map.fromListWith (<>) . (map fromScores') . Map.toList
-- We ignore the ListType of children for the parents' one -- We ignore the ListType of children for the parents' one
-- added after and winner of semigroup actions -- added after and winner of semigroup actions
--8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<-- -8<--
...@@ -8,13 +8,13 @@ Stability : experimental ...@@ -8,13 +8,13 @@ Stability : experimental
Portability : POSIX Portability : POSIX
-} -}
------------------------------------------------------------------------
{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
------------------------------------------------------------------------
module Gargantext.Core.Text.List.Social.Prelude module Gargantext.Core.Text.List.Social.Prelude
where where
......
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