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
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE ConstrainedClassMethods #-}
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE InstanceSigs #-}
19 module Gargantext.Database.Action.Flow.List
22 import Control.Monad (mapM_)
23 import Data.Map (Map, toList)
24 import Data.Maybe (Maybe(..), catMaybes)
25 import Data.Text (Text)
26 import Gargantext.API.Ngrams (NgramsElement(..), putListNgrams)
27 import Gargantext.Core.Flow.Types
28 import Gargantext.Core.Types.Main (ListType(CandidateTerm))
29 import Gargantext.Database.Admin.Types.Node -- (HyperdataDocument(..), NodeType(..), NodeId, UserId, ListId, CorpusId, RootId, MasterCorpusId, MasterUserId)
30 import Gargantext.Database.Query.Table.NodeNgrams (NodeNgramsPoly(..), NodeNgramsW, listInsertDb, getCgramsId)
31 import Gargantext.Database.Schema.Ngrams -- (insertNgrams, Ngrams(..), NgramsIndexed(..), indexNgrams, NgramsType(..), text2ngrams, ngramsTypeId)
32 import Gargantext.Database.Action.Flow.Types
33 import Gargantext.Database.Query.Table.Node_NodeNgramsNodeNgrams
34 import Gargantext.Prelude
35 import qualified Data.List as List
36 import qualified Data.Map as Map
39 -- 1. select specific terms of the corpus when compared with others langs
40 -- (for now, suppose english)
41 -- 2. select specific terms of the corpus when compared with others corpora (same database)
42 -- 3. select clusters of terms (generic and specific)
45 data FlowList = FlowListLang
50 flowList_Tficf :: UserCorpusId
54 -> Cmd err (Map Text (Double, Set Text))
55 flowList_Tficf u m nt f = do
57 u' <- Map.filter (\s -> Set.size s > 1) <$> getNodesByNgramsUser u nt
58 m' <- Map.filter (\s -> Set.size s > 1) <$> getNodesByNgramsMaster u m
61 $ toTficfData (countNodesByNgramsWith f u')
62 (countNodesByNgramsWith f m')
64 flowList_Tficf' :: UserCorpusId
67 -> Cmd err (Map Text (Double, Set Text))
68 flowList_Tficf' u m nt f = do
70 u' <- Map.filter (\s -> Set.size s > 1) <$> getNodesByNgramsUser u nt
71 m' <- Map.filter (\s -> Set.size s > 1) <$> getNodesByNgramsMaster u m
74 $ toTficfData (countNodesByNgramsWith f u')
75 (countNodesByNgramsWith f m')
84 -- | TODO check optimization
85 mapNodeIdNgrams :: [DocumentIdWithNgrams a]
86 -> Map Ngrams (Map NgramsType (Map NodeId Int))
87 mapNodeIdNgrams = Map.unionsWith (Map.unionWith (Map.unionWith (+))) . fmap f
89 f :: DocumentIdWithNgrams a
90 -> Map Ngrams (Map NgramsType (Map NodeId Int))
91 f d = fmap (fmap (Map.singleton nId)) $ document_ngrams d
93 nId = documentId $ documentWithId d
95 ------------------------------------------------------------------------
96 flowList_DbRepo :: FlowCmdM env err m
98 -> Map NgramsType [NgramsElement]
100 flowList_DbRepo lId ngs = do
101 -- printDebug "listId flowList" lId
102 mapCgramsId <- listInsertDb lId toNodeNgramsW (Map.toList ngs)
103 let toInsert = catMaybes [ (,) <$> (getCgramsId mapCgramsId ntype <$> parent)
104 <*> getCgramsId mapCgramsId ntype ngram
105 | (ntype, ngs') <- Map.toList ngs
106 , NgramsElement ngram _ _ _ _ parent _ <- ngs'
108 -- Inserting groups of ngrams
109 _r <- insert_Node_NodeNgrams_NodeNgrams
110 $ map (\(a,b) -> Node_NodeNgrams_NodeNgrams lId a b Nothing) toInsert
114 --trace (show $ List.filter (\n -> _ne_ngrams n == "versatile") $ List.concat $ Map.elems ngs) $ listInsert lId ngs
116 ------------------------------------------------------------------------
117 ------------------------------------------------------------------------
119 toNodeNgramsW :: ListId
120 -> [(NgramsType, [NgramsElement])]
122 toNodeNgramsW l ngs = List.concat $ map (toNodeNgramsW'' l) ngs
124 toNodeNgramsW'' :: ListId
125 -> (NgramsType, [NgramsElement])
127 toNodeNgramsW'' l' (ngrams_type, elms) =
128 [ NodeNgrams Nothing l' list_type ngrams_terms' ngrams_type Nothing Nothing Nothing 0 |
129 (NgramsElement ngrams_terms' _size list_type _occ _root _parent _children) <- elms
133 toNodeNgramsW' :: ListId
134 -> [(Text, [NgramsType])]
136 toNodeNgramsW' l'' ngs = [ NodeNgrams Nothing l'' CandidateTerm terms ngrams_type Nothing Nothing Nothing 0
137 | (terms, ngrams_types) <- ngs
138 , ngrams_type <- ngrams_types
142 listInsert :: FlowCmdM env err m
144 -> Map NgramsType [NgramsElement]
146 listInsert lId ngs = mapM_ (\(typeList, ngElmts)
147 -> putListNgrams lId typeList ngElmts) (toList ngs)
149 ------------------------------------------------------------------------
150 ------------------------------------------------------------------------