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
46 => (a -> Map (NgramsT Ngrams) Int)
48 -> Map (NgramsT Ngrams) (Map NodeId Int)
49 toMaps fun ns = mapNodeIdNgrams $ documentIdWithNgrams fun ns'
51 ns' = map (\(Node nId _ _ _ _ _ json) -> DocumentWithId nId json) ns
53 mapNodeIdNgrams :: Hyperdata a
54 => [DocumentIdWithNgrams a]
55 -> Map (NgramsT Ngrams) (Map NodeId Int)
56 mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
58 xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
59 n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d))
62 documentIdWithNgrams :: Hyperdata a
63 => (a -> Map (NgramsT Ngrams) Int)
65 -> [DocumentIdWithNgrams a]
66 documentIdWithNgrams f = map (\d -> DocumentIdWithNgrams d ((f . documentData) d))
69 data DocumentWithId a =
70 DocumentWithId { documentId :: NodeId
75 data DocumentIdWithNgrams a =
77 { documentWithId :: DocumentWithId a
78 , document_ngrams :: Map (NgramsT Ngrams) Int
82 docNgrams2nodeNodeNgrams :: CorpusId
85 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
86 NodeNodeNgrams cId d n nt w
88 data DocNgrams = DocNgrams { dn_doc_id :: DocId
90 , dn_ngrams_type :: NgramsTypeId
94 insertDocNgramsOn :: CorpusId
97 insertDocNgramsOn cId dn =
99 $ (map (docNgrams2nodeNodeNgrams cId) dn)
101 insertDocNgrams :: CorpusId
102 -> Map NgramsIndexed (Map NgramsType (Map NodeId Int))
104 insertDocNgrams cId m =
105 insertDocNgramsOn cId [ DocNgrams n (_ngramsId ng) (ngramsTypeId t) (fromIntegral i)
106 | (ng, t2n2i) <- DM.toList m
107 , (t, n2i) <- DM.toList t2n2i
108 , (n, i) <- DM.toList n2i