2 Module : Gargantext.Database.Cooc
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE NoImplicitPrelude #-}
13 --{-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE QuasiQuotes #-}
16 {-# LANGUAGE RankNTypes #-}
18 module Gargantext.Database.Cooc where
20 import Database.PostgreSQL.Simple.SqlQQ
22 import Gargantext.Prelude
23 import Gargantext.Database.Utils (Cmd, runCmdDevNoErr, runPGSQuery)
27 type GroupListId = Int
29 coocTest :: IO [(Int, Int, Int)]
30 coocTest = runCmdDevNoErr $ dBcooc 421968 446602 446599
32 dBcooc :: CorpusId -> MainListId -> GroupListId -> Cmd err [(Int, Int, Int)]
33 dBcooc corpus mainList groupList = runPGSQuery [sql|
39 COALESCE(grA.ngram1_id, wlA.ngram_id) as ngA,
40 COALESCE(grB.ngram1_id, wlB.ngram_id) as ngB,
45 -- SQL graph for getting the cooccurrences
47 -- STEP 1: X axis of the matrix
48 INNER JOIN nodes_ngrams
49 AS ngA ON ngA.node_id = n.id
50 -- \--> get the occurrences node/ngram of the corpus
52 INNER JOIN nodes_ngrams
53 AS wlA ON ngA.ngram_id = wlA.ngram_id
55 -- \--> filter with white/main list (typename 7)
57 LEFT JOIN nodes_ngrams_ngrams
58 AS grA ON wlA.ngram_id = grA.ngram1_id
60 -- \--> adding (joining) ngrams that are grouped (typename 6)
61 LEFT JOIN nodes_ngrams
62 AS wlAA ON grA.ngram2_id = wlAA.ngram_id
63 AND wlAA.node_id = wlA.node_id
64 -- \--> adding (joining) ngrams that are not grouped
65 --LEFT JOIN ngrams AS wlAA ON grA.ngram2_id = wlAA.id
66 -- \--> for joining all synonyms even if they are not in the main list (white list)
69 -- STEP 2: Y axi of the matrix
70 INNER JOIN nodes_ngrams
71 AS ngB ON ngB.node_id = n.id
72 -- \--> get the occurrences node/ngram of the corpus
74 INNER JOIN nodes_ngrams
75 AS wlB ON ngB.ngram_id = wlB.ngram_id
77 -- \--> filter with white/main list
79 LEFT JOIN nodes_ngrams_ngrams
80 AS grB ON wlB.ngram_id = grB.ngram1_id
82 -- \--> adding (joining) ngrams that are grouped
83 LEFT JOIN nodes_ngrams
84 AS wlBB ON grB.ngram2_id = wlBB.ngram_id
85 AND wlBB.node_id = wlB.node_id
86 -- \--> adding (joining) ngrams that are not grouped
88 -- LEFT JOIN ngrams AS wlBB ON grB.ngram2_id = wlBB.id
89 -- \--> for joining all synonyms even if they are not in the main list (white list)
99 SELECT ngA, ngB, score
100 FROM COOC --> from the query above
105 |] (mainList, groupList, mainList, groupList, corpus)