2 Module : Gargantext.Database.Metrics
3 Description : Get Metrics from Storage (Database like)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE FlexibleContexts #-}
18 module Gargantext.Database.Metrics
22 import Data.Text (Text)
23 import Gargantext.API.Ngrams (TabType(..), ngramsTypeFromTabType)
24 import Gargantext.API.Ngrams.Tools (filterListWithRoot, groupNodesByNgrams, Diagonal(..), getCoocByNgrams, mapTermListRoot, RootTerm)
25 import Gargantext.Core.Types (ListType(..), Limit, NodeType(..))
26 import Gargantext.Database.Flow (FlowCmdM)
27 import Gargantext.Database.Metrics.NgramsByNode (getNodesByNgramsOnlyUser{-, getTficfWith-})
28 import Gargantext.Database.Node.Select
29 import Gargantext.Database.Schema.Node (defaultList)
30 import Gargantext.Database.Types.Node (ListId, CorpusId{-, HyperdataCorpus-})
31 --import Gargantext.Database.Flow (getOrMkRootWithCorpus)
32 import Gargantext.Database.Config (userMaster)
33 import Gargantext.Prelude
34 import Gargantext.Text.Metrics (scored, Scored(..), {-localMetrics, toScored-})
35 import qualified Data.Map as Map
36 --import qualified Data.Vector.Storable as Vec
38 getMetrics :: FlowCmdM env err m
39 => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
40 -> m (Map Text (ListType, Maybe Text), [Scored Text])
41 getMetrics cId maybeListId tabType maybeLimit = do
42 (ngs, _, myCooc) <- getNgramsCooc cId maybeListId tabType maybeLimit
43 pure (ngs, scored myCooc)
46 getNgramsCooc :: (FlowCmdM env err m)
47 => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
48 -> m ( Map Text (ListType, Maybe Text)
49 , Map Text (Maybe RootTerm)
50 , Map (Text, Text) Int
52 getNgramsCooc cId maybeListId tabType maybeLimit = do
53 (ngs', ngs) <- getNgrams cId maybeListId tabType
57 take' (Just n) xs = take n xs
59 lId <- defaultList cId
60 lIds <- selectNodesWithUsername NodeList userMaster
62 myCooc <- Map.filter (>1) <$> getCoocByNgrams (Diagonal True)
63 <$> groupNodesByNgrams ngs
64 <$> getNodesByNgramsOnlyUser cId (lIds <> [lId]) (ngramsTypeFromTabType tabType)
65 (take' maybeLimit $ Map.keys ngs)
66 pure $ (ngs', ngs, myCooc)
70 getNgrams :: (FlowCmdM env err m)
71 => CorpusId -> Maybe ListId -> TabType
72 -> m (Map Text (ListType, Maybe Text), Map Text (Maybe RootTerm))
73 getNgrams cId maybeListId tabType = do
75 lId <- case maybeListId of
76 Nothing -> defaultList cId
77 Just lId' -> pure lId'
79 lists <- mapTermListRoot [lId] (ngramsTypeFromTabType tabType)
80 let maybeSyn = Map.unions $ map (\t -> filterListWithRoot t lists)
81 [GraphTerm, StopTerm, CandidateTerm]
82 pure (lists, maybeSyn)