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.Semigroup (Semigroup(..))
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.Group
24 import Gargantext.Core.Types.Individu
25 import Gargantext.Core.Types.Main
26 import Gargantext.Database.Admin.Types.Node
27 import Gargantext.Database.Prelude
28 import Gargantext.Database.Query.Table.Node.Error
29 import Gargantext.Database.Query.Tree
30 import Gargantext.Database.Schema.Ngrams
31 import Gargantext.Prelude
32 import qualified Data.Map as Map
33 import qualified Data.Set as Set
35 ------------------------------------------------------------------------
36 flowSocialList :: ( RepoCmdM env err m
41 => User -> NgramsType -> Set Text
42 -> m (Map ListType (Set Text))
43 flowSocialList user nt ngrams' = do
44 -- Here preference to privateLists (discutable: let user choice)
45 privateListIds <- findListsId Private user
46 privateLists <- flowSocialListByMode privateListIds nt ngrams'
47 -- printDebug "* privateLists *: \n" privateLists
49 sharedListIds <- findListsId Shared user
50 sharedLists <- flowSocialListByMode sharedListIds nt (termsByList CandidateTerm privateLists)
51 -- printDebug "* sharedLists *: \n" sharedLists
53 -- TODO publicMapList:
54 -- Note: if both produce 3 identic repetition => refactor mode
55 -- publicListIds <- findListsId Public user
56 -- publicLists <- flowSocialListByMode' publicListIds nt (termsByList CandidateTerm privateLists)
58 let result = parentUnionsExcl
59 [ Map.mapKeys (fromMaybe CandidateTerm) privateLists
60 , Map.mapKeys (fromMaybe CandidateTerm) sharedLists
61 -- , Map.mapKeys (fromMaybe CandidateTerm) publicLists
63 -- printDebug "* socialLists *: results \n" result
66 ------------------------------------------------------------------------
67 flowSocialListByMode :: ( RepoCmdM env err m
72 => [NodeId]-> NgramsType -> Set Text
73 -> m (Map (Maybe ListType) (Set Text))
74 flowSocialListByMode [] _nt ngrams' = pure $ Map.fromList [(Nothing, ngrams')]
75 flowSocialListByMode listIds nt ngrams' = do
76 counts <- countFilterList ngrams' nt listIds Map.empty
77 let r = toSocialList counts ngrams'
81 flowSocialListByMode' :: ( RepoCmdM env err m
86 => KeepAllParents -> [NodeId]-> NgramsType -> Set Text
87 -> m (Map Text FlowListScores)
88 flowSocialListByMode' k ns nt st = do
89 ngramsRepos <- mapM (\l -> getListNgrams [l] nt) ns
90 pure $ toFlowListScores k st Map.empty ngramsRepos
92 ------------------------------------------------------------------------
93 -- TODO: maybe use social groups too
94 -- | TODO what if equality ?
95 -- choice depends on Ord instance of ListType
96 -- for now : data ListType = StopTerm | CandidateTerm | MapTerm
97 -- means MapTerm > CandidateTerm > StopTerm in case of equality of counts
98 -- (we minimize errors on MapTerms if doubt)
99 toSocialList :: Map Text (Map ListType Int)
101 -> Map (Maybe ListType) (Set Text)
102 toSocialList m = Map.fromListWith (<>)
104 . Set.map (toSocialList1 m)
106 toSocialList1 :: Map Text (Map ListType Int)
108 -> (Maybe ListType, Set Text)
109 toSocialList1 m t = case Map.lookup t m of
110 Nothing -> (Nothing, Set.singleton t)
111 Just m' -> ( (fst . fst) <$> Map.maxViewWithKey m'
115 toSocialList1_testIsTrue :: Bool
116 toSocialList1_testIsTrue = result == (Just MapTerm, Set.singleton token)
118 result = toSocialList1 (Map.fromList [(token, m)]) token
120 m = Map.fromList [ (CandidateTerm, 1)
125 ------------------------------------------------------------------------
127 ------------------------------------------------------------------------
128 termsByList :: ListType -> (Map (Maybe ListType) (Set Text)) -> Set Text
129 termsByList CandidateTerm m = Set.unions
130 $ map (\lt -> fromMaybe Set.empty $ Map.lookup lt m)
131 [ Nothing, Just CandidateTerm ]
133 fromMaybe Set.empty $ Map.lookup (Just l) m
135 ------------------------------------------------------------------------
136 unions :: (Ord a, Semigroup a, Semigroup b, Ord b)
137 => [Map a (Set b)] -> Map a (Set b)
138 unions = invertBack . Map.unionsWith (<>) . map invertForw
140 invertForw :: (Ord b, Semigroup a) => Map a (Set b) -> Map b a
141 invertForw = Map.unionsWith (<>)
142 . (map (\(k,sets) -> Map.fromSet (\_ -> k) sets))
145 invertBack :: (Ord a, Ord b) => Map b a -> Map a (Set b)
146 invertBack = Map.fromListWith (<>)
147 . (map (\(b,a) -> (a, Set.singleton b)))
150 unions_test :: Map ListType (Set Text)
151 unions_test = unions [m1, m2]
153 m1 = Map.fromList [ (StopTerm , Set.singleton "Candidate")]
154 m2 = Map.fromList [ (CandidateTerm, Set.singleton "Candidate")
155 , (MapTerm , Set.singleton "Candidate")