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 QuasiQuotes #-}
13 -- {-# LANGUAGE Arrows #-}
15 module Gargantext.Database.Action.Flow.Pairing
19 import Control.Lens (_Just, (^.))
20 import Data.Map (Map, fromList)
21 import Data.Maybe (catMaybes)
22 import Data.Text (Text, toLower)
23 import Database.PostgreSQL.Simple.SqlQQ (sql)
24 import Gargantext.Core.Types (TableResult(..))
25 import Gargantext.Database.Action.Flow.Utils
26 import Gargantext.Database.Admin.Types.Node (AnnuaireId, CorpusId, ListId{-, DocId, ContactId-})
27 import Gargantext.Database.Prelude (Cmd, runPGSQuery)
28 import Gargantext.Database.Query.Table.Node.Children (getAllContacts)
29 import Gargantext.Database.Query.Table.Node.Contact -- (HyperdataContact(..))
30 import Gargantext.Database.Schema.Ngrams -- (NgramsType(..))
31 import Gargantext.Prelude hiding (sum)
33 import qualified Data.Map as DM
34 import qualified Data.Text as DT
36 -- TODO mv this type in Types Main
40 pairing'' :: (CorpusId, CorpusId) -> (DocId -> DocId)
43 pairing' :: (CorpusId, AnnuaireId) -> (DocId -> ContactId)
47 -- | TODO : add paring policy as parameter
48 pairing :: CorpusId -- (CorpusId, ListId) -- Pair (Either CorpusId AnnuaireId) ListId
49 -> AnnuaireId -- (AnnuaireId, ListId) -- Pair (Either CorpusId AnnuaireId) ListId
52 pairing cId aId lId = do
53 contacts' <- getAllContacts aId
54 let contactsMap = pairingPolicyToMap toLower
55 $ toMaps extractNgramsT (tr_docs contacts')
57 ngramsMap' <- getNgramsTindexed cId Authors
58 let ngramsMap = pairingPolicyToMap lastName ngramsMap'
60 let indexedNgrams = pairMaps contactsMap ngramsMap
62 insertDocNgrams lId indexedNgrams
64 lastName :: Terms -> Terms
65 lastName texte = DT.toLower
66 $ maybe texte (\x -> if DT.length x > 3 then x else texte) (lastName' texte)
68 lastName' = lastMay . DT.splitOn " "
70 -- TODO: this method is dangerous (maybe equalities of the result are
71 -- not taken into account emergency demo plan...)
72 pairingPolicyToMap :: (Terms -> Terms)
73 -> Map (NgramsT Ngrams) a
74 -> Map (NgramsT Ngrams) a
75 pairingPolicyToMap f = DM.mapKeys (pairingPolicy f)
77 pairingPolicy :: (Terms -> Terms)
80 pairingPolicy f (NgramsT nt (Ngrams ng _)) = (NgramsT nt (Ngrams (f ng) 1))
82 -- | TODO : use Occurrences in place of Int
83 extractNgramsT :: HyperdataContact
84 -> Map (NgramsT Ngrams) Int
85 extractNgramsT contact = fromList [(NgramsT Authors a' , 1)| a' <- authors ]
87 authors = map text2ngrams
88 $ catMaybes [ contact^.(hc_who . _Just . cw_lastName) ]
91 pairMaps :: Map (NgramsT Ngrams) a
92 -> Map (NgramsT Ngrams) NgramsId
93 -> Map NgramsIndexed (Map NgramsType a)
96 [ (NgramsIndexed ng nId, DM.singleton nt n2i)
97 | (k@(NgramsT nt ng),n2i) <- DM.toList m1
98 , Just nId <- [DM.lookup k m2]
101 -----------------------------------------------------------------------
102 getNgramsTindexed :: CorpusId
104 -> Cmd err (Map (NgramsT Ngrams) NgramsId)
105 getNgramsTindexed corpusId ngramsType' = fromList
106 <$> map (\(ngramsId',t,n) -> (NgramsT ngramsType' (Ngrams t n),ngramsId'))
107 <$> selectNgramsTindexed corpusId ngramsType'
109 selectNgramsTindexed :: CorpusId
111 -> Cmd err [(NgramsId, Terms, Int)]
112 selectNgramsTindexed corpusId' ngramsType'' = runPGSQuery selectQuery (corpusId', ngramsTypeId ngramsType'')
114 selectQuery = [sql| SELECT n.id,n.terms,n.n from ngrams n
115 JOIN node_node_ngrams occ ON occ.ngrams_id = n.id
116 -- JOIN node_node_ngrams2 occ ON occ.ngrams_id = n.id
117 JOIN nodes_nodes nn ON nn.node2_id = occ.node2_id
119 WHERE nn.node1_id = ?
120 AND occ.ngrams_type = ?
121 AND occ.node2_id = nn.node2_id
125 {- | TODO more typed SQL queries
126 selectNgramsTindexed :: CorpusId -> NgramsType -> Query NgramsRead
127 selectNgramsTindexed corpusId ngramsType = proc () -> do
128 nodeNode <- queryNodeNodeTable -< ()
129 nodeNgrams <- queryNodesNgramsTable -< ()
130 ngrams <- queryNgramsTable -< ()
132 restrict -< node1_id nodeNode .== pgInt4 corpusId
133 restrict -< node2_id nodeNode .== node_id nodeNgrams
134 restrict -< ngrams_id ngrams .== node_ngrams nodeNgrams
136 result <- aggregate groupBy (ngrams_id ngrams)