]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/NodeNgramNgram.hs
[API][NGRAMS] NgramsId changes and Patch added.
[gargantext.git] / src / Gargantext / Database / NodeNgramNgram.hs
1 {-|
2 Module : Gargantext.Database.NodeNgramNgram
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 NodeNgramNgram table is used to group Ngrams
11 - NodeId :: List Id
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)
14
15 Next Step benchmark:
16 - recursive queries of postgres
17 - group with: https://en.wikipedia.org/wiki/Nested_set_model
18
19 -}
20
21 {-# OPTIONS_GHC -fno-warn-orphans #-}
22 {-# LANGUAGE Arrows #-}
23 {-# LANGUAGE FlexibleInstances #-}
24 {-# LANGUAGE FunctionalDependencies #-}
25 {-# LANGUAGE MultiParamTypeClasses #-}
26 {-# LANGUAGE NoImplicitPrelude #-}
27 {-# LANGUAGE TemplateHaskell #-}
28
29 module Gargantext.Database.NodeNgramNgram
30 where
31
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
37 import Opaleye
38 import qualified Database.PostgreSQL.Simple as PGS
39
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
45 } deriving (Show)
46
47
48 type NodeNgramNgramWrite =
49 NodeNgramNgramPoly (Column PGInt4 )
50 (Column PGInt4 )
51 (Column PGInt4 )
52 (Maybe (Column PGFloat8))
53
54 type NodeNgramNgramRead =
55 NodeNgramNgramPoly (Column PGInt4 )
56 (Column PGInt4 )
57 (Column PGInt4 )
58 (Column PGFloat8)
59
60 type NodeNgramNgram =
61 NodeNgramNgramPoly Int
62 Int
63 Int
64 (Maybe Double)
65
66 $(makeAdaptorAndInstance "pNodeNgramNgram"
67 ''NodeNgramNgramPoly)
68 $(makeLensesWith abbreviatedFields
69 ''NodeNgramNgramPoly)
70
71
72 nodeNgramNgramTable :: Table NodeNgramNgramWrite NodeNgramNgramRead
73 nodeNgramNgramTable =
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"
80 }
81 )
82
83 queryNodeNgramNgramTable :: Query NodeNgramNgramRead
84 queryNodeNgramNgramTable = queryTable nodeNgramNgramTable
85
86 -- | Select NodeNgramNgram
87 -- TODO not optimized (get all ngrams without filters)
88 nodeNgramNgram :: PGS.Connection -> IO [NodeNgramNgram]
89 nodeNgramNgram conn = runQuery conn queryNodeNgramNgramTable
90
91 instance QueryRunnerColumnDefault PGInt4 (Maybe Int) where
92 queryRunnerColumnDefault = fieldQueryRunnerColumn
93
94 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
95 queryRunnerColumnDefault = fieldQueryRunnerColumn
96
97
98 insertNodeNgramNgram :: [NodeNgramNgram] -> Cmd Int
99 insertNodeNgramNgram = insertNodeNgramNgramW
100 . map (\(NodeNgramNgram n ng1 ng2 maybeWeight) ->
101 NodeNgramNgram (pgInt4 n)
102 (pgInt4 ng1)
103 (pgInt4 ng2)
104 (pgDouble <$> maybeWeight)
105 )
106
107
108 insertNodeNgramNgramW :: [NodeNgramNgramWrite] -> Cmd Int
109 insertNodeNgramNgramW ns =
110 mkCmd $ \c -> fromIntegral
111 <$> runInsertMany c nodeNgramNgramTable ns
112
113