Commit 4e3342db authored by Alexandre Delanoë's avatar Alexandre Delanoë

[FIX] Counts in NgramsTable and context_node_ngrams insertion

parent 25b81234
...@@ -147,9 +147,9 @@ ALTER TABLE public.nodes_contexts OWNER TO gargantua; ...@@ -147,9 +147,9 @@ ALTER TABLE public.nodes_contexts OWNER TO gargantua;
--------------------------------------------------------------- ---------------------------------------------------------------
CREATE TABLE public.context_node_ngrams ( CREATE TABLE public.context_node_ngrams (
context_id INTEGER NOT NULL REFERENCES public.nodes (id) ON DELETE CASCADE, context_id INTEGER NOT NULL REFERENCES public.contexts (id) ON DELETE CASCADE,
node_id INTEGER NOT NULL REFERENCES public.contexts (id) ON DELETE CASCADE, node_id INTEGER NOT NULL REFERENCES public.nodes (id) ON DELETE CASCADE,
ngrams_id INTEGER NOT NULL REFERENCES public.ngrams (id) ON DELETE CASCADE, ngrams_id INTEGER NOT NULL REFERENCES public.ngrams (id) ON DELETE CASCADE,
ngrams_type INTEGER , ngrams_type INTEGER ,
weight double precision, weight double precision,
PRIMARY KEY (context_id, node_id, ngrams_id, ngrams_type) PRIMARY KEY (context_id, node_id, ngrams_id, ngrams_type)
......
...@@ -321,9 +321,6 @@ saveDocNgramsWith lId mapNgramsDocs' = do ...@@ -321,9 +321,6 @@ saveDocNgramsWith lId mapNgramsDocs' = do
terms2id <- insertExtractedNgrams $ HashMap.keys mapNgramsDocs' terms2id <- insertExtractedNgrams $ HashMap.keys mapNgramsDocs'
let mapNgramsDocs = HashMap.mapKeys extracted2ngrams mapNgramsDocs' let mapNgramsDocs = HashMap.mapKeys extracted2ngrams mapNgramsDocs'
-- to be removed
let indexedNgrams = HashMap.mapKeys (indexNgrams terms2id) mapNgramsDocs
-- new -- new
mapCgramsId <- listInsertDb lId toNodeNgramsW' mapCgramsId <- listInsertDb lId toNodeNgramsW'
$ map (first _ngramsTerms . second Map.keys) $ map (first _ngramsTerms . second Map.keys)
...@@ -341,7 +338,7 @@ saveDocNgramsWith lId mapNgramsDocs' = do ...@@ -341,7 +338,7 @@ saveDocNgramsWith lId mapNgramsDocs' = do
] ]
-- to be removed -- to be removed
_ <- insertDocNgrams lId indexedNgrams _ <- insertDocNgrams lId $ HashMap.mapKeys (indexNgrams terms2id) mapNgramsDocs
pure () pure ()
...@@ -355,7 +352,7 @@ insertDocs :: ( FlowCmdM env err m ...@@ -355,7 +352,7 @@ insertDocs :: ( FlowCmdM env err m
=> UserId => UserId
-> CorpusId -> CorpusId
-> [a] -> [a]
-> m ([DocId], [Indexed NodeId a]) -> m ([ContextId], [Indexed ContextId a])
insertDocs uId cId hs = do insertDocs uId cId hs = do
let docs = map addUniqId hs let docs = map addUniqId hs
newIds <- insertDb uId cId docs newIds <- insertDb uId cId docs
......
...@@ -21,6 +21,7 @@ import Gargantext.Database.Query.Table.ContextNodeNgrams ...@@ -21,6 +21,7 @@ import Gargantext.Database.Query.Table.ContextNodeNgrams
import Gargantext.Database.Schema.Ngrams import Gargantext.Database.Schema.Ngrams
import Gargantext.Database.Types import Gargantext.Database.Types
import Gargantext.Prelude import Gargantext.Prelude
import Control.Lens ((^.))
import qualified Data.Map as DM import qualified Data.Map as DM
import qualified Data.HashMap.Strict as HashMap import qualified Data.HashMap.Strict as HashMap
...@@ -31,35 +32,21 @@ data DocumentIdWithNgrams a b = ...@@ -31,35 +32,21 @@ data DocumentIdWithNgrams a b =
, documentNgrams :: HashMap b (Map NgramsType Int) , documentNgrams :: HashMap b (Map NgramsType Int)
} deriving (Show) } deriving (Show)
docNgrams2contextNodeNgrams :: ListId insertDocNgrams :: ListId
-> DocNgrams -> HashMap (Indexed NgramsId Ngrams) (Map NgramsType (Map DocId Int))
-> ContextNodeNgrams -> Cmd err Int
docNgrams2contextNodeNgrams lId (DocNgrams d n nt w) = insertDocNgrams lId m = insertContextNodeNgrams ns
ContextNodeNgrams d lId n nt w where
ns = [ ContextNodeNgrams docId lId (ng^.index)
(ngramsTypeId t)
(fromIntegral i)
| (ng, t2n2i) <- HashMap.toList m
, (t, n2i) <- DM.toList t2n2i
, (docId, i) <- DM.toList n2i
]
data DocNgrams = DocNgrams { dn_doc_id :: DocId
, dn_ngrams_id :: Int
, dn_ngrams_type :: NgramsTypeId
, dn_weight :: Double
}
insertDocNgramsOn :: ListId
-> [DocNgrams]
-> Cmd err Int
insertDocNgramsOn cId dn =
insertContextNodeNgrams
$ (map (docNgrams2contextNodeNgrams cId) dn)
insertDocNgrams :: ListId
-> HashMap (Indexed Int Ngrams) (Map NgramsType (Map DocId Int))
-> Cmd err Int
insertDocNgrams cId m =
insertDocNgramsOn cId [ DocNgrams { dn_doc_id = n
, dn_ngrams_id = _index ng
, dn_ngrams_type = ngramsTypeId t
, dn_weight = fromIntegral i }
| (ng, t2n2i) <- HashMap.toList m
, (t, n2i) <- DM.toList t2n2i
, (n, i) <- DM.toList n2i
]
...@@ -66,16 +66,16 @@ triggerUpdateAdd lId = execPGSQuery query (lId, nodeTypeId NodeList) ...@@ -66,16 +66,16 @@ triggerUpdateAdd lId = execPGSQuery query (lId, nodeTypeId NodeList)
CREATE OR REPLACE FUNCTION set_update_ngrams_add() RETURNS trigger AS $$ CREATE OR REPLACE FUNCTION set_update_ngrams_add() RETURNS trigger AS $$
BEGIN BEGIN
UPDATE node_node_ngrams nnn0 SET weight = weight + d.fix_count UPDATE node_node_ngrams nnn0 SET weight = weight + d.fix_count
FROM ( SELECT lists.parent_id as node1_id FROM ( SELECT lists.parent_id AS node1_id
, lists.id as node2_id , lists.id AS node2_id
, nnn.ngrams_id as ngrams_id , cnn.ngrams_id AS ngrams_id
, nnn.ngrams_type as ngrams_type , cnn.ngrams_type AS ngrams_type
, count(*) as fix_count , count(*) AS fix_count
FROM NEW as new1 FROM NEW as new1
INNER JOIN contexts doc ON doc.id = new1.context_id INNER JOIN contexts doc ON doc.id = new1.context_id
INNER JOIN nodes lists ON lists.id = new1.node_id INNER JOIN nodes lists ON new1.node_id = lists.parent_id
INNER JOIN context_node_ngrams nnn ON nnn.context_id = doc.id INNER JOIN context_node_ngrams cnn ON cnn.context_id = doc.id
WHERE nnn.node_id in (?, lists.id) -- (masterList_id, userLists) WHERE lists.id in (?, lists.id) -- (masterList_id, userLists)
AND lists.typename = ? AND lists.typename = ?
GROUP BY node1_id, node2_id, ngrams_id, ngrams_type GROUP BY node1_id, node2_id, ngrams_id, ngrams_type
) as d ) as d
......
...@@ -33,13 +33,13 @@ import Gargantext.Prelude ...@@ -33,13 +33,13 @@ import Gargantext.Prelude
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
add :: ParentId -> [NodeId] -> Cmd err [Only Int] add :: CorpusId -> [ContextId] -> Cmd err [Only Int]
add pId ns = runPGSQuery queryAdd (Only $ Values fields inputData) add pId ns = runPGSQuery queryAdd (Only $ Values fields inputData)
where where
fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
inputData = prepare pId ns inputData = prepare pId ns
add_debug :: ParentId -> [NodeId] -> Cmd err ByteString add_debug :: CorpusId -> [ContextId] -> Cmd err ByteString
add_debug pId ns = formatPGSQuery queryAdd (Only $ Values fields inputData) add_debug pId ns = formatPGSQuery queryAdd (Only $ Values fields inputData)
where where
fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
...@@ -62,19 +62,19 @@ queryAdd = [sql| ...@@ -62,19 +62,19 @@ queryAdd = [sql|
; ;
|] |]
prepare :: ParentId -> [NodeId] -> [InputData] prepare :: ParentId -> [ContextId] -> [InputData]
prepare pId ns = map (\nId -> InputData pId nId) ns prepare pId ns = map (\cId -> InputData pId cId) ns
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- * Main Types used -- * Main Types used
data InputData = InputData { inNode1_id :: NodeId data InputData = InputData { inNode_id :: NodeId
, inNode2_id :: NodeId , inContext_id :: ContextId
} deriving (Show, Generic, Typeable) } deriving (Show, Generic, Typeable)
instance ToRow InputData where instance ToRow InputData where
toRow inputData = [ toField (inNode1_id inputData) toRow inputData = [ toField (inNode_id inputData)
, toField (inNode2_id inputData) , toField (inContext_id inputData)
, toField (1 :: Int) , toField (1 :: Int)
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment