2 Module : Gargantext.Database.Flow.Utils
3 Description : Database Flow
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 module Gargantext.Database.Action.Flow.Utils
17 import qualified Data.Map as DM
19 import Gargantext.Core.Types.Individu (User(..))
20 import Gargantext.Database.Admin.Types.Node
21 import Gargantext.Database.Admin.Types.Hyperdata (Hyperdata)
22 import Gargantext.Database.Prelude (Cmd)
23 import Gargantext.Database.Query.Table.Node
24 import Gargantext.Database.Query.Table.User
25 import Gargantext.Database.Query.Table.Node.Error
26 import Gargantext.Database.Query.Table.NodeNodeNgrams
27 import Gargantext.Database.Schema.Ngrams
28 import Gargantext.Database.Schema.Node
29 import Gargantext.Prelude
31 getUserId :: HasNodeError err
34 getUserId (UserDBId uid) = pure uid
35 getUserId (RootId rid) = do
38 getUserId (UserName u ) = do
41 Just user -> pure $ userLight_id user
42 Nothing -> nodeError NoUserFound
43 getUserId UserPublic = nodeError NoUserFound
47 => (a -> Map (NgramsT Ngrams) Int)
49 -> Map (NgramsT Ngrams) (Map NodeId Int)
50 toMaps fun ns = mapNodeIdNgrams $ documentIdWithNgrams fun ns'
52 ns' = map (\(Node nId _ _ _ _ _ json) -> DocumentWithId nId json) ns
54 mapNodeIdNgrams :: Hyperdata a
55 => [DocumentIdWithNgrams a]
56 -> Map (NgramsT Ngrams) (Map NodeId Int)
57 mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
59 xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
60 n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d))
63 documentIdWithNgrams :: Hyperdata a
64 => (a -> Map (NgramsT Ngrams) Int)
66 -> [DocumentIdWithNgrams a]
67 documentIdWithNgrams f = map (\d -> DocumentIdWithNgrams d ((f . documentData) d))
70 data DocumentWithId a =
71 DocumentWithId { documentId :: NodeId
76 data DocumentIdWithNgrams a =
78 { documentWithId :: DocumentWithId a
79 , document_ngrams :: Map (NgramsT Ngrams) Int
83 docNgrams2nodeNodeNgrams :: CorpusId
86 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
87 NodeNodeNgrams cId d n nt w
89 data DocNgrams = DocNgrams { dn_doc_id :: DocId
91 , dn_ngrams_type :: NgramsTypeId
95 insertDocNgramsOn :: CorpusId
98 insertDocNgramsOn cId dn =
100 $ (map (docNgrams2nodeNodeNgrams cId) dn)
102 insertDocNgrams :: CorpusId
103 -> Map NgramsIndexed (Map NgramsType (Map NodeId Int))
105 insertDocNgrams cId m =
106 insertDocNgramsOn cId [ DocNgrams n (_ngramsId ng) (ngramsTypeId t) (fromIntegral i)
107 | (ng, t2n2i) <- DM.toList m
108 , (t, n2i) <- DM.toList t2n2i
109 , (n, i) <- DM.toList n2i