]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Flow/List.hs
[DB|WIP] fixing imports still.
[gargantext.git] / src / Gargantext / Database / Action / Flow / List.hs
1 {-|
2 Module : Gargantext.Database.Flow.List
3 Description : List Flow
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE RankNTypes #-}
16 {-# LANGUAGE ConstrainedClassMethods #-}
17 {-# LANGUAGE ConstraintKinds #-}
18 {-# LANGUAGE DeriveGeneric #-}
19 {-# LANGUAGE FlexibleContexts #-}
20 {-# LANGUAGE InstanceSigs #-}
21 {-# LANGUAGE NoImplicitPrelude #-}
22 {-# LANGUAGE OverloadedStrings #-}
23
24 module Gargantext.Database.Action.Flow.List
25 where
26
27 import Control.Monad (mapM_)
28 import Data.Map (Map, toList)
29 import Data.Maybe (Maybe(..), catMaybes)
30 import Data.Text (Text)
31 import Gargantext.API.Ngrams (NgramsElement(..), putListNgrams)
32 import Gargantext.Core.Types.Main (ListType(CandidateTerm))
33 import Gargantext.Database.Action.Flow.Types
34 import Gargantext.Database.Admin.Types.Node -- (HyperdataDocument(..), NodeType(..), NodeId, UserId, ListId, CorpusId, RootId, MasterCorpusId, MasterUserId)
35 import Gargantext.Database.Schema.Ngrams -- (insertNgrams, Ngrams(..), NgramsIndexed(..), indexNgrams, NgramsType(..), text2ngrams, ngramsTypeId)
36 import Gargantext.Database.Schema.NodeNgrams (NodeNgramsPoly(..), NodeNgramsW, listInsertDb, getCgramsId)
37 import Gargantext.Database.Schema.Node_NodeNgramsNodeNgrams -- (insert_Node_NodeNgrams_NodeNgrams, Node_NodeNgrams_NodeNgrams(..))
38 import Gargantext.Prelude
39 import qualified Data.List as List
40 import qualified Data.Map as Map
41
42
43 -- FLOW LIST
44 -- | TODO check optimization
45 mapNodeIdNgrams :: [DocumentIdWithNgrams a]
46 -> Map Ngrams (Map NgramsType (Map NodeId Int))
47 mapNodeIdNgrams = Map.unionsWith (Map.unionWith (Map.unionWith (+))) . fmap f
48 where
49 f :: DocumentIdWithNgrams a
50 -> Map Ngrams (Map NgramsType (Map NodeId Int))
51 f d = fmap (fmap (Map.singleton nId)) $ document_ngrams d
52 where
53 nId = documentId $ documentWithId d
54
55 ------------------------------------------------------------------------
56 flowList_DbRepo :: FlowCmdM env err m
57 => ListId
58 -> Map NgramsType [NgramsElement]
59 -> m ListId
60 flowList_DbRepo lId ngs = do
61 -- printDebug "listId flowList" lId
62 mapCgramsId <- listInsertDb lId toNodeNgramsW (Map.toList ngs)
63 let toInsert = catMaybes [ (,) <$> (getCgramsId mapCgramsId ntype <$> parent)
64 <*> getCgramsId mapCgramsId ntype ngram
65 | (ntype, ngs') <- Map.toList ngs
66 , NgramsElement ngram _ _ _ _ parent _ <- ngs'
67 ]
68 -- Inserting groups of ngrams
69 _r <- insert_Node_NodeNgrams_NodeNgrams
70 $ map (\(a,b) -> Node_NodeNgrams_NodeNgrams lId a b Nothing) toInsert
71 listInsert lId ngs
72 --trace (show $ List.filter (\n -> _ne_ngrams n == "versatile") $ List.concat $ Map.elems ngs) $ listInsert lId ngs
73 pure lId
74 ------------------------------------------------------------------------
75 ------------------------------------------------------------------------
76
77 toNodeNgramsW :: ListId
78 -> [(NgramsType, [NgramsElement])]
79 -> [NodeNgramsW]
80 toNodeNgramsW l ngs = List.concat $ map (toNodeNgramsW'' l) ngs
81 where
82 toNodeNgramsW'' :: ListId
83 -> (NgramsType, [NgramsElement])
84 -> [NodeNgramsW]
85 toNodeNgramsW'' l' (ngrams_type, elms) =
86 [ NodeNgrams Nothing l' list_type ngrams_terms' ngrams_type Nothing Nothing Nothing 0 |
87 (NgramsElement ngrams_terms' _size list_type _occ _root _parent _children) <- elms
88 ]
89
90 toNodeNgramsW' :: ListId
91 -> [(Text, [NgramsType])]
92 -> [NodeNgramsW]
93 toNodeNgramsW' l'' ngs = [ NodeNgrams Nothing l'' CandidateTerm terms ngrams_type Nothing Nothing Nothing 0
94 | (terms, ngrams_types) <- ngs
95 , ngrams_type <- ngrams_types
96 ]
97
98
99 listInsert :: FlowCmdM env err m
100 => ListId
101 -> Map NgramsType [NgramsElement]
102 -> m ()
103 listInsert lId ngs = mapM_ (\(typeList, ngElmts)
104 -> putListNgrams lId typeList ngElmts
105 ) $ toList ngs
106
107 ------------------------------------------------------------------------
108 ------------------------------------------------------------------------