]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Metrics.hs
Merge branch 'dev-social-list' into dev-merge
[gargantext.git] / src / Gargantext / Database / Action / Metrics.hs
1 {-|
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
8 Portability : POSIX
9
10 Node API
11 -}
12
13
14
15 module Gargantext.Database.Action.Metrics
16 where
17
18 import Data.Map (Map)
19 import qualified Data.Map as Map
20 import Data.Text (Text)
21 import Data.Vector (Vector)
22
23 import Gargantext.API.Ngrams.Types (TabType(..), ngramsTypeFromTabType)
24 import Gargantext.API.Ngrams.Tools (filterListWithRoot, groupNodesByNgrams, Diagonal(..), getCoocByNgrams, mapTermListRoot, RootTerm, getRepo)
25 import Gargantext.Core.Types (ListType(..), Limit, NodeType(..))
26 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
27 import Gargantext.Database.Action.Metrics.NgramsByNode (getNodesByNgramsOnlyUser{-, getTficfWith-})
28 import Gargantext.Database.Admin.Config (userMaster)
29 import Gargantext.Database.Admin.Types.Node (ListId, CorpusId)
30 import Gargantext.Database.Query.Table.Node (defaultList)
31 import Gargantext.Database.Query.Table.Node.Select
32 import Gargantext.Prelude
33 import Gargantext.Core.Text.Metrics (scored, Scored(..), {-localMetrics, toScored-})
34
35 getMetrics :: FlowCmdM env err m
36 => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
37 -> m (Map Text (ListType, Maybe Text), Vector (Scored Text))
38 getMetrics cId maybeListId tabType maybeLimit = do
39 (ngs, _, myCooc) <- getNgramsCooc cId maybeListId tabType maybeLimit
40 pure (ngs, scored myCooc)
41
42
43 getNgramsCooc :: (FlowCmdM env err m)
44 => CorpusId -> Maybe ListId -> TabType -> Maybe Limit
45 -> m ( Map Text (ListType, Maybe Text)
46 , Map Text (Maybe RootTerm)
47 , HashMap (Text, Text) Int
48 )
49 getNgramsCooc cId maybeListId tabType maybeLimit = do
50 (ngs', ngs) <- getNgrams cId maybeListId tabType
51
52 let
53 take' Nothing xs = xs
54 take' (Just n) xs = take n xs
55
56 lId <- defaultList cId
57 lIds <- selectNodesWithUsername NodeList userMaster
58
59 myCooc <- HM.filter (>1) <$> getCoocByNgrams (Diagonal True)
60 <$> groupNodesByNgrams ngs
61 <$> getNodesByNgramsOnlyUser cId (lIds <> [lId]) (ngramsTypeFromTabType tabType)
62 (take' maybeLimit $ Map.keys ngs)
63 pure $ (ngs', ngs, myCooc)
64
65
66
67 getNgrams :: (FlowCmdM env err m)
68 => CorpusId -> Maybe ListId -> TabType
69 -> m (Map Text (ListType, Maybe Text), Map Text (Maybe RootTerm))
70 getNgrams cId maybeListId tabType = do
71
72 lId <- case maybeListId of
73 Nothing -> defaultList cId
74 Just lId' -> pure lId'
75
76 lists <- mapTermListRoot [lId] (ngramsTypeFromTabType tabType) <$> getRepo
77 let maybeSyn = Map.unions $ map (\t -> filterListWithRoot t lists)
78 [MapTerm, StopTerm, CandidateTerm]
79 pure (lists, maybeSyn)
80