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,7 +69,7 @@ flowSocialListByMode :: ( RepoCmdM env err m ...@@ -76,7 +69,7 @@ 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'
......
...@@ -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'
------------------------------------------------------------------------
data FlowListScores =
FlowListScores { _flc_parents :: Map Parent Int
, _flc_lists :: Map ListType Int
-- You can add any score by incrementing this type
-- , _flc_score :: Map Score Int
}
deriving (Generic)
toMapTextParent :: Set Text makeLenses ''FlowListScores
-> Map Text (Map Parent Int)
------------------------------------------------------------------------
-- | toFlowListScores which generate Score from list of Map Text NgramsRepoElement
toFlowListScores :: Set Text
-> Map Text FlowListScores
-> [Map Text NgramsRepoElement] -> [Map Text NgramsRepoElement]
-> Map Text (Map Parent Int) -> Map Text FlowListScores
toMapTextParent ts = foldl' (toMapTextParent' ts) toFlowListScores ts = foldl' (toFlowListScores' ts)
where where
toFlowListScores' :: Set Text
toMapTextParent' :: Set Text -> Map Text FlowListScores
-> Map Text (Map Parent Int)
-> Map Text NgramsRepoElement -> Map Text NgramsRepoElement
-> Map Text (Map Parent Int) -> Map Text FlowListScores
toMapTextParent' ts' to from = Set.foldl' (toMapTextParent'' ts' from) to ts' toFlowListScores' ts' to' ngramsRepo =
Set.foldl' (toFlowListScores'' ts' ngramsRepo) to' ts'
toMapTextParent'' :: Set Text 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
------------------------------------------------------------------------
-- | Main addFunctions to FlowListScores
------------------------------------------------------------------------
-- | Very unseful but nice comment:
-- "this function looks like an ASCII bird"
addList :: ListType
-> Maybe FlowListScores
-> Maybe FlowListScores
addList l Nothing =
Just $ FlowListScores Map.empty (addList' l Map.empty)
addList l (Just (FlowListScores mapParent mapList)) =
Just $ FlowListScores mapParent mapList'
where where
addParent p'' Nothing = Just $ addCountParent p'' Map.empty mapList' = addList' l mapList
addParent p'' (Just ps) = Just $ addCountParent p'' ps
addCountParent :: Parent -> Map Parent Int -> Map Parent Int addList' :: ListType -> Map ListType Int -> Map ListType Int
addCountParent p m = Map.alter addCount p m addList' l m = Map.alter (plus l) l m
where where
addCount Nothing = Just 1 plus CandidateTerm Nothing = Just 1
addCount (Just n) = Just $ n + 1 plus CandidateTerm (Just x) = Just $ x + 1
_ -> to 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
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -60,10 +60,9 @@ toMapTextListType m = Map.fromListWith (<>) ...@@ -60,10 +60,9 @@ 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 :: Map Text NgramsRepoElement -> (Text, NgramsRepoElement) -> [(Text, ListType)] toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) =
toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) =
List.zip terms (List.cycle [lt']) List.zip terms (List.cycle [lt'])
where where
terms = [t] terms = [t]
...@@ -72,8 +71,8 @@ toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) = ...@@ -72,8 +71,8 @@ toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) =
<> (map unNgramsTerm $ Map.keys children) <> (map unNgramsTerm $ Map.keys children)
lt' = listOf m nre lt' = listOf m nre
listOf :: Map Text NgramsRepoElement -> NgramsRepoElement -> ListType listOf :: Map Text NgramsRepoElement -> NgramsRepoElement -> ListType
listOf m ng = case _nre_parent ng of listOf m ng = case _nre_parent ng of
Nothing -> _nre_list ng Nothing -> _nre_list ng
Just p -> case Map.lookup (unNgramsTerm p) m of Just p -> case Map.lookup (unNgramsTerm p) m of
Just ng' -> listOf m ng' Just ng' -> listOf m ng'
...@@ -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 _ Nothing = Just 3
plus _ (Just x) = Just $ x + 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