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
11 module Gargantext.Core.Text.List.Social
15 import Data.Maybe (fromMaybe)
16 import Data.Monoid (mconcat)
18 import Data.Text (Text)
19 import Gargantext.API.Ngrams.Tools -- (getListNgrams)
20 import Gargantext.API.Ngrams.Types
21 import Gargantext.Core.Text.List.Social.Find
22 import Gargantext.Core.Text.List.Social.ListType
23 import Gargantext.Core.Text.List.Social.Prelude
24 import Gargantext.Core.Text.List.Social.Scores
25 import Gargantext.Core.Types.Individu
26 import Gargantext.Core.Types.Main
27 import Gargantext.Database.Admin.Types.Node
28 import Gargantext.Database.Prelude
29 import Gargantext.Database.Query.Table.Node.Error
30 import Gargantext.Database.Query.Tree
31 import Gargantext.Database.Schema.Ngrams
32 import Gargantext.Prelude
33 import qualified Data.Map as Map
34 import qualified Data.Set as Set
36 ------------------------------------------------------------------------
37 ------------------------------------------------------------------------
38 ------------------------------------------------------------------------
41 -- | FlowSocialListPriority
42 -- Sociological assumption: either private or others (public) first
43 -- This parameter depends on the user choice
44 data FlowSocialListPriority = MySelfFirst | OthersFirst
46 flowSocialListPriority :: FlowSocialListPriority -> [NodeMode]
47 flowSocialListPriority MySelfFirst = [Private, Shared{-, Public -}]
48 flowSocialListPriority OthersFirst = reverse $ flowSocialListPriority MySelfFirst
51 -- | We keep the parents for all ngrams but terms
52 keepAllParents :: NgramsType -> KeepAllParents
53 keepAllParents NgramsTerms = KeepAllParents False
54 keepAllParents _ = KeepAllParents True
57 ------------------------------------------------------------------------
58 flowSocialList' :: ( RepoCmdM env err m
63 => FlowSocialListPriority
66 -> m (FlowListCont Text)
67 flowSocialList' flowPriority user nt flc =
68 mconcat <$> mapM (flowSocialListByMode' user nt flc)
69 (flowSocialListPriority flowPriority)
71 ------------------------------------------------------------------------
73 flowSocialListByMode' :: ( RepoCmdM env err m
81 -> m (FlowListCont Text)
82 flowSocialListByMode' user nt flc mode =
84 >>= flowSocialListByModeWith nt flc
87 flowSocialListByModeWith :: ( RepoCmdM env err m
95 -> m (FlowListCont Text)
96 flowSocialListByModeWith nt flc ns =
97 mapM (\l -> getListNgrams [l] nt) ns
99 . toFlowListScores (keepAllParents nt) flc
103 ---8<-TODO-ALL BELOW--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-
105 -- | Choice depends on Ord instance of ListType
106 -- for now : data ListType = StopTerm | CandidateTerm | MapTerm
107 -- means MapTerm > CandidateTerm > StopTerm in case of equality of counts
108 -- (we minimize errors on MapTerms if doubt)
109 -- * TODO what if equality ?
110 -- * TODO maybe use social groups too
111 toSocialList :: Map Text (Map ListType Int)
113 -> Map (Maybe ListType) (Set Text)
114 toSocialList m = Map.fromListWith (<>)
116 . Set.map (toSocialList1 m)
118 toSocialList1 :: Map Text (Map ListType Int)
120 -> (Maybe ListType, Set Text)
121 toSocialList1 m t = case Map.lookup t m of
122 Nothing -> (Nothing, Set.singleton t)
123 Just m' -> ( (fst . fst) <$> Map.maxViewWithKey m'
127 toSocialList1_testIsTrue :: Bool
128 toSocialList1_testIsTrue = result == (Just MapTerm, Set.singleton token)
130 result = toSocialList1 (Map.fromList [(token, m)]) token
132 m = Map.fromList [ (CandidateTerm, 1)
139 flowSocialList :: ( RepoCmdM env err m
144 => User -> NgramsType -> Set Text
145 -> m (Map ListType (Set Text))
146 flowSocialList user nt ngrams' = do
147 -- Here preference to privateLists (discutable: let user choice)
148 privateListIds <- findListsId user Private
149 privateLists <- flowSocialListByMode privateListIds nt ngrams'
150 -- printDebug "* privateLists *: \n" privateLists
152 sharedListIds <- findListsId user Shared
153 sharedLists <- flowSocialListByMode sharedListIds nt (termsByList CandidateTerm privateLists)
154 -- printDebug "* sharedLists *: \n" sharedLists
156 -- TODO publicMapList:
157 -- Note: if both produce 3 identic repetition => refactor mode
158 -- publicListIds <- findListsId Public user
159 -- publicLists <- flowSocialListByMode' publicListIds nt (termsByList CandidateTerm privateLists)
161 let result = parentUnionsExcl
162 [ Map.mapKeys (fromMaybe CandidateTerm) privateLists
163 , Map.mapKeys (fromMaybe CandidateTerm) sharedLists
164 -- , Map.mapKeys (fromMaybe CandidateTerm) publicLists
166 -- printDebug "* socialLists *: results \n" result
171 flowSocialListByMode :: ( RepoCmdM env err m
176 => [NodeId]-> NgramsType -> Set Text
177 -> m (Map (Maybe ListType) (Set Text))
178 flowSocialListByMode [] _nt ngrams' = pure $ Map.fromList [(Nothing, ngrams')]
179 flowSocialListByMode listIds nt ngrams' = do
180 counts <- countFilterList ngrams' nt listIds Map.empty
181 let r = toSocialList counts ngrams'