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

[Types] organization, using more Lenses

parent b9de7802
...@@ -14,18 +14,14 @@ Portability : POSIX ...@@ -14,18 +14,14 @@ Portability : POSIX
module Gargantext.Core.Text.List.Group.Prelude module Gargantext.Core.Text.List.Group.Prelude
where where
import Control.Lens (makeLenses, view, set) import Control.Lens (makeLenses)
import Data.Monoid
import Data.Semigroup import Data.Semigroup
import Data.Set (Set) import Data.Set (Set)
import Data.Map (Map)
import Data.Maybe (catMaybes)
import Data.Text (Text) import Data.Text (Text)
import Gargantext.Core.Types (ListType(..)) import Gargantext.Core.Types (ListType(..))
import Gargantext.Database.Admin.Types.Node (NodeId)
import Gargantext.Core.Text.List.Social.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
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Group With Scores Main Types -- | Group With Scores Main Types
...@@ -33,8 +29,8 @@ import qualified Data.Map as Map ...@@ -33,8 +29,8 @@ import qualified Data.Map as Map
-- Target : type FlowCont Text GroupedTextScores' -- Target : type FlowCont Text GroupedTextScores'
data GroupedTextScores' score = data GroupedTextScores' score =
GroupedTextScores' { _gts'_listType :: !(Maybe ListType) GroupedTextScores' { _gts'_listType :: !(Maybe ListType)
, _gts'_score :: score
, _gts'_children :: !(Set (GroupedTextScores' score)) , _gts'_children :: !(Set (GroupedTextScores' score))
, _gts'_score :: score
} deriving (Show, Ord, Eq) } deriving (Show, Ord, Eq)
makeLenses 'GroupedTextScores' makeLenses 'GroupedTextScores'
instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where
...@@ -45,8 +41,8 @@ instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where ...@@ -45,8 +41,8 @@ instance (Semigroup a, Ord a) => Semigroup (GroupedTextScores' a) where
-- | Intermediary Type -- | Intermediary Type
data GroupedWithListScores = data GroupedWithListScores =
GroupedWithListScores { _gwls_children :: !(Set Text) GroupedWithListScores { _gwls_listType :: !(Maybe ListType)
, _gwls_listType :: !(Maybe ListType) , _gwls_children :: !(Set Text)
} deriving (Show) } deriving (Show)
makeLenses ''GroupedWithListScores makeLenses ''GroupedWithListScores
instance Semigroup GroupedWithListScores where instance Semigroup GroupedWithListScores where
...@@ -55,5 +51,9 @@ instance Semigroup GroupedWithListScores where ...@@ -55,5 +51,9 @@ instance Semigroup GroupedWithListScores where
GroupedWithListScores (c1 <> c2) GroupedWithListScores (c1 <> c2)
(l1 <> l2) (l1 <> l2)
instance Monoid GroupedWithListScores where
mempty = GroupedWithListScores Nothing Set.empty
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Group With Stem Main Types -- | Group With Stem Main Types
...@@ -18,6 +18,7 @@ import Control.Lens (makeLenses, view, set) ...@@ -18,6 +18,7 @@ import Control.Lens (makeLenses, view, set)
import Data.Semigroup import Data.Semigroup
import Data.Set (Set) import Data.Set (Set)
import Data.Map (Map) import Data.Map (Map)
import Data.Monoid (mempty)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import Data.Text (Text) import Data.Text (Text)
import Gargantext.Core.Types (ListType(..)) import Gargantext.Core.Types (ListType(..))
...@@ -29,22 +30,26 @@ import qualified Data.Set as Set ...@@ -29,22 +30,26 @@ import qualified Data.Set as Set
import qualified Data.Map as Map import qualified Data.Map as Map
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Main function -- | Main function
groupWithScores' :: FlowCont Text FlowListScores groupWithScores' :: FlowCont Text FlowListScores
-> (Text -> Set NodeId) -- Map Text (Set NodeId) -> (Text -> Set NodeId) -- Map Text (Set NodeId)
-> FlowCont Text (GroupedTextScores' (Set NodeId)) -> FlowCont Text (GroupedTextScores' (Set NodeId))
groupWithScores' flc scores = FlowCont groups orphans groupWithScores' flc _scores = FlowCont groups orphans
where where
groups = toGroupedTextScores' $ view flc_scores flc groups = toGroupedTextScores' $ view flc_scores flc
orphans = (view flc_cont flc) -- parent/child relation is inherited from social lists
orphans = (view flc_cont flc)
-- orphans have been filtered already
------------------------------------------------------------------------
------------------------------------------------------------------------
toGroupedTextScores' :: Map Text FlowListScores toGroupedTextScores' :: Map Text FlowListScores
-> Map Parent (GroupedTextScores' (Set NodeId)) -> Map Parent (GroupedTextScores' (Set NodeId))
toGroupedTextScores' = toGroupedScores' . fromListScores' toGroupedTextScores' = toGroupedScores' . fromListScores'
------------------------------------------------------------------------ ------------------------------------------------------------------------
fromListScores' :: Map Text FlowListScores fromListScores' :: Map Text FlowListScores
-> Map Parent GroupedWithListScores -> Map Parent GroupedWithListScores
...@@ -52,18 +57,22 @@ fromListScores' = Map.fromListWith (<>) . (map fromScores') . Map.toList ...@@ -52,18 +57,22 @@ fromListScores' = Map.fromListWith (<>) . (map fromScores') . Map.toList
where where
fromScores' :: (Text, FlowListScores) -> (Text, GroupedWithListScores) fromScores' :: (Text, FlowListScores) -> (Text, GroupedWithListScores)
fromScores' (t, fs) = case (keyWithMaxValue $ view fls_parents fs) of fromScores' (t, fs) = case (keyWithMaxValue $ view fls_parents fs) of
Nothing -> (t, GroupedWithListScores Set.empty (keyWithMaxValue $ view fls_listType fs)) Nothing -> ( t
, set gwls_listType (keyWithMaxValue $ view fls_listType fs) mempty
)
-- Parent case: taking its listType, for now children Set is empty -- Parent case: taking its listType, for now children Set is empty
Just parent -> (parent, GroupedWithListScores (Set.singleton t) Nothing) Just parent -> (parent, set gwls_children (Set.singleton t) mempty)
-- 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
toGroupedScores' :: Map Parent GroupedWithListScores toGroupedScores' :: Map Parent GroupedWithListScores
-> Map Parent (GroupedTextScores' (Set NodeId)) -> Map Parent (GroupedTextScores' (Set NodeId))
toGroupedScores' = undefined -- Map.map (\(GroupedWithListScores c l) -> GroupedTextScores l Set.empty c) toGroupedScores' = undefined
-- Map.map (\(GroupedWithListScores c l) -> GroupedTextScores l Set.empty c)
-- toGroupedTree :: GroupedW
...@@ -133,7 +142,7 @@ addIfNotExist mapSocialScores mapScores = ...@@ -133,7 +142,7 @@ addIfNotExist mapSocialScores mapScores =
------------------------------------------------------------------------ ------------------------------------------------------------------------
fromGroupedScores :: Map Parent GroupedWithListScores fromGroupedScores :: Map Parent GroupedWithListScores
-> Map Parent (GroupedTextScores (Set NodeId)) -> Map Parent (GroupedTextScores (Set NodeId))
fromGroupedScores = Map.map (\(GroupedWithListScores c l) -> GroupedTextScores l Set.empty c) fromGroupedScores = Map.map (\(GroupedWithListScores l c) -> GroupedTextScores l Set.empty c)
------------------------------------------------------------------------ ------------------------------------------------------------------------
fromListScores :: Map Text FlowListScores -> Map Parent GroupedWithListScores fromListScores :: Map Text FlowListScores -> Map Parent GroupedWithListScores
...@@ -141,10 +150,10 @@ fromListScores = Map.fromListWith (<>) . (map fromScores') . Map.toList ...@@ -141,10 +150,10 @@ fromListScores = Map.fromListWith (<>) . (map fromScores') . Map.toList
where where
fromScores' :: (Text, FlowListScores) -> (Text, GroupedWithListScores) fromScores' :: (Text, FlowListScores) -> (Text, GroupedWithListScores)
fromScores' (t, fs) = case (keyWithMaxValue $ view fls_parents fs) of fromScores' (t, fs) = case (keyWithMaxValue $ view fls_parents fs) of
Nothing -> (t, GroupedWithListScores Set.empty (keyWithMaxValue $ view fls_listType fs)) Nothing -> (t, set gwls_listType (keyWithMaxValue $ view fls_listType fs) mempty)
-- Parent case: taking its listType, for now children Set is empty -- Parent case: taking its listType, for now children Set is empty
Just parent -> (parent, GroupedWithListScores (Set.singleton t) Nothing) Just parent -> (parent, set gwls_children (Set.singleton t) mempty)
-- 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
......
...@@ -37,8 +37,8 @@ type Parent = Text ...@@ -37,8 +37,8 @@ type Parent = Text
-- | DataType inspired by continuation Monad (but simpler) -- | DataType inspired by continuation Monad (but simpler)
data FlowCont a b = data FlowCont a b =
FlowCont { _flc_scores :: Map a b FlowCont { _flc_scores :: Map a b
, _flc_cont :: Set a , _flc_cont :: Set a
} }
instance Ord a => Monoid (FlowCont a b) where instance Ord a => Monoid (FlowCont a b) where
mempty = FlowCont Map.empty Set.empty mempty = FlowCont Map.empty Set.empty
...@@ -55,8 +55,8 @@ instance (Eq a, Ord a) => Semigroup (FlowCont a b) where ...@@ -55,8 +55,8 @@ instance (Eq a, Ord a) => Semigroup (FlowCont a b) where
-- | Datatype definition -- | Datatype definition
data FlowListScores = data FlowListScores =
FlowListScores { _fls_parents :: Map Parent Int FlowListScores { _fls_listType :: Map ListType Int
, _fls_listType :: Map ListType Int , _fls_parents :: Map Parent Int
-- You can add any score by incrementing this type -- You can add any score by incrementing this type
-- , _flc_score :: Map Score Int -- , _flc_score :: Map Score Int
} }
...@@ -78,6 +78,8 @@ instance Semigroup FlowListScores where ...@@ -78,6 +78,8 @@ instance Semigroup FlowListScores where
FlowListScores (p1 <> p2) FlowListScores (p1 <> p2)
(l1 <> l2) (l1 <> l2)
instance Monoid FlowListScores where
mempty = FlowListScores Map.empty Map.empty
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Tools to inherit groupings -- | Tools to inherit groupings
......
...@@ -72,10 +72,10 @@ addList :: ListType ...@@ -72,10 +72,10 @@ addList :: ListType
-> Maybe FlowListScores -> Maybe FlowListScores
-> Maybe FlowListScores -> Maybe FlowListScores
addList l Nothing = addList l Nothing =
Just $ FlowListScores Map.empty (addListScore l Map.empty) Just $ set fls_listType (addListScore l Map.empty) mempty
addList l (Just (FlowListScores mapParent mapList)) = addList l (Just fls) =
Just $ FlowListScores mapParent (addListScore l mapList) Just $ over fls_listType (addListScore l) fls
-- * Unseful but nice comment: -- * Unseful but nice comment:
-- "the addList function looks like an ASCII bird" -- "the addList function looks like an ASCII bird"
...@@ -101,14 +101,12 @@ addParent :: KeepAllParents -> NgramsRepoElement -> Set Text ...@@ -101,14 +101,12 @@ addParent :: KeepAllParents -> NgramsRepoElement -> Set Text
-> Maybe FlowListScores -> Maybe FlowListScores
addParent k nre ss Nothing = addParent k nre ss Nothing =
Just $ FlowListScores mapParent Map.empty Just $ FlowListScores Map.empty mapParent
where where
mapParent = addParentScore k (_nre_parent nre) ss Map.empty mapParent = addParentScore k (view nre_parent nre) ss Map.empty
addParent k nre ss (Just (FlowListScores mapParent mapList)) = addParent k nre ss (Just fls{-(FlowListScores mapList mapParent)-}) =
Just $ FlowListScores mapParent' mapList Just $ over fls_parents (addParentScore k (view nre_parent nre) ss) fls
where
mapParent' = addParentScore k (_nre_parent nre) ss mapParent
addParentScore :: Num a addParentScore :: Num a
=> KeepAllParents => KeepAllParents
......
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