]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Flow/Utils.hs
[REFACT/CLEAN] TextFlow
[gargantext.git] / src / Gargantext / Database / Action / Flow / Utils.hs
1 {-|
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
8 Portability : POSIX
9
10 -}
11
12
13 module Gargantext.Database.Action.Flow.Utils
14 where
15
16 import Data.Map (Map)
17 import Gargantext.Database.Admin.Types.Node
18 import Gargantext.Database.Prelude (Cmd)
19 import Gargantext.Database.Query.Table.NodeNodeNgrams
20 import Gargantext.Database.Schema.Ngrams
21 import Gargantext.Database.Types
22 import Gargantext.Prelude
23 import qualified Data.Map as DM
24
25 type DocumentWithId a = Indexed NodeId a
26
27 data DocumentIdWithNgrams a =
28 DocumentIdWithNgrams
29 { documentWithId :: DocumentWithId a
30 , documentNgrams :: Map Ngrams (Map NgramsType Int)
31 } deriving (Show)
32
33
34 docNgrams2nodeNodeNgrams :: CorpusId
35 -> DocNgrams
36 -> NodeNodeNgrams
37 docNgrams2nodeNodeNgrams cId (DocNgrams d n nt w) =
38 NodeNodeNgrams cId d n nt w
39
40 data DocNgrams = DocNgrams { dn_doc_id :: DocId
41 , dn_ngrams_id :: Int
42 , dn_ngrams_type :: NgramsTypeId
43 , dn_weight :: Double
44 }
45
46 insertDocNgramsOn :: CorpusId
47 -> [DocNgrams]
48 -> Cmd err Int
49 insertDocNgramsOn cId dn =
50 insertNodeNodeNgrams
51 $ (map (docNgrams2nodeNodeNgrams cId) dn)
52
53 insertDocNgrams :: CorpusId
54 -> Map (Indexed Int Ngrams) (Map NgramsType (Map NodeId Int))
55 -> Cmd err Int
56 insertDocNgrams cId m =
57 insertDocNgramsOn cId [ DocNgrams n (_index ng) (ngramsTypeId t) (fromIntegral i)
58 | (ng, t2n2i) <- DM.toList m
59 , (t, n2i) <- DM.toList t2n2i
60 , (n, i) <- DM.toList n2i
61 ]
62