2 Module : Gargantext.Database.NodeNgramNgram
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 NodeNgramNgram table is used to group Ngrams
12 - NgramId_1, NgramId_2 where all NgramId_2 will be added to NgramId_1
13 - weight: probability of the relation (TODO, fixed to 1 for simple stemming)
16 - recursive queries of postgres
17 - group with: https://en.wikipedia.org/wiki/Nested_set_model
21 {-# OPTIONS_GHC -fno-warn-orphans #-}
22 {-# LANGUAGE Arrows #-}
23 {-# LANGUAGE FlexibleInstances #-}
24 {-# LANGUAGE FunctionalDependencies #-}
25 {-# LANGUAGE MultiParamTypeClasses #-}
26 {-# LANGUAGE NoImplicitPrelude #-}
27 {-# LANGUAGE TemplateHaskell #-}
29 module Gargantext.Database.NodeNgramNgram
32 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
33 import Data.Maybe (Maybe)
34 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
35 import Gargantext.Database.Node (mkCmd, Cmd(..))
36 import Gargantext.Prelude
38 import qualified Database.PostgreSQL.Simple as PGS
40 data NodeNgramNgramPoly node_id ngram1_id ngram2_id weight =
41 NodeNgramNgram { nng_NodeId :: node_id
42 , nng_Ngram1Id :: ngram1_id
43 , nng_Ngram2Id :: ngram2_id
44 , nng_Weight :: weight
48 type NodeNgramNgramWrite =
49 NodeNgramNgramPoly (Column PGInt4 )
52 (Maybe (Column PGFloat8))
54 type NodeNgramNgramRead =
55 NodeNgramNgramPoly (Column PGInt4 )
61 NodeNgramNgramPoly Int
66 $(makeAdaptorAndInstance "pNodeNgramNgram"
68 $(makeLensesWith abbreviatedFields
72 nodeNgramNgramTable :: Table NodeNgramNgramWrite NodeNgramNgramRead
74 Table "nodes_ngrams_ngrams"
75 ( pNodeNgramNgram NodeNgramNgram
76 { nng_NodeId = required "node_id"
77 , nng_Ngram1Id = required "ngram1_id"
78 , nng_Ngram2Id = required "ngram2_id"
79 , nng_Weight = optional "weight"
83 queryNodeNgramNgramTable :: Query NodeNgramNgramRead
84 queryNodeNgramNgramTable = queryTable nodeNgramNgramTable
86 -- | Select NodeNgramNgram
87 -- TODO not optimized (get all ngrams without filters)
88 nodeNgramNgram :: PGS.Connection -> IO [NodeNgramNgram]
89 nodeNgramNgram conn = runQuery conn queryNodeNgramNgramTable
91 instance QueryRunnerColumnDefault PGInt4 (Maybe Int) where
92 queryRunnerColumnDefault = fieldQueryRunnerColumn
94 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
95 queryRunnerColumnDefault = fieldQueryRunnerColumn
98 insertNodeNgramNgram :: [NodeNgramNgram] -> Cmd Int
99 insertNodeNgramNgram = insertNodeNgramNgramW
100 . map (\(NodeNgramNgram n ng1 ng2 maybeWeight) ->
101 NodeNgramNgram (pgInt4 n)
104 (pgDouble <$> maybeWeight)
108 insertNodeNgramNgramW :: [NodeNgramNgramWrite] -> Cmd Int
109 insertNodeNgramNgramW ns =
110 mkCmd $ \c -> fromIntegral
111 <$> runInsertMany c nodeNgramNgramTable ns