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

[REFACT] SocialLists: merging scores

parent 6160cc34
...@@ -11,30 +11,23 @@ Portability : POSIX ...@@ -11,30 +11,23 @@ Portability : POSIX
module Gargantext.Core.Text.List.Social module Gargantext.Core.Text.List.Social
where where
-- findList imports
import Gargantext.Core.Types.Individu
import Gargantext.Database.Admin.Config
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Database.Query.Tree
import Gargantext.Database.Query.Tree.Root (getRootId)
import Gargantext.Prelude
-- filterList imports
import Data.Maybe (fromMaybe)
import Data.Map (Map) import Data.Map (Map)
import Data.Set (Set) import Data.Maybe (fromMaybe)
import Data.Semigroup (Semigroup(..)) import Data.Semigroup (Semigroup(..))
import Data.Set (Set)
import Data.Text (Text) import Data.Text (Text)
import Gargantext.API.Ngrams.Tools -- (getListNgrams) import Gargantext.API.Ngrams.Tools -- (getListNgrams)
import Gargantext.API.Ngrams.Types import Gargantext.API.Ngrams.Types
import Gargantext.Core.Types.Main
import Gargantext.Database.Schema.Ngrams
import Gargantext.Core.Text.List.Social.Find import Gargantext.Core.Text.List.Social.Find
import Gargantext.Core.Text.List.Social.Group
import Gargantext.Core.Text.List.Social.ListType import Gargantext.Core.Text.List.Social.ListType
import qualified Data.List as List import Gargantext.Core.Types.Individu
import Gargantext.Core.Types.Main
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Database.Query.Tree
import Gargantext.Database.Schema.Ngrams
import Gargantext.Prelude
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
...@@ -76,8 +69,8 @@ flowSocialListByMode :: ( RepoCmdM env err m ...@@ -76,8 +69,8 @@ flowSocialListByMode :: ( RepoCmdM env err m
) )
=> [NodeId]-> NgramsType -> Set Text => [NodeId]-> NgramsType -> Set Text
-> m (Map (Maybe ListType) (Set Text)) -> m (Map (Maybe ListType) (Set Text))
flowSocialListByMode [] nt ngrams' = pure $ Map.fromList [(Nothing, ngrams')] flowSocialListByMode [] _nt ngrams' = pure $ Map.fromList [(Nothing, ngrams')]
flowSocialListByMode listIds nt ngrams' = do flowSocialListByMode listIds nt ngrams' = do
counts <- countFilterList ngrams' nt listIds Map.empty counts <- countFilterList ngrams' nt listIds Map.empty
let r = toSocialList counts ngrams' let r = toSocialList counts ngrams'
pure r pure r
......
...@@ -8,38 +8,39 @@ Stability : experimental ...@@ -8,38 +8,39 @@ Stability : experimental
Portability : POSIX Portability : POSIX
-} -}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
module Gargantext.Core.Text.List.Social.Group module Gargantext.Core.Text.List.Social.Group
where where
-- findList imports import Control.Lens
import Data.Map (Map)
import Data.Maybe (fromMaybe)
import Data.Semigroup (Semigroup(..))
import Data.Set (Set)
import Data.Text (Text)
import GHC.Generics (Generic)
import Gargantext.API.Ngrams.Types
import Gargantext.Core.Types.Individu import Gargantext.Core.Types.Individu
import Gargantext.Core.Types.Main
import Gargantext.Database.Admin.Config import Gargantext.Database.Admin.Config
import Gargantext.Database.Admin.Types.Node import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Database.Query.Tree
import Gargantext.Database.Query.Tree.Root (getRootId)
import Gargantext.Prelude import Gargantext.Prelude
-- filterList imports
import Data.Maybe (fromMaybe)
import Data.Map (Map)
import Data.Set (Set)
import Data.Semigroup (Semigroup(..))
import Data.Text (Text)
import Gargantext.API.Ngrams.Tools -- (getListNgrams)
import Gargantext.API.Ngrams.Types
import Gargantext.Core.Types.Main
import Gargantext.Database.Schema.Ngrams
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
---------------------- ------------------------------------------------------------------------
-- | Tools to inherit groupings -- | Tools to inherit groupings
---------------------- ------------------------------------------------------------------------
type Parent = Text
-- | Tools
parentUnionsMerge :: (Ord a, Ord b, Num c) parentUnionsMerge :: (Ord a, Ord b, Num c)
=> [Map a (Map b c)] => [Map a (Map b c)]
-> Map a (Map b c) -> Map a (Map b c)
...@@ -56,6 +57,9 @@ parentUnionsExcl :: Ord a ...@@ -56,6 +57,9 @@ parentUnionsExcl :: Ord a
-> Map a b -> Map a b
parentUnionsExcl = Map.unions parentUnionsExcl = Map.unions
------------------------------------------------------------------------
type Parent = Text
hasParent :: Text hasParent :: Text
-> Map Text (Map Parent Int) -> Map Text (Map Parent Int)
...@@ -64,43 +68,100 @@ hasParent t m = case Map.lookup t m of ...@@ -64,43 +68,100 @@ hasParent t m = case Map.lookup t m of
Nothing -> Nothing Nothing -> Nothing
Just m' -> (fst . fst) <$> Map.maxViewWithKey m' Just m' -> (fst . fst) <$> Map.maxViewWithKey m'
------------------------------------------------------------------------
toMapTextParent :: Set Text data FlowListScores =
-> Map Text (Map Parent Int) FlowListScores { _flc_parents :: Map Parent Int
-> [Map Text NgramsRepoElement] , _flc_lists :: Map ListType Int
-> Map Text (Map Parent Int) -- You can add any score by incrementing this type
toMapTextParent ts = foldl' (toMapTextParent' ts) -- , _flc_score :: Map Score Int
where }
deriving (Generic)
toMapTextParent' :: Set Text makeLenses ''FlowListScores
-> Map Text (Map Parent Int)
-> Map Text NgramsRepoElement
-> Map Text (Map Parent Int)
toMapTextParent' ts' to from = Set.foldl' (toMapTextParent'' ts' from) to ts'
------------------------------------------------------------------------
-- | toFlowListScores which generate Score from list of Map Text NgramsRepoElement
toMapTextParent'' :: Set Text toFlowListScores :: Set Text
-> Map Text FlowListScores
-> [Map Text NgramsRepoElement]
-> Map Text FlowListScores
toFlowListScores ts = foldl' (toFlowListScores' ts)
where
toFlowListScores' :: Set Text
-> Map Text FlowListScores
-> Map Text NgramsRepoElement
-> Map Text FlowListScores
toFlowListScores' ts' to' ngramsRepo =
Set.foldl' (toFlowListScores'' ts' ngramsRepo) to' ts'
toFlowListScores'' :: Set Text
-> Map Text NgramsRepoElement -> Map Text NgramsRepoElement
-> Map Text (Map Parent Int) -> Map Text FlowListScores
-> Text -> Text
-> Map Text (Map Parent Int) -> Map Text FlowListScores
toMapTextParent'' ss from to t = case Map.lookup t from of toFlowListScores'' ss ngramsRepo to'' t =
Nothing -> to case Map.lookup t ngramsRepo of
Just nre -> case _nre_parent nre of Nothing -> to''
Just (NgramsTerm p') -> if Set.member p' ss Just nre -> Map.alter (addParent nre ss) t
then Map.alter (addParent p') t to $ Map.alter (addList $ _nre_list nre) t to''
else to
where ------------------------------------------------------------------------
addParent p'' Nothing = Just $ addCountParent p'' Map.empty -- | Main addFunctions to FlowListScores
addParent p'' (Just ps) = Just $ addCountParent p'' ps ------------------------------------------------------------------------
addCountParent :: Parent -> Map Parent Int -> Map Parent Int -- | Very unseful but nice comment:
addCountParent p m = Map.alter addCount p m -- "this function looks like an ASCII bird"
where addList :: ListType
addCount Nothing = Just 1 -> Maybe FlowListScores
addCount (Just n) = Just $ n + 1 -> Maybe FlowListScores
addList l Nothing =
_ -> to Just $ FlowListScores Map.empty (addList' l Map.empty)
addList l (Just (FlowListScores mapParent mapList)) =
Just $ FlowListScores mapParent mapList'
where
mapList' = addList' l mapList
addList' :: ListType -> Map ListType Int -> Map ListType Int
addList' l m = Map.alter (plus l) l m
where
plus CandidateTerm Nothing = Just 1
plus CandidateTerm (Just x) = Just $ x + 1
plus _ Nothing = Just 3
plus _ (Just x) = Just $ x + 3
------------------------------------------------------------------------
------------------------------------------------------------------------
addParent :: NgramsRepoElement -> Set Text
-> Maybe FlowListScores
-> Maybe FlowListScores
addParent nre ss Nothing =
Just $ FlowListScores mapParent Map.empty
where
mapParent = addParent' (_nre_parent nre) ss Map.empty
addParent nre ss (Just (FlowListScores mapParent mapList)) =
Just $ FlowListScores mapParent' mapList
where
mapParent' = addParent' (_nre_parent nre) ss mapParent
addParent' :: Num a
=> Maybe NgramsTerm
-> Set Text
-> Map Text a
-> Map Text a
addParent' Nothing _ss mapParent = mapParent
addParent' (Just (NgramsTerm p')) ss mapParent =
if not (Set.member p' ss)
then mapParent
else Map.alter addCount p' mapParent
where
addCount Nothing = Just 1
addCount (Just n) = Just $ n + 1
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -40,14 +40,14 @@ import qualified Data.Set as Set ...@@ -40,14 +40,14 @@ import qualified Data.Set as Set
-- here we need UserList only -- here we need UserList only
countFilterList :: RepoCmdM env err m countFilterList :: RepoCmdM env err m
=> Set Text -> NgramsType -> [ListId] => Set Text -> NgramsType -> [ListId]
-> Map Text (Map ListType Int) -> Map Text (Map ListType Int)
-> m (Map Text (Map ListType Int)) -> m (Map Text (Map ListType Int))
countFilterList st nt ls input = countFilterList st nt ls input =
foldM' (\m l -> countFilterList' st nt [l] m) input ls foldM' (\m l -> countFilterList' st nt [l] m) input ls
where where
countFilterList' :: RepoCmdM env err m countFilterList' :: RepoCmdM env err m
=> Set Text -> NgramsType -> [ListId] => Set Text -> NgramsType -> [ListId]
-> Map Text (Map ListType Int) -> Map Text (Map ListType Int)
-> m (Map Text (Map ListType Int)) -> m (Map Text (Map ListType Int))
countFilterList' st nt ls input = do countFilterList' st nt ls input = do
ml <- toMapTextListType <$> getListNgrams ls nt ml <- toMapTextListType <$> getListNgrams ls nt
...@@ -60,25 +60,24 @@ toMapTextListType m = Map.fromListWith (<>) ...@@ -60,25 +60,24 @@ toMapTextListType m = Map.fromListWith (<>)
$ List.concat $ List.concat
$ map (toList m) $ map (toList m)
$ Map.toList m $ Map.toList m
where
toList :: Map Text NgramsRepoElement -> (Text, NgramsRepoElement) -> [(Text, ListType)]
toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) =
List.zip terms (List.cycle [lt'])
where
terms = [t]
-- <> maybe [] (\n -> [unNgramsTerm n]) root
-- <> maybe [] (\n -> [unNgramsTerm n]) parent
<> (map unNgramsTerm $ Map.keys children)
lt' = listOf m nre
------------------------------------------------------------------------ listOf :: Map Text NgramsRepoElement -> NgramsRepoElement -> ListType
toList :: Map Text NgramsRepoElement -> (Text, NgramsRepoElement) -> [(Text, ListType)] listOf m ng = case _nre_parent ng of
toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) = Nothing -> _nre_list ng
List.zip terms (List.cycle [lt']) Just p -> case Map.lookup (unNgramsTerm p) m of
where Just ng' -> listOf m ng'
terms = [t] Nothing -> CandidateTerm
-- <> maybe [] (\n -> [unNgramsTerm n]) root -- panic "[G.C.T.L.Social.listOf] Nothing: Should Not happen"
-- <> maybe [] (\n -> [unNgramsTerm n]) parent
<> (map unNgramsTerm $ Map.keys children)
lt' = listOf m nre
listOf :: Map Text NgramsRepoElement -> NgramsRepoElement -> ListType
listOf m ng = case _nre_parent ng of
Nothing -> _nre_list ng
Just p -> case Map.lookup (unNgramsTerm p) m of
Just ng' -> listOf m ng'
Nothing -> CandidateTerm
-- panic "[G.C.T.L.Social.listOf] Nothing: Should Not happen"
------------------------------------------------------------------------ ------------------------------------------------------------------------
countList :: Text countList :: Text
...@@ -89,14 +88,16 @@ countList t m input = case Map.lookup t m of ...@@ -89,14 +88,16 @@ countList t m input = case Map.lookup t m of
Nothing -> input Nothing -> input
Just l -> Map.alter addList t input Just l -> Map.alter addList t input
where where
addList Nothing = Just $ addCountList l Map.empty addList Nothing = Just $ addCountList l Map.empty
addList (Just lm) = Just $ addCountList l lm addList (Just lm) = Just $ addCountList l lm
addCountList :: ListType -> Map ListType Int -> Map ListType Int addCountList :: ListType -> Map ListType Int -> Map ListType Int
addCountList l m = Map.alter (plus l) l m addCountList l m = Map.alter (plus l) l m
where where
plus CandidateTerm Nothing = Just 1 plus CandidateTerm Nothing = Just 1
plus CandidateTerm (Just x) = Just $ x + 1 plus CandidateTerm (Just x) = Just $ x + 1
plus _ Nothing = Just 3
plus _ (Just x) = Just $ x + 3
plus _ Nothing = Just 3
plus _ (Just x) = Just $ x + 3
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