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
32 mergeNgramsElement :: NgramsRepoElement -> NgramsRepoElement -> NgramsRepoElement
33 mergeNgramsElement _neOld neNew = neNew
35 type RootTerm = NgramsTerm
37 getRepo :: RepoCmdM env err m => m NgramsRepo
42 listNgramsFromRepo :: [ListId] -> NgramsType
43 -> NgramsRepo -> HashMap NgramsTerm NgramsRepoElement
44 listNgramsFromRepo nodeIds ngramsType repo = ngrams
46 ngramsMap = repo ^. r_state . at ngramsType . _Just
48 -- TODO HashMap linked
49 ngrams = HM.fromList $ Map.toList $ Map.unionsWith mergeNgramsElement
50 [ ngramsMap ^. at nodeId . _Just | nodeId <- nodeIds ]
54 -- TODO-ACCESS: We want to do the security check before entering here.
55 -- Add a static capability parameter would be nice.
56 -- Ideally this is the access to `repoVar` which needs to
57 -- be properly guarded.
58 getListNgrams :: RepoCmdM env err m
59 => [ListId] -> NgramsType
60 -> m (HashMap NgramsTerm NgramsRepoElement)
61 getListNgrams nodeIds ngramsType = listNgramsFromRepo nodeIds ngramsType <$> getRepo
63 getTermsWith :: (RepoCmdM env err m, Eq a, Hashable a)
64 => (NgramsTerm -> a) -> [ListId]
65 -> NgramsType -> ListType
67 getTermsWith f ls ngt lt = HM.fromListWith (<>)
70 <$> HM.filter (\f' -> fst f' == lt)
71 <$> mapTermListRoot ls ngt
74 toTreeWith (t, (_lt, maybeRoot)) = case maybeRoot of
76 Just r -> (f r, [f t])
78 mapTermListRoot :: [ListId]
81 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
82 mapTermListRoot nodeIds ngramsType repo =
83 (\nre -> (_nre_list nre, _nre_root nre))
84 <$> listNgramsFromRepo nodeIds ngramsType repo
86 filterListWithRootHashMap :: ListType
87 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
88 -> HashMap NgramsTerm (Maybe RootTerm)
89 filterListWithRootHashMap lt m = snd <$> HM.filter isMapTerm m
91 isMapTerm (l, maybeRoot) = case maybeRoot of
93 Just r -> case HM.lookup r m of
94 Nothing -> panic $ "Garg.API.Ngrams.Tools: filterWithRoot, unknown key: " <> unNgramsTerm r
95 Just (l',_) -> l' == lt
97 filterListWithRoot :: ListType
98 -> HashMap NgramsTerm (ListType, Maybe NgramsTerm)
99 -> HashMap NgramsTerm (Maybe RootTerm)
100 filterListWithRoot lt m = snd <$> HM.filter isMapTerm m
102 isMapTerm (l, maybeRoot) = case maybeRoot of
104 Just r -> case HM.lookup r m of
105 Nothing -> panic $ "Garg.API.Ngrams.Tools: filterWithRoot, unknown key: " <> unNgramsTerm r
106 Just (l',_) -> l' == lt
108 groupNodesByNgrams :: ( At root_map
109 , Index root_map ~ NgramsTerm
110 , IxValue root_map ~ Maybe RootTerm
113 -> HashMap NgramsTerm (Set NodeId)
114 -> HashMap NgramsTerm (Set NodeId)
115 groupNodesByNgrams syn occs = HM.fromListWith (<>) occs'
117 occs' = map toSyn (HM.toList occs)
118 toSyn (t,ns) = case syn ^. at t of
119 Nothing -> panic $ "[Garg.API.Ngrams.Tools.groupNodesByNgrams] unknown key: " <> unNgramsTerm t
124 data Diagonal = Diagonal Bool
126 getCoocByNgrams :: Diagonal -> HashMap NgramsTerm (Set NodeId) -> HashMap (NgramsTerm, NgramsTerm) Int
127 getCoocByNgrams = getCoocByNgrams' identity
130 getCoocByNgrams' :: (Hashable a, Ord a, Ord c) => (b -> Set c) -> Diagonal -> HashMap a b -> HashMap (a, a) Int
131 getCoocByNgrams' f (Diagonal diag) m =
132 HM.fromList [( (t1,t2)
133 , maybe 0 Set.size $ Set.intersection
134 <$> (fmap f $ HM.lookup t1 m)
135 <*> (fmap f $ HM.lookup t2 m)
137 | (t1,t2) <- if diag then
138 [ (x,y) | x <- ks, y <- ks, x <= y] -- TODO if we keep a Data.Map here it might be
139 -- more efficient to enumerate all the y <= x.
141 listToCombi identity ks
146 ------------------------------------------