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
12 {-# LANGUAGE FlexibleContexts #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE RankNTypes #-}
17 module Gargantext.Database.Action.Flow.Utils
21 import Gargantext.Core.Types.Individu (User(..))
22 import Gargantext.Database.Action.Query.Node
23 import Gargantext.Database.Action.Query.User
24 import Gargantext.Database.Admin.Types.Errors
25 import Gargantext.Database.Admin.Types.Node
26 import Gargantext.Database.Admin.Utils (Cmd)
27 import Gargantext.Database.Schema.Ngrams
28 import Gargantext.Database.Schema.Node
29 import Gargantext.Database.Schema.User
30 import Gargantext.Database.Schema.NodeNodeNgrams
31 import Gargantext.Prelude
32 import qualified Data.Map as DM
34 getUserId :: HasNodeError err
37 getUserId (UserDBId uid) = pure uid
38 getUserId (RootId rid) = do
41 getUserId (UserName u ) = do
44 Just user -> pure $ userLight_id user
45 Nothing -> nodeError NoUserFound
49 => (a -> Map (NgramsT Ngrams) Int)
51 -> Map (NgramsT Ngrams) (Map NodeId Int)
52 toMaps fun ns = mapNodeIdNgrams $ documentIdWithNgrams fun ns'
54 ns' = map (\(Node nId _ _ _ _ _ json) -> DocumentWithId nId json) ns
56 mapNodeIdNgrams :: Hyperdata a
57 => [DocumentIdWithNgrams a]
58 -> Map (NgramsT Ngrams) (Map NodeId Int)
59 mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
61 xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
62 n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d))
65 documentIdWithNgrams :: Hyperdata a
66 => (a -> Map (NgramsT Ngrams) Int)
68 -> [DocumentIdWithNgrams a]
69 documentIdWithNgrams f = map (\d -> DocumentIdWithNgrams d ((f . documentData) d))
72 data DocumentWithId a =
73 DocumentWithId { documentId :: NodeId
78 data DocumentIdWithNgrams a =
80 { documentWithId :: DocumentWithId a
81 , document_ngrams :: Map (NgramsT Ngrams) Int
85 docNgrams2nodeNodeNgrams :: CorpusId
88 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
89 NodeNodeNgrams cId d n nt w
91 data DocNgrams = DocNgrams { dn_doc_id :: DocId
93 , dn_ngrams_type :: NgramsTypeId
97 insertDocNgramsOn :: CorpusId
100 insertDocNgramsOn cId dn =
102 $ (map (docNgrams2nodeNodeNgrams cId) dn)
104 insertDocNgrams :: CorpusId
105 -> Map NgramsIndexed (Map NgramsType (Map NodeId Int))
107 insertDocNgrams cId m =
108 insertDocNgramsOn cId [ DocNgrams n (_ngramsId ng) (ngramsTypeId t) (fromIntegral i)
109 | (ng, t2n2i) <- DM.toList m
110 , (t, n2i) <- DM.toList t2n2i
111 , (n, i) <- DM.toList n2i