2 Module : Gargantext.Database.Metrics.TFICF
3 Description : Ngrams by Node user and master
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE QuasiQuotes #-}
14 module Gargantext.Database.Action.Metrics.TFICF
17 -- import Debug.Trace (trace)
18 -- import Gargantext.Core (Lang(..))
19 import Data.HashMap.Strict (HashMap)
20 import qualified Data.HashMap.Strict as HM
21 import Data.Maybe (fromMaybe)
22 import Gargantext.Core
23 import Gargantext.Core.Text.Metrics.TFICF
24 import Gargantext.Database.Action.Metrics.NgramsByContext (getContextsByNgramsUser, {-getOccByNgramsOnlyFast,-} getOccByNgramsOnlyFast_withSample)
25 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
26 import Gargantext.Database.Prelude (Cmd)
27 import Gargantext.Database.Query.Table.NodeContext (selectCountDocs)
28 import Gargantext.Database.Schema.Ngrams (NgramsType(..))
29 import Gargantext.API.Ngrams.Types
30 import Gargantext.Prelude
31 import qualified Data.Set as Set
34 getTficf :: HasDBid NodeType
38 -> Cmd err (HashMap NgramsTerm Double)
39 getTficf cId mId nt = do
40 mapTextDoubleLocal <- HM.filter (> 1)
41 <$> HM.map (fromIntegral . Set.size)
42 <$> getContextsByNgramsUser cId nt
44 mapTextDoubleGlobal <- HM.map fromIntegral
45 <$> getOccByNgramsOnlyFast mId nt (HM.keys mapTextDoubleLocal)
47 countLocal <- selectCountDocs cId
48 countGlobal <- selectCountDocs mId
50 printDebug "getTficf" (mapTextDoubleLocal, mapTextDoubleGlobal, countLocal, countGlobal)
52 pure $ HM.mapWithKey (\t n ->
53 tficf (TficfInfra (Count n )
54 (Total $ fromIntegral countLocal))
55 (TficfSupra (Count $ fromMaybe 0 $ HM.lookup t mapTextDoubleGlobal)
56 (Total $ fromIntegral countGlobal))
60 getTficf_withSample :: HasDBid NodeType
64 -> Cmd err (HashMap NgramsTerm Double)
65 getTficf_withSample cId mId nt = do
66 mapTextDoubleLocal <- HM.filter (> 1)
67 <$> HM.map (fromIntegral . Set.size)
68 <$> getContextsByNgramsUser cId nt
70 countLocal <- selectCountDocs cId
71 let countGlobal = countLocal
74 mapTextDoubleGlobal <- HM.map fromIntegral
75 <$> getOccByNgramsOnlyFast_withSample mId countGlobal nt
76 (HM.keys mapTextDoubleLocal)
78 --printDebug "getTficf_withSample" (mapTextDoubleLocal, mapTextDoubleGlobal, countLocal, countGlobal)
79 pure $ HM.mapWithKey (\t n ->
80 tficf (TficfInfra (Count n )
81 (Total $ fromIntegral countLocal))
82 (TficfSupra (Count $ fromMaybe 0 $ HM.lookup t mapTextDoubleGlobal)
83 (Total $ fromIntegral countGlobal))