]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/NodeNgram.hs
[FLOW][DB][NGRAMS][Lists] Done, need to be tested with real data.
[gargantext.git] / src / Gargantext / Database / NodeNgram.hs
1 {-|
2 Module : Gargantext.Database.NodeNgram
3 Description : NodeNgram for Ngram indexation or Lists
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 NodeNgram: relation between a Node and a Ngrams
11
12 if Node is a Document then it is indexing
13 if Node is a List then it is listing (either Stop, Candidate or Map)
14
15 -}
16
17 {-# OPTIONS_GHC -fno-warn-orphans #-}
18
19 {-# LANGUAGE Arrows #-}
20 {-# LANGUAGE FlexibleInstances #-}
21 {-# LANGUAGE FunctionalDependencies #-}
22 {-# LANGUAGE MultiParamTypeClasses #-}
23 {-# LANGUAGE NoImplicitPrelude #-}
24 {-# LANGUAGE TemplateHaskell #-}
25
26
27 module Gargantext.Database.NodeNgram where
28
29 import Gargantext.Prelude
30 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
31 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
32 import Gargantext.Database.Node (mkCmd, Cmd(..))
33 import Opaleye
34
35 -- | TODO : remove id
36 data NodeNgramPoly id node_id ngram_id weight ngrams_type
37 = NodeNgram { nodeNgram_NodeNgramId :: id
38 , nodeNgram_NodeNgramNodeId :: node_id
39 , nodeNgram_NodeNgramNgramId :: ngram_id
40 , nodeNgram_NodeNgramWeight :: weight
41 , nodeNgram_NodeNgramType :: ngrams_type
42 } deriving (Show)
43
44 type NodeNgramWrite =
45 NodeNgramPoly
46 (Maybe (Column PGInt4 ))
47 (Column PGInt4 )
48 (Column PGInt4 )
49 (Column PGFloat8)
50 (Column PGInt4 )
51
52 type NodeNgramRead =
53 NodeNgramPoly
54 (Column PGInt4 )
55 (Column PGInt4 )
56 (Column PGInt4 )
57 (Column PGFloat8)
58 (Column PGInt4 )
59
60 type NodeNgram =
61 NodeNgramPoly (Maybe Int) Int Int Double Int
62
63 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
64 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
65
66
67 nodeNgramTable :: Table NodeNgramWrite NodeNgramRead
68 nodeNgramTable = Table "nodes_ngrams"
69 ( pNodeNgram NodeNgram
70 { nodeNgram_NodeNgramId = optional "id"
71 , nodeNgram_NodeNgramNodeId = required "node_id"
72 , nodeNgram_NodeNgramNgramId = required "ngram_id"
73 , nodeNgram_NodeNgramWeight = required "weight"
74 , nodeNgram_NodeNgramType = required "ngrams_type"
75 }
76 )
77
78 queryNodeNgramTable :: Query NodeNgramRead
79 queryNodeNgramTable = queryTable nodeNgramTable
80
81 insertNodeNgrams :: [NodeNgram] -> Cmd Int
82 insertNodeNgrams = insertNodeNgramW
83 . map (\(NodeNgram _ n g w t) ->
84 NodeNgram Nothing (pgInt4 n) (pgInt4 g)
85 (pgDouble w) (pgInt4 t)
86 )
87
88 insertNodeNgramW :: [NodeNgramWrite] -> Cmd Int
89 insertNodeNgramW nns =
90 mkCmd $ \c -> fromIntegral
91 <$> runInsertMany c nodeNgramTable nns
92