Metrics.hs 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{-|
Module      : Gargantext.Database.Metrics
Description : Get Metrics from Storage (Database like)
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

Node API
-}



15
module Gargantext.Database.Action.Metrics
16 17 18 19 20
  where

import Data.Map (Map)
import Data.Text (Text)
import Gargantext.API.Ngrams (TabType(..), ngramsTypeFromTabType)
21
import Gargantext.API.Ngrams.Tools (filterListWithRoot, groupNodesByNgrams, Diagonal(..), getCoocByNgrams, mapTermListRoot, RootTerm, getRepo)
22
import Gargantext.Core.Types (ListType(..), Limit, NodeType(..))
23 24 25
import Gargantext.Database.Action.Flow (FlowCmdM)
import Gargantext.Database.Action.Metrics.NgramsByNode (getNodesByNgramsOnlyUser{-, getTficfWith-})
import Gargantext.Database.Admin.Config (userMaster)
26
import Gargantext.Database.Admin.Types.Node (ListId, CorpusId)
27 28
import Gargantext.Database.Query.Table.Node (defaultList)
import Gargantext.Database.Query.Table.Node.Select
29
import Gargantext.Prelude
30
import Gargantext.Text.Metrics (scored, Scored(..), {-localMetrics, toScored-})
31 32
import qualified Data.Map    as Map

33
getMetrics :: FlowCmdM env err m
34 35
            => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
            -> m (Map Text (ListType, Maybe Text), [Scored Text])
36
getMetrics cId maybeListId tabType maybeLimit = do
37
  (ngs, _, myCooc) <- getNgramsCooc cId maybeListId tabType maybeLimit
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  pure (ngs, scored myCooc)


getNgramsCooc :: (FlowCmdM env err m)
            => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
            -> m ( Map Text (ListType, Maybe Text)
                 , Map Text (Maybe RootTerm)
                 , Map (Text, Text) Int
                 )
getNgramsCooc cId maybeListId tabType maybeLimit = do
  (ngs', ngs) <- getNgrams cId maybeListId tabType
  
  let
    take' Nothing xs  = xs
    take' (Just n) xs = take n xs
53 54 55 56

  lId  <- defaultList cId
  lIds <- selectNodesWithUsername NodeList userMaster

57 58
  myCooc <- Map.filter (>1) <$> getCoocByNgrams (Diagonal True)
                            <$> groupNodesByNgrams ngs
59
                            <$> getNodesByNgramsOnlyUser cId (lIds <> [lId]) (ngramsTypeFromTabType tabType)
60 61 62 63 64 65 66 67 68
                                                             (take' maybeLimit $ Map.keys ngs)
  pure $ (ngs', ngs, myCooc)



getNgrams :: (FlowCmdM env err m)
            => CorpusId -> Maybe ListId -> TabType
            -> m (Map Text (ListType, Maybe Text), Map Text (Maybe RootTerm))
getNgrams cId maybeListId tabType = do
Alexandre Delanoë's avatar
Alexandre Delanoë committed
69

70 71 72 73
  lId <- case maybeListId of
    Nothing   -> defaultList cId
    Just lId' -> pure lId'

74
  lists <- mapTermListRoot [lId] (ngramsTypeFromTabType tabType) <$> getRepo
75
  let maybeSyn = Map.unions $ map (\t -> filterListWithRoot t lists)
76
                             [MapTerm, StopTerm, CandidateTerm]
77 78
  pure (lists, maybeSyn)