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
65 -> FlowCont Text FlowListScores
66 -> m (FlowCont Text FlowListScores)
67 flowSocialList' flowPriority user nt flc =
68 mconcat <$> mapM (flowSocialListByMode' user nt flc)
69 (flowSocialListPriority flowPriority)
72 flowSocialListByMode' :: ( RepoCmdM env err m
78 -> FlowCont Text FlowListScores
80 -> m (FlowCont Text FlowListScores)
81 flowSocialListByMode' user' nt' flc' mode =
82 findListsId user' mode
83 >>= flowSocialListByModeWith nt' flc'
86 flowSocialListByModeWith :: ( RepoCmdM env err m
92 -> FlowCont Text FlowListScores
94 -> m (FlowCont Text FlowListScores)
95 flowSocialListByModeWith nt'' flc'' ns =
96 mapM (\l -> getListNgrams [l] nt'') ns
98 . toFlowListScores (keepAllParents nt'') flc''
102 ---8<-TODO-REMOVE ALL BELOW--8<--8<-- 8<-- 8<--8<--8<--
104 -- | Choice depends on Ord instance of ListType
105 -- for now : data ListType = StopTerm | CandidateTerm | MapTerm
106 -- means MapTerm > CandidateTerm > StopTerm in case of equality of counts
107 -- (we minimize errors on MapTerms if doubt)
108 -- * TODO what if equality ?
109 -- * TODO maybe use social groups too
110 toSocialList :: Map Text (Map ListType Int)
112 -> Map (Maybe ListType) (Set Text)
113 toSocialList m = Map.fromListWith (<>)
115 . Set.map (toSocialList1 m)
117 toSocialList1 :: Map Text (Map ListType Int)
119 -> (Maybe ListType, Set Text)
120 toSocialList1 m t = case Map.lookup t m of
121 Nothing -> (Nothing, Set.singleton t)
122 Just m' -> ( (fst . fst) <$> Map.maxViewWithKey m'
126 toSocialList1_testIsTrue :: Bool
127 toSocialList1_testIsTrue = result == (Just MapTerm, Set.singleton token)
129 result = toSocialList1 (Map.fromList [(token, m)]) token
131 m = Map.fromList [ (CandidateTerm, 1)
138 flowSocialList :: ( RepoCmdM env err m
143 => User -> NgramsType -> Set Text
144 -> m (Map ListType (Set Text))
145 flowSocialList user nt ngrams' = do
146 -- Here preference to privateLists (discutable: let user choice)
147 privateListIds <- findListsId user Private
148 privateLists <- flowSocialListByMode privateListIds nt ngrams'
149 -- printDebug "* privateLists *: \n" privateLists
151 sharedListIds <- findListsId user Shared
152 sharedLists <- flowSocialListByMode sharedListIds nt (termsByList CandidateTerm privateLists)
153 -- printDebug "* sharedLists *: \n" sharedLists
155 -- TODO publicMapList:
156 -- Note: if both produce 3 identic repetition => refactor mode
157 -- publicListIds <- findListsId Public user
158 -- publicLists <- flowSocialListByMode' publicListIds nt (termsByList CandidateTerm privateLists)
160 let result = parentUnionsExcl
161 [ Map.mapKeys (fromMaybe CandidateTerm) privateLists
162 , Map.mapKeys (fromMaybe CandidateTerm) sharedLists
163 -- , Map.mapKeys (fromMaybe CandidateTerm) publicLists
165 -- printDebug "* socialLists *: results \n" result
170 flowSocialListByMode :: ( RepoCmdM env err m
175 => [NodeId]-> NgramsType -> Set Text
176 -> m (Map (Maybe ListType) (Set Text))
177 flowSocialListByMode [] _nt ngrams' = pure $ Map.fromList [(Nothing, ngrams')]
178 flowSocialListByMode listIds nt ngrams' = do
179 counts <- countFilterList ngrams' nt listIds Map.empty
180 let r = toSocialList counts ngrams'