2 Module : Gargantext.API.Ngrams.Tools
3 Description : Tools to manage Ngrams Elements (from the API)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TypeFamilies #-}
14 module Gargantext.API.Ngrams.Tools
17 import Control.Concurrent
18 import Control.Lens (_Just, (^.), at, view, At, Index, IxValue)
19 import Control.Monad.Reader
20 import Data.HashMap.Strict (HashMap)
21 import Data.Hashable (Hashable)
24 import Gargantext.API.Ngrams.Types
25 import Gargantext.Core.Types (ListType(..), NodeId, ListId)
26 import Gargantext.Database.Schema.Ngrams (NgramsType)
27 import Gargantext.Prelude
28 import qualified Data.HashMap.Strict as HM
29 import qualified Data.Map.Strict as Map
30 import qualified Data.Set as Set
31 import Gargantext.Core.NodeStory
33 mergeNgramsElement :: NgramsRepoElement -> NgramsRepoElement -> NgramsRepoElement
34 mergeNgramsElement _neOld neNew = neNew
36 type RootTerm = NgramsTerm
39 getRepo :: RepoCmdM env err m => m NgramsRepo
45 getRepo' :: HasNodeStory env err m
46 => [ListId] -> m NodeListStory
49 v <- liftBase $ f listIds
50 v' <- liftBase $ readMVar v
54 getNodeStoryVar :: HasNodeStory env err m
55 => [ListId] -> m (MVar NodeListStory)
56 getNodeStoryVar l = do
62 getNodeListStory :: HasNodeStory env err m
63 => m ([NodeId] -> IO (MVar NodeListStory))
65 env <- view hasNodeStory
66 pure $ view nse_getter env
70 listNgramsFromRepo :: [ListId]
73 -> HashMap NgramsTerm NgramsRepoElement
74 listNgramsFromRepo nodeIds ngramsType repo =
75 HM.fromList $ Map.toList
76 $ Map.unionsWith mergeNgramsElement ngrams
82 . at ngramsType . _Just
88 -- TODO-ACCESS: We want to do the security check before entering here.
89 -- Add a static capability parameter would be nice.
90 -- Ideally this is the access to `repoVar` which needs to
91 -- be properly guarded.
92 getListNgrams :: HasNodeStory env err m
93 => [ListId] -> NgramsType
94 -> m (HashMap NgramsTerm NgramsRepoElement)
95 getListNgrams nodeIds ngramsType = listNgramsFromRepo nodeIds ngramsType
99 getTermsWith :: (HasNodeStory env err m, Eq a, Hashable a)
100 => (NgramsTerm -> a) -> [ListId]
101 -> NgramsType -> Set ListType
103 getTermsWith f ls ngt lts = HM.fromListWith (<>)
106 <$> HM.filter (\f' -> Set.member (fst f') lts)
107 <$> mapTermListRoot ls ngt
110 toTreeWith (t, (_lt, maybeRoot)) = case maybeRoot of
112 Just r -> (f r, [f t])
116 mapTermListRoot :: [ListId]
119 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
120 mapTermListRoot nodeIds ngramsType repo =
121 (\nre -> (_nre_list nre, _nre_root nre))
122 <$> listNgramsFromRepo nodeIds ngramsType repo
127 filterListWithRootHashMap :: ListType
128 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
129 -> HashMap NgramsTerm (Maybe RootTerm)
130 filterListWithRootHashMap lt m = snd <$> HM.filter isMapTerm m
132 isMapTerm (l, maybeRoot) = case maybeRoot of
134 Just r -> case HM.lookup r m of
135 Nothing -> panic $ "[Garg.API.Ngrams.Tools] filterWithRoot, unknown key: " <> unNgramsTerm r
136 Just (l',_) -> l' == lt
138 filterListWithRoot :: [ListType]
139 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
140 -> HashMap NgramsTerm (Maybe RootTerm)
141 filterListWithRoot lt m = snd <$> HM.filter isMapTerm m
143 isMapTerm (l, maybeRoot) = case maybeRoot of
145 Just r -> case HM.lookup r m of
146 Nothing -> panic $ "[Garg.API.Ngrams.Tools] filterWithRoot, unknown key: " <> unNgramsTerm r
147 Just (l',_) -> elem l' lt
149 groupNodesByNgrams :: ( At root_map
150 , Index root_map ~ NgramsTerm
151 , IxValue root_map ~ Maybe RootTerm
154 -> HashMap NgramsTerm (Set NodeId)
155 -> HashMap NgramsTerm (Set NodeId)
156 groupNodesByNgrams syn occs = HM.fromListWith (<>) occs'
158 occs' = map toSyn (HM.toList occs)
159 toSyn (t,ns) = case syn ^. at t of
160 Nothing -> panic $ "[Garg.API.Ngrams.Tools.groupNodesByNgrams] unknown key: " <> unNgramsTerm t
165 data Diagonal = Diagonal Bool
167 getCoocByNgrams :: Diagonal
168 -> HashMap NgramsTerm (Set NodeId)
169 -> HashMap (NgramsTerm, NgramsTerm) Int
170 getCoocByNgrams = getCoocByNgrams' identity
173 getCoocByNgrams' :: (Hashable a, Ord a, Ord c)
177 -> HashMap (a, a) Int
178 getCoocByNgrams' f (Diagonal diag) m =
179 HM.fromList [( (t1,t2)
180 , maybe 0 Set.size $ Set.intersection
181 <$> (fmap f $ HM.lookup t1 m)
182 <*> (fmap f $ HM.lookup t2 m)
184 | (t1,t2) <- if diag then
185 [ (x,y) | x <- ks, y <- ks, x <= y] -- TODO if we keep a Data.Map here it might be
186 -- more efficient to enumerate all the y <= x.
188 listToCombi identity ks
193 ------------------------------------------