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;
---------------------------------------------------------------
CREATE TABLE public.context_node_ngrams (
context_id INTEGER NOT NULL REFERENCES public.nodes (id) ON DELETE CASCADE,
node_id INTEGER NOT NULL REFERENCES public.contexts (id) ON DELETE CASCADE,
ngrams_id INTEGER NOT NULL REFERENCES public.ngrams (id) ON DELETE CASCADE,
context_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_type INTEGER ,
weight double precision,
PRIMARY KEY (context_id, node_id, ngrams_id, ngrams_type)
......
......@@ -321,9 +321,6 @@ saveDocNgramsWith lId mapNgramsDocs' = do
terms2id <- insertExtractedNgrams $ HashMap.keys mapNgramsDocs'
let mapNgramsDocs = HashMap.mapKeys extracted2ngrams mapNgramsDocs'
-- to be removed
let indexedNgrams = HashMap.mapKeys (indexNgrams terms2id) mapNgramsDocs
-- new
mapCgramsId <- listInsertDb lId toNodeNgramsW'
$ map (first _ngramsTerms . second Map.keys)
......@@ -341,7 +338,7 @@ saveDocNgramsWith lId mapNgramsDocs' = do
]
-- to be removed
_ <- insertDocNgrams lId indexedNgrams
_ <- insertDocNgrams lId $ HashMap.mapKeys (indexNgrams terms2id) mapNgramsDocs
pure ()
......@@ -355,7 +352,7 @@ insertDocs :: ( FlowCmdM env err m
=> UserId
-> CorpusId
-> [a]
-> m ([DocId], [Indexed NodeId a])
-> m ([ContextId], [Indexed ContextId a])
insertDocs uId cId hs = do
let docs = map addUniqId hs
newIds <- insertDb uId cId docs
......
......@@ -21,6 +21,7 @@ import Gargantext.Database.Query.Table.ContextNodeNgrams
import Gargantext.Database.Schema.Ngrams
import Gargantext.Database.Types
import Gargantext.Prelude
import Control.Lens ((^.))
import qualified Data.Map as DM
import qualified Data.HashMap.Strict as HashMap
......@@ -31,35 +32,21 @@ data DocumentIdWithNgrams a b =
, documentNgrams :: HashMap b (Map NgramsType Int)
} deriving (Show)
docNgrams2contextNodeNgrams :: ListId
-> DocNgrams
-> ContextNodeNgrams
docNgrams2contextNodeNgrams lId (DocNgrams d n nt w) =
ContextNodeNgrams d lId n nt w
insertDocNgrams :: ListId
-> HashMap (Indexed NgramsId Ngrams) (Map NgramsType (Map DocId Int))
-> Cmd err Int
insertDocNgrams lId m = insertContextNodeNgrams ns
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)
CREATE OR REPLACE FUNCTION set_update_ngrams_add() RETURNS trigger AS $$
BEGIN
UPDATE node_node_ngrams nnn0 SET weight = weight + d.fix_count
FROM ( SELECT lists.parent_id as node1_id
, lists.id as node2_id
, nnn.ngrams_id as ngrams_id
, nnn.ngrams_type as ngrams_type
, count(*) as fix_count
FROM ( SELECT lists.parent_id AS node1_id
, lists.id AS node2_id
, cnn.ngrams_id AS ngrams_id
, cnn.ngrams_type AS ngrams_type
, count(*) AS fix_count
FROM NEW as new1
INNER JOIN contexts doc ON doc.id = new1.context_id
INNER JOIN nodes lists ON lists.id = new1.node_id
INNER JOIN context_node_ngrams nnn ON nnn.context_id = doc.id
WHERE nnn.node_id in (?, lists.id) -- (masterList_id, userLists)
INNER JOIN contexts doc ON doc.id = new1.context_id
INNER JOIN nodes lists ON new1.node_id = lists.parent_id
INNER JOIN context_node_ngrams cnn ON cnn.context_id = doc.id
WHERE lists.id in (?, lists.id) -- (masterList_id, userLists)
AND lists.typename = ?
GROUP BY node1_id, node2_id, ngrams_id, ngrams_type
) as d
......
......@@ -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)
where
fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
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)
where
fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
......@@ -62,19 +62,19 @@ queryAdd = [sql|
;
|]
prepare :: ParentId -> [NodeId] -> [InputData]
prepare pId ns = map (\nId -> InputData pId nId) ns
prepare :: ParentId -> [ContextId] -> [InputData]
prepare pId ns = map (\cId -> InputData pId cId) ns
------------------------------------------------------------------------
-- * Main Types used
data InputData = InputData { inNode1_id :: NodeId
, inNode2_id :: NodeId
data InputData = InputData { inNode_id :: NodeId
, inContext_id :: ContextId
} deriving (Show, Generic, Typeable)
instance ToRow InputData where
toRow inputData = [ toField (inNode1_id inputData)
, toField (inNode2_id inputData)
toRow inputData = [ toField (inNode_id inputData)
, toField (inContext_id inputData)
, 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