]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Schema/Node_NodeNgramsNodeNgrams.hs
change beta to 1 / beta
[gargantext.git] / src / Gargantext / Database / Schema / Node_NodeNgramsNodeNgrams.hs
1 {-|
2 Module : Gargantext.Database.Schema.Node_NodeNgrams_NodeNgrams
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 lgrams: listed ngrams
11
12 Node_NodeNgrams_NodeNgrams table is used to group ngrams
13 - first NodeId :: Referential / space node (corpus)
14 - NodeNgrams where Node is List
15 - lgrams1_id, lgrams2_id where all lgrams2_id will be added to lgrams1_id
16 - weight: score the relation
17
18 Next Step benchmark:
19 - recursive queries of postgres
20 - group with: https://en.wikipedia.org/wiki/Nested_set_model
21
22 -}
23
24 {-# LANGUAGE Arrows #-}
25 {-# LANGUAGE FlexibleInstances #-}
26 {-# LANGUAGE FunctionalDependencies #-}
27 {-# LANGUAGE MultiParamTypeClasses #-}
28 {-# LANGUAGE NoImplicitPrelude #-}
29 {-# LANGUAGE OverloadedStrings #-}
30 {-# LANGUAGE QuasiQuotes #-}
31 {-# LANGUAGE RankNTypes #-}
32 {-# LANGUAGE TemplateHaskell #-}
33 {-# OPTIONS_GHC -fno-warn-orphans #-}
34
35 module Gargantext.Database.Schema.Node_NodeNgramsNodeNgrams
36 where
37
38 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
39 import Data.Maybe (Maybe)
40 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
41 import Gargantext.Database.Utils (Cmd, runOpaQuery, mkCmd)
42 import Gargantext.Database.Types.Node (CorpusId)
43 import Gargantext.Database.Schema.Node (pgNodeId)
44 import Gargantext.Prelude
45 import Opaleye
46
47 data Node_NodeNgrams_NodeNgrams_Poly node_id nng1_id nng2_id weight =
48 Node_NodeNgrams_NodeNgrams { _nnn_node_id :: node_id
49 , _nnn_nng1_id :: nng1_id
50 , _nnn_nng2_id :: nng2_id
51 , _nnn_weight :: weight
52 } deriving (Show)
53
54 type Node_NodeNgrams_NodeNgrams_Write =
55 Node_NodeNgrams_NodeNgrams_Poly
56 (Column PGInt4 )
57 (Maybe (Column PGInt4 ))
58 (Column PGInt4 )
59 (Maybe (Column PGFloat8))
60
61 type Node_NodeNgrams_NodeNgrams_Read =
62 Node_NodeNgrams_NodeNgrams_Poly
63 (Column PGInt4 )
64 (Column PGInt4 )
65 (Column PGInt4 )
66 (Column PGFloat8)
67
68 type ListNgramsId = Int
69
70 type Node_NodeNgrams_NodeNgrams =
71 Node_NodeNgrams_NodeNgrams_Poly CorpusId (Maybe ListNgramsId) ListNgramsId (Maybe Double)
72
73 $(makeAdaptorAndInstance "pNode_NodeNgrams_NodeNgrams"
74 ''Node_NodeNgrams_NodeNgrams_Poly)
75 $(makeLensesWith abbreviatedFields
76 ''Node_NodeNgrams_NodeNgrams_Poly)
77
78
79 node_NodeNgrams_NodeNgrams_Table :: Table Node_NodeNgrams_NodeNgrams_Write Node_NodeNgrams_NodeNgrams_Read
80 node_NodeNgrams_NodeNgrams_Table =
81 Table "node_nodengrams_nodengrams"
82 ( pNode_NodeNgrams_NodeNgrams Node_NodeNgrams_NodeNgrams
83 { _nnn_node_id = required "node_id"
84 , _nnn_nng1_id = optional "node_ngrams1_id"
85 , _nnn_nng2_id = required "node_ngrams2_id"
86 , _nnn_weight = optional "weight"
87 }
88 )
89
90 queryNode_NodeNgrams_NodeNgrams_Table :: Query Node_NodeNgrams_NodeNgrams_Read
91 queryNode_NodeNgrams_NodeNgrams_Table = queryTable node_NodeNgrams_NodeNgrams_Table
92
93 -- | Select NodeNgramsNgrams
94 -- TODO not optimized (get all ngrams without filters)
95 node_Node_NodeNgrams_NodeNgrams :: Cmd err [Node_NodeNgrams_NodeNgrams]
96 node_Node_NodeNgrams_NodeNgrams = runOpaQuery queryNode_NodeNgrams_NodeNgrams_Table
97
98 instance QueryRunnerColumnDefault PGInt4 (Maybe Int) where
99 queryRunnerColumnDefault = fieldQueryRunnerColumn
100
101 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
102 queryRunnerColumnDefault = fieldQueryRunnerColumn
103
104
105 -- TODO: Add option on conflict
106 insert_Node_NodeNgrams_NodeNgrams :: [Node_NodeNgrams_NodeNgrams] -> Cmd err Int64
107 insert_Node_NodeNgrams_NodeNgrams = insert_Node_NodeNgrams_NodeNgrams_W
108 . map (\(Node_NodeNgrams_NodeNgrams n ng1 ng2 maybeWeight) ->
109 Node_NodeNgrams_NodeNgrams (pgNodeId n )
110 (pgInt4 <$> ng1)
111 (pgInt4 ng2)
112 (pgDouble <$> maybeWeight)
113 )
114
115 insert_Node_NodeNgrams_NodeNgrams_W :: [Node_NodeNgrams_NodeNgrams_Write] -> Cmd err Int64
116 insert_Node_NodeNgrams_NodeNgrams_W ns =
117 mkCmd $ \c -> runInsert_ c Insert { iTable = node_NodeNgrams_NodeNgrams_Table
118 , iRows = ns
119 , iReturning = rCount
120 , iOnConflict = (Just DoNothing)
121 }