2 Module : Gargantext.Database.NodeNgrams
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
10 NodeNgram: relation between a Node and a Ngrams
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)
17 {-# OPTIONS_GHC -fno-warn-orphans #-}
19 {-# LANGUAGE Arrows #-}
20 {-# LANGUAGE FlexibleInstances #-}
21 {-# LANGUAGE FunctionalDependencies #-}
22 {-# LANGUAGE MultiParamTypeClasses #-}
23 {-# LANGUAGE NoImplicitPrelude #-}
24 {-# LANGUAGE OverloadedStrings #-}
25 {-# LANGUAGE QuasiQuotes #-}
26 {-# LANGUAGE TemplateHaskell #-}
30 module Gargantext.Database.NodeNgram where
32 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
33 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
34 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
35 import Database.PostgreSQL.Simple.SqlQQ (sql)
36 import Gargantext.Database.Ngrams (NgramsId)
37 import Gargantext.Text.List.Types (ListId, ListTypeId)
38 import Gargantext.Database.Node (mkCmd, Cmd(..))
39 import Gargantext.Prelude
41 import qualified Database.PostgreSQL.Simple as PGS (Connection, query, Only(..))
44 data NodeNgramPoly id node_id ngram_id weight ngrams_type
45 = NodeNgram { nodeNgram_NodeNgramId :: id
46 , nodeNgram_NodeNgramNodeId :: node_id
47 , nodeNgram_NodeNgramNgramId :: ngram_id
48 , nodeNgram_NodeNgramWeight :: weight
49 , nodeNgram_NodeNgramType :: ngrams_type
54 (Maybe (Column PGInt4 ))
69 NodeNgramPoly (Maybe Int) Int Int Double Int
71 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
72 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
75 nodeNgramTable :: Table NodeNgramWrite NodeNgramRead
76 nodeNgramTable = Table "nodes_ngrams"
77 ( pNodeNgram NodeNgram
78 { nodeNgram_NodeNgramId = optional "id"
79 , nodeNgram_NodeNgramNodeId = required "node_id"
80 , nodeNgram_NodeNgramNgramId = required "ngram_id"
81 , nodeNgram_NodeNgramWeight = required "weight"
82 , nodeNgram_NodeNgramType = required "ngrams_type"
86 queryNodeNgramTable :: Query NodeNgramRead
87 queryNodeNgramTable = queryTable nodeNgramTable
89 insertNodeNgrams :: [NodeNgram] -> Cmd Int
90 insertNodeNgrams = insertNodeNgramW
91 . map (\(NodeNgram _ n g w t) ->
92 NodeNgram Nothing (pgInt4 n) (pgInt4 g)
93 (pgDouble w) (pgInt4 t)
96 insertNodeNgramW :: [NodeNgramWrite] -> Cmd Int
97 insertNodeNgramW nns =
98 mkCmd $ \c -> fromIntegral
99 <$> runInsertMany c nodeNgramTable nns
102 updateNodeNgrams :: PGS.Connection -> [(ListId, NgramsId, ListTypeId)] -> IO [PGS.Only Int]
103 updateNodeNgrams c input = PGS.query c updateQuery (PGS.Only $ Values fields $ input)
105 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
106 updateQuery = [sql| UPDATE nodes_ngrams as old SET
107 ngrams_type = new.typeList
108 from (?) as new(node_id,ngram_id,typeList)
109 WHERE old.node_id = new.node_id
110 AND old.gram_id = new.gram_id
111 RETURNING new.ngram_id