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 NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
14 {-# LANGUAGE RankNTypes #-}
16 module Gargantext.Database.Flow.Utils
20 import qualified Data.Map as DM
21 import Gargantext.Prelude
22 import Gargantext.Database.Schema.Ngrams
23 import Gargantext.Database.Types.Node (NodeId, Node, NodePoly(..), Hyperdata)
24 import Gargantext.Database.Utils (Cmd)
25 import Gargantext.Database.Schema.NodeNodeNgrams
26 import Gargantext.Database.Types.Node
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, document_ngrams 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 , document_ngrams :: Map (NgramsT Ngrams) Int
65 docNgrams2nodeNodeNgrams :: CorpusId
68 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
69 NodeNodeNgrams Nothing 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