2 Module : Gargantext.Database.Flow
3 Description : Database Flow
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE FlexibleContexts #-}
13 {-# LANGUAGE QuasiQuotes #-}
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE RankNTypes #-}
17 -- {-# LANGUAGE Arrows #-}
19 module Gargantext.Database.Action.Flow.Pairing
23 import Control.Lens (_Just, (^.))
24 import Data.Map (Map, fromList)
25 import Data.Maybe (catMaybes)
26 import Data.Text (Text, toLower)
27 import Database.PostgreSQL.Simple.SqlQQ (sql)
28 import Gargantext.Core.Types (TableResult(..))
29 import Gargantext.Database.Action.Flow.Utils
30 import Gargantext.Database.Admin.Types.Node (AnnuaireId, CorpusId, ListId{-, DocId, ContactId-})
31 import Gargantext.Database.Prelude (Cmd, runPGSQuery)
32 import Gargantext.Database.Query.Table.Node.Children (getAllContacts)
33 import Gargantext.Database.Query.Table.Node.Contact -- (HyperdataContact(..))
34 import Gargantext.Database.Schema.Ngrams -- (NgramsType(..))
35 import Gargantext.Prelude hiding (sum)
37 import qualified Data.Map as DM
38 import qualified Data.Text as DT
40 -- TODO mv this type in Types Main
44 pairing'' :: (CorpusId, CorpusId) -> (DocId -> DocId)
47 pairing' :: (CorpusId, AnnuaireId) -> (DocId -> ContactId)
51 -- | TODO : add paring policy as parameter
52 pairing :: CorpusId -- (CorpusId, ListId) -- Pair (Either CorpusId AnnuaireId) ListId
53 -> AnnuaireId -- (AnnuaireId, ListId) -- Pair (Either CorpusId AnnuaireId) ListId
56 pairing cId aId lId = do
57 contacts' <- getAllContacts aId
58 let contactsMap = pairingPolicyToMap toLower
59 $ toMaps extractNgramsT (tr_docs contacts')
61 ngramsMap' <- getNgramsTindexed cId Authors
62 let ngramsMap = pairingPolicyToMap lastName ngramsMap'
64 let indexedNgrams = pairMaps contactsMap ngramsMap
66 insertDocNgrams lId indexedNgrams
68 lastName :: Terms -> Terms
69 lastName texte = DT.toLower
70 $ maybe texte (\x -> if DT.length x > 3 then x else texte) (lastName' texte)
72 lastName' = lastMay . DT.splitOn " "
74 -- TODO: this method is dangerous (maybe equalities of the result are
75 -- not taken into account emergency demo plan...)
76 pairingPolicyToMap :: (Terms -> Terms)
77 -> Map (NgramsT Ngrams) a
78 -> Map (NgramsT Ngrams) a
79 pairingPolicyToMap f = DM.mapKeys (pairingPolicy f)
81 pairingPolicy :: (Terms -> Terms)
84 pairingPolicy f (NgramsT nt (Ngrams ng _)) = (NgramsT nt (Ngrams (f ng) 1))
86 -- | TODO : use Occurrences in place of Int
87 extractNgramsT :: HyperdataContact
88 -> Map (NgramsT Ngrams) Int
89 extractNgramsT contact = fromList [(NgramsT Authors a' , 1)| a' <- authors ]
91 authors = map text2ngrams
92 $ catMaybes [ contact^.(hc_who . _Just . cw_lastName) ]
95 pairMaps :: Map (NgramsT Ngrams) a
96 -> Map (NgramsT Ngrams) NgramsId
97 -> Map NgramsIndexed (Map NgramsType a)
100 [ (NgramsIndexed ng nId, DM.singleton nt n2i)
101 | (k@(NgramsT nt ng),n2i) <- DM.toList m1
102 , Just nId <- [DM.lookup k m2]
105 -----------------------------------------------------------------------
106 getNgramsTindexed :: CorpusId
108 -> Cmd err (Map (NgramsT Ngrams) NgramsId)
109 getNgramsTindexed corpusId ngramsType' = fromList
110 <$> map (\(ngramsId',t,n) -> (NgramsT ngramsType' (Ngrams t n),ngramsId'))
111 <$> selectNgramsTindexed corpusId ngramsType'
113 selectNgramsTindexed :: CorpusId
115 -> Cmd err [(NgramsId, Terms, Int)]
116 selectNgramsTindexed corpusId' ngramsType'' = runPGSQuery selectQuery (corpusId', ngramsTypeId ngramsType'')
118 selectQuery = [sql| SELECT n.id,n.terms,n.n from ngrams n
119 JOIN node_node_ngrams occ ON occ.ngrams_id = n.id
120 -- JOIN node_node_ngrams2 occ ON occ.ngrams_id = n.id
121 JOIN nodes_nodes nn ON nn.node2_id = occ.node2_id
123 WHERE nn.node1_id = ?
124 AND occ.ngrams_type = ?
125 AND occ.node2_id = nn.node2_id
129 {- | TODO more typed SQL queries
130 selectNgramsTindexed :: CorpusId -> NgramsType -> Query NgramsRead
131 selectNgramsTindexed corpusId ngramsType = proc () -> do
132 nodeNode <- queryNodeNodeTable -< ()
133 nodeNgrams <- queryNodesNgramsTable -< ()
134 ngrams <- queryNgramsTable -< ()
136 restrict -< node1_id nodeNode .== pgInt4 corpusId
137 restrict -< node2_id nodeNode .== node_id nodeNgrams
138 restrict -< ngrams_id ngrams .== node_ngrams nodeNgrams
140 result <- aggregate groupBy (ngrams_id ngrams)