2 Module : Gargantext.Database.TextSearch
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE NoImplicitPrelude #-}
15 --{-# LANGUAGE OverloadedStrings #-}
17 {-# LANGUAGE QuasiQuotes #-}
19 module Gargantext.Database.Cooc where
21 import Control.Monad ((>>=))
22 import Database.PostgreSQL.Simple
23 import Database.PostgreSQL.Simple.SqlQQ
25 import Gargantext.Prelude
26 import Gargantext (connectGargandb)
30 type GroupListId = Int
32 coocTest :: IO [(Int, Int, Int)]
33 coocTest = connectGargandb "gargantext.ini"
34 >>= \conn -> dBcooc conn 421968 446602 446599
36 dBcooc :: Connection -> CorpusId -> MainListId -> GroupListId -> IO [(Int, Int, Int)]
37 dBcooc conn corpus mainList groupList = query conn [sql|
43 COALESCE(grA.ngram1_id, wlA.ngram_id) as ngA,
44 COALESCE(grB.ngram1_id, wlB.ngram_id) as ngB,
49 -- SQL graph for getting the cooccurrences
51 -- STEP 1: X axis of the matrix
52 INNER JOIN nodes_ngrams
53 AS ngA ON ngA.node_id = n.id
54 -- \--> get the occurrences node/ngram of the corpus
56 INNER JOIN nodes_ngrams
57 AS wlA ON ngA.ngram_id = wlA.ngram_id
59 -- \--> filter with white/main list (typename 7)
61 LEFT JOIN nodes_ngrams_ngrams
62 AS grA ON wlA.ngram_id = grA.ngram1_id
64 -- \--> adding (joining) ngrams that are grouped (typename 6)
65 LEFT JOIN nodes_ngrams
66 AS wlAA ON grA.ngram2_id = wlAA.ngram_id
67 AND wlAA.node_id = wlA.node_id
68 -- \--> adding (joining) ngrams that are not grouped
69 --LEFT JOIN ngrams AS wlAA ON grA.ngram2_id = wlAA.id
70 -- \--> for joining all synonyms even if they are not in the main list (white list)
73 -- STEP 2: Y axi of the matrix
74 INNER JOIN nodes_ngrams
75 AS ngB ON ngB.node_id = n.id
76 -- \--> get the occurrences node/ngram of the corpus
78 INNER JOIN nodes_ngrams
79 AS wlB ON ngB.ngram_id = wlB.ngram_id
81 -- \--> filter with white/main list
83 LEFT JOIN nodes_ngrams_ngrams
84 AS grB ON wlB.ngram_id = grB.ngram1_id
86 -- \--> adding (joining) ngrams that are grouped
87 LEFT JOIN nodes_ngrams
88 AS wlBB ON grB.ngram2_id = wlBB.ngram_id
89 AND wlBB.node_id = wlB.node_id
90 -- \--> adding (joining) ngrams that are not grouped
92 -- LEFT JOIN ngrams AS wlBB ON grB.ngram2_id = wlBB.id
93 -- \--> for joining all synonyms even if they are not in the main list (white list)
103 SELECT ngA, ngB, score
104 FROM COOC --> from the query above
109 |] (mainList, groupList, mainList, groupList, corpus)