2 Module : Gargantext.Core.Text.List.Social
4 Copyright : (c) CNRS, 2018-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 module Gargantext.Core.Text.List.Social
17 import Gargantext.Core.Types.Individu
18 import Gargantext.Database.Admin.Config
19 import Gargantext.Database.Admin.Types.Node
20 import Gargantext.Database.Prelude
21 import Gargantext.Database.Query.Table.Node.Error
22 import Gargantext.Database.Query.Tree
23 import Gargantext.Database.Query.Tree.Root (getRootId)
24 import Gargantext.Prelude
29 import Data.Text (Text)
30 import Gargantext.API.Ngrams
31 import Gargantext.API.Ngrams.Tools -- (getListNgrams)
32 import Gargantext.API.Ngrams.Types
33 import Gargantext.Core.Types.Main
34 import Gargantext.Database.Schema.Ngrams
35 import qualified Data.List as List
36 import qualified Data.Map as Map
37 import qualified Data.Set as Set
41 flowSocialList :: ( RepoCmdM env err m
46 => NodeMode -> User -> NgramsType -> Set Text
47 -> m (Map (Maybe ListType) (Set Text))
48 flowSocialList mode user nt ngrams' = do
49 privateMapList <- flowSocialListByMode Private user nt ngrams'
50 sharedMapList <- flowSocialListByMode Shared user nt (fromMaybe Set.empty $
54 flowSocialListByMode :: ( RepoCmdM env err m
59 => NodeMode -> User -> NgramsType -> Set Text
60 -> m (Map (Maybe ListType) (Set Text))
61 flowSocialListByMode mode user nt ngrams' = do
62 listIds <- findListsId mode user
63 counts <- countFilterList ngrams' nt listIds Map.empty
64 pure $ toSocialList counts ngrams'
66 ---------------------------------------------------------------------------
67 -- TODO: maybe use social groups too
68 toSocialList :: Map Text (Map ListType Int)
70 -> Map (Maybe ListType) (Set Text)
71 toSocialList m = Map.fromListWith (<>)
73 . Set.map (toSocialList1 m)
75 -- | TODO what if equality ?
76 -- choice depends on Ord instance of ListType
77 -- for now : data ListType = StopTerm | CandidateTerm | MapTerm
78 -- means MapTerm > CandidateTerm > StopTerm in case of equality of counts
79 -- (we minimize errors on MapTerms if doubt)
80 toSocialList1 :: Map Text (Map ListType Int)
82 -> (Maybe ListType, Set Text)
83 toSocialList1 m t = case Map.lookup t m of
84 Nothing -> (Nothing, Set.singleton t)
85 Just m' -> ( (fst . fst) <$> Map.maxViewWithKey m'
89 ---------------------------------------------------------------------------
90 -- | [ListId] does not merge the lists (it is for Master and User lists
91 -- here we need UserList only
92 countFilterList :: RepoCmdM env err m
93 => Set Text -> NgramsType -> [ListId]
94 -> Map Text (Map ListType Int)
95 -> m (Map Text (Map ListType Int))
96 countFilterList st nt ls input =
97 foldM' (\m l -> countFilterList' st nt [l] m) input ls
100 countFilterList' :: RepoCmdM env err m
101 => Set Text -> NgramsType -> [ListId]
102 -> Map Text (Map ListType Int)
103 -> m (Map Text (Map ListType Int))
104 countFilterList' st nt ls input = do
105 ml <- toMapTextListType <$> getListNgrams ls nt
106 pure $ Set.foldl' (\m t -> countList t ml m) input st
108 ---------------------------------------------------------------------------
109 toMapTextListType :: Map Text NgramsRepoElement -> Map Text ListType
110 toMapTextListType = Map.fromListWith (<>)
115 toList :: (Text, NgramsRepoElement) -> [(Text, ListType)]
116 toList (t, NgramsRepoElement _ lt root parent (MSet children)) =
117 List.zip terms (List.cycle [lt])
120 <> maybe [] (\n -> [unNgramsTerm n]) root
121 <> maybe [] (\n -> [unNgramsTerm n]) parent
122 <> (map unNgramsTerm $ Map.keys children)
124 ---------------------------------------------------------------------------
127 -> Map Text (Map ListType Int)
128 -> Map Text (Map ListType Int)
129 countList t m input = case Map.lookup t m of
131 Just l -> Map.alter addList t input
133 addList Nothing = Just $ addCount l Map.empty
134 addList (Just lm) = Just $ addCount l lm
136 addCount :: ListType -> Map ListType Int -> Map ListType Int
137 addCount l m = Map.alter plus l m
139 plus Nothing = Just 1
140 plus (Just x) = Just $ x + 1
142 ------------------------------------------------------------------------
143 findListsId :: (HasNodeError err, HasTreeError err)
144 => NodeMode -> User -> Cmd err [NodeId]
145 findListsId mode u = do
147 map _dt_nodeId <$> filter (\n -> _dt_typeId n == nodeTypeId NodeList)
148 <$> findNodes' mode r
150 findNodes' :: HasTreeError err
151 => NodeMode -> RootId
152 -> Cmd err [DbTreeNode]
153 findNodes' Private r = findNodes Private r [NodeFolderPrivate, NodeCorpus, NodeList]
154 findNodes' Shared r = findNodes Shared r [NodeFolderShared , NodeCorpus, NodeList]
155 findNodes' Public r = findNodes Public r [NodeFolderPublic , NodeCorpus, NodeList]