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.Database.Admin.Types.Node
20 import Gargantext.Database.Admin.Types.Hyperdata (Hyperdata)
21 import Gargantext.Database.Prelude (Cmd)
22 import Gargantext.Database.Query.Table.NodeNodeNgrams
23 import Gargantext.Database.Schema.Ngrams
24 import Gargantext.Database.Schema.Node
25 import Gargantext.Prelude
29 => (a -> Map (NgramsT Ngrams) Int)
31 -> Map (NgramsT Ngrams) (Map NodeId Int)
32 toMaps fun ns = mapNodeIdNgrams $ documentIdWithNgrams fun ns'
34 ns' = map (\(Node nId _ _ _ _ _ _ json) -> DocumentWithId nId json) ns
36 mapNodeIdNgrams :: Hyperdata a
37 => [DocumentIdWithNgrams a]
38 -> Map (NgramsT Ngrams) (Map NodeId Int)
39 mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
41 xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
42 n2i = map (\d -> ((documentId . documentWithId) d, documentNgrams d))
45 documentIdWithNgrams :: Hyperdata a
46 => (a -> Map (NgramsT Ngrams) Int)
48 -> [DocumentIdWithNgrams a]
49 documentIdWithNgrams f = map (\d -> DocumentIdWithNgrams d ((f . documentData) d))
52 data DocumentWithId a =
53 DocumentWithId { documentId :: NodeId
58 data DocumentIdWithNgrams a =
60 { documentWithId :: DocumentWithId a
61 , documentNgrams :: Map (NgramsT Ngrams) Int
65 docNgrams2nodeNodeNgrams :: CorpusId
68 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
69 NodeNodeNgrams cId d n nt w
71 data DocNgrams = DocNgrams { dn_doc_id :: DocId
73 , dn_ngrams_type :: NgramsTypeId
77 insertDocNgramsOn :: CorpusId
80 insertDocNgramsOn cId dn =
82 $ (map (docNgrams2nodeNodeNgrams cId) dn)
84 insertDocNgrams :: CorpusId
85 -> Map NgramsIndexed (Map NgramsType (Map NodeId Int))
87 insertDocNgrams cId m =
88 insertDocNgramsOn cId [ DocNgrams n (_ngramsId ng) (ngramsTypeId t) (fromIntegral i)
89 | (ng, t2n2i) <- DM.toList m
90 , (t, n2i) <- DM.toList t2n2i
91 , (n, i) <- DM.toList n2i