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 Gargantext.Core.Types.Individu (User(..))
18 import Gargantext.Database.Query.Table.Node
19 import Gargantext.Database.Query.Table.User
20 import Gargantext.Database.Query.Table.Node.Error
21 import Gargantext.Database.Admin.Types.Node
22 import Gargantext.Database.Prelude (Cmd)
23 import Gargantext.Database.Schema.Ngrams
24 import Gargantext.Database.Schema.Node
25 import Gargantext.Database.Query.Table.NodeNodeNgrams
26 import Gargantext.Prelude
27 import qualified Data.Map as DM
29 getUserId :: HasNodeError err
32 getUserId (UserDBId uid) = pure uid
33 getUserId (RootId rid) = do
36 getUserId (UserName u ) = do
39 Just user -> pure $ userLight_id user
40 Nothing -> nodeError NoUserFound
44 => (a -> Map (NgramsT Ngrams) Int)
46 -> Map (NgramsT Ngrams) (Map NodeId Int)
47 toMaps fun ns = mapNodeIdNgrams $ documentIdWithNgrams fun ns'
49 ns' = map (\(Node nId _ _ _ _ _ json) -> DocumentWithId nId json) ns
51 mapNodeIdNgrams :: Hyperdata a
52 => [DocumentIdWithNgrams a]
53 -> Map (NgramsT Ngrams) (Map NodeId Int)
54 mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
56 xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
57 n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d))
60 documentIdWithNgrams :: Hyperdata a
61 => (a -> Map (NgramsT Ngrams) Int)
63 -> [DocumentIdWithNgrams a]
64 documentIdWithNgrams f = map (\d -> DocumentIdWithNgrams d ((f . documentData) d))
67 data DocumentWithId a =
68 DocumentWithId { documentId :: NodeId
73 data DocumentIdWithNgrams a =
75 { documentWithId :: DocumentWithId a
76 , document_ngrams :: Map (NgramsT Ngrams) Int
80 docNgrams2nodeNodeNgrams :: CorpusId
83 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
84 NodeNodeNgrams cId d n nt w
86 data DocNgrams = DocNgrams { dn_doc_id :: DocId
88 , dn_ngrams_type :: NgramsTypeId
92 insertDocNgramsOn :: CorpusId
95 insertDocNgramsOn cId dn =
97 $ (map (docNgrams2nodeNodeNgrams cId) dn)
99 insertDocNgrams :: CorpusId
100 -> Map NgramsIndexed (Map NgramsType (Map NodeId Int))
102 insertDocNgrams cId m =
103 insertDocNgramsOn cId [ DocNgrams n (_ngramsId ng) (ngramsTypeId t) (fromIntegral i)
104 | (ng, t2n2i) <- DM.toList m
105 , (t, n2i) <- DM.toList t2n2i
106 , (n, i) <- DM.toList n2i