2 Module : Gargantext.Database.Schema.NodeNgrams
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 NodeNgrams register Context of Ngrams (named Cgrams then)
15 {-# OPTIONS_GHC -fno-warn-orphans #-}
17 {-# LANGUAGE Arrows #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE QuasiQuotes #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE OverloadedStrings #-}
24 {-# LANGUAGE RankNTypes #-}
25 {-# LANGUAGE TemplateHaskell #-}
27 module Gargantext.Database.Schema.NodeNgrams where
29 import Data.Text (Text)
30 import qualified Database.PostgreSQL.Simple as PGS (Query, Only(..))
31 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
32 import Database.PostgreSQL.Simple.FromRow (fromRow, field)
33 import Database.PostgreSQL.Simple (FromRow)
34 import Database.PostgreSQL.Simple.SqlQQ (sql)
35 -- import Control.Lens.TH (makeLenses)
36 import Data.Maybe (Maybe, fromMaybe)
37 import Gargantext.Core.Types
38 import Gargantext.Database.Utils
39 import Gargantext.Database.Schema.Ngrams (NgramsType, NgramsTypeId, ngramsTypeId)
40 import Gargantext.Prelude
42 data NodeNgramsPoly id
51 = NodeNgrams { _nng_id :: id
52 , _nng_node_id :: node_id'
53 , _nng_node_subtype :: node_subtype
54 , _nng_ngrams_id :: ngrams_id
55 , _nng_ngrams_type :: ngrams_type
56 , _nng_ngrams_field :: ngrams_field
57 , _nng_ngrams_tag :: ngrams_tag
58 , _nng_ngrams_class :: ngrams_class
59 , _nng_ngrams_weight :: weight
63 type NodeNgramsWrite = NodeNgramsPoly (Maybe (Column (PGInt4)))
65 (Maybe (Column (PGInt4)))
67 (Maybe (Column (PGInt4)))
68 (Maybe (Column (PGInt4)))
69 (Maybe (Column (PGInt4)))
70 (Maybe (Column (PGInt4)))
71 (Maybe (Column (PGFloat8)))
73 type NodeNodeRead = NodeNgramsPoly (Column PGInt4)
83 type NodeNgramsReadNull = NodeNgramsPoly (Column (Nullable PGInt4))
84 (Column (Nullable PGInt4))
85 (Column (Nullable PGInt4))
86 (Column (Nullable PGInt4))
88 (Column (Nullable PGInt4))
89 (Column (Nullable PGInt4))
90 (Column (Nullable PGInt4))
91 (Column (Nullable PGInt4))
92 (Column (Nullable PGFloat8))
95 type NgramsField = Int
97 type NgramsClass = Int
98 type NgramsText = Text
100 -- Example of list Ngrams
101 -- type ListNgrams = NodeNgramsPoly (Maybe Int) ListType Text
104 NodeNgramsPoly (Maybe Int) NodeId ListType NgramsText
105 NgramsType (Maybe NgramsField) (Maybe NgramsTag) (Maybe NgramsClass)
108 data Returning = Returning { re_terms :: Text
109 , re_ngrams_id :: Int
113 instance FromRow Returning where
114 fromRow = Returning <$> field <*> field
116 -- insertDb :: ListId -> Map NgramsType [NgramsElemet] -> Cmd err [Result]
117 listInsertDb :: ListId
118 -> (ListId -> a -> [NodeNgramsW])
120 -> Cmd err [Returning]
121 listInsertDb l f ngs = insertNodeNgrams (f l ngs)
123 -- TODO optimize with size of ngrams
124 insertNodeNgrams :: [NodeNgramsW] -> Cmd err [Returning]
125 insertNodeNgrams nns = runPGSQuery query (PGS.Only $ Values fields nns')
127 fields = map (\t-> QualifiedIdentifier Nothing t) [ "int4","int4","text","int4"
128 ,"int4","int4","int4","int4"
130 nns' :: [(Int, ListTypeId, NgramsText, NgramsTypeId ,NgramsField, NgramsTag, NgramsClass, Double)]
131 nns' = map (\(NodeNgrams _id (NodeId node_id'') node_subtype ngrams_terms ngrams_type ngrams_field ngrams_tag ngrams_class weight)
133 , listTypeId node_subtype
135 , ngramsTypeId ngrams_type
136 , fromMaybe 0 ngrams_field
137 , fromMaybe 0 ngrams_tag
138 , fromMaybe 0 ngrams_class
145 INSERT INTO node_ngrams_ngrams nnn VALUES (node_id, node_type, ngrams_id, ngrams_type, ngrams_field, ngrams_tag, ngrams_class, weight)
146 SELECT n.node_id, n.node_type, ng.ngrams_id, n.ngrams_type, n.ngrams_field, n.ngrams_tag, n.ngrams_class, n.weight FROM (?)
147 AS n(node_id, node_type, ngrams_terms, ngrams_type, ngrams_field, ngrams_tag, ngrams_class, weight)
148 INNER JOIN ngrams as ng ON ng.terms = n.ngrams_terms
149 ON CONFLICT(node_id, ngrams_id)
150 DO UPDATE SET node_type = excluded.node_type, ngrams_type = excluded.ngrams_type, ngrams_field = excluded.ngrams_field, ngrams_tag = excluded.ngrams_tag, ngrams_class = excluded.ngrams_class, weight = excluded.weight
151 RETURNING nnn.id, n.ngrams_terms