2 Module : Gargantext.Core.Text.Ngrams.Lists
3 Description : Tools to build lists
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 module Gargantext.Core.Text.List
16 -- import Data.Either (partitionEithers, Either(..))
19 import Data.Text (Text)
20 import Gargantext.API.Ngrams (NgramsElement, mkNgramsElement, RootParent(..), mSetFromList)
21 -- import Gargantext.API.Ngrams.Tools (getCoocByNgrams', Diagonal(..))
22 import Gargantext.Core (Lang(..))
23 import Gargantext.Core.Types (ListType(..), MasterCorpusId, UserCorpusId, Ordering(..))
24 import Gargantext.Database.Action.Metrics.NgramsByNode (ngramsGroup, getNodesByNgramsUser, groupNodesByNgramsWith)
25 import Gargantext.Database.Action.Metrics.TFICF (getTficf)
26 import Gargantext.Core.Text.Metrics.TFICF (sortTficf)
27 import Gargantext.Database.Prelude (Cmd)
28 import Gargantext.Database.Schema.Ngrams (NgramsType(..))
29 import Gargantext.Prelude
30 import Gargantext.Core.Text (size)
31 import Gargantext.Core.Text.List.Learn (Model(..))
32 -- import Gargantext.Core.Text.Metrics (takeScored)
33 import qualified Data.Char as Char
34 import qualified Data.List as List
35 import qualified Data.Map as Map
36 import qualified Data.Set as Set
37 import qualified Data.Text as Text
40 data NgramsListBuilder = BuilderStepO { stemSize :: Int
44 | BuilderStep1 { withModel :: Model }
45 | BuilderStepN { withModel :: Model }
46 | Tficf { nlb_lang :: Lang
49 , nlb_stopSize :: StopSize
50 , nlb_userCorpusId :: UserCorpusId
51 , nlb_masterCorpusId :: MasterCorpusId
55 data StopSize = StopSize {unStopSize :: Int}
57 -- | TODO improve grouping functions of Authors, Sources, Institutes..
58 buildNgramsLists :: Lang
64 -> Cmd err (Map NgramsType [NgramsElement])
65 buildNgramsLists l n m s uCid mCid = do
66 ngTerms <- buildNgramsTermsList l n m s uCid mCid
67 othersTerms <- mapM (buildNgramsOthersList uCid identity)
68 [Authors, Sources, Institutes]
69 pure $ Map.unions $ othersTerms <> [ngTerms]
72 buildNgramsOthersList :: UserCorpusId
75 -> Cmd err (Map NgramsType [NgramsElement])
76 buildNgramsOthersList uCid groupIt nt = do
77 ngs <- groupNodesByNgramsWith groupIt <$> getNodesByNgramsUser uCid nt
82 $ List.sortOn (Set.size . snd . snd)
85 graphTerms = List.take listSize all'
86 candiTerms = List.drop listSize all'
88 pure $ Map.unionsWith (<>) [ toElements MapTerm graphTerms
89 , toElements CandidateTerm candiTerms
93 Map.fromList [(nt, [ mkNgramsElement t nType Nothing (mSetFromList [])
98 buildNgramsTermsList :: Lang
104 -> Cmd err (Map NgramsType [NgramsElement])
105 buildNgramsTermsList l n m s uCid mCid = do
106 candidates <- sortTficf Up <$> getTficf uCid mCid NgramsTerms
107 -- printDebug "head candidates" (List.take 10 $ candidates)
108 -- printDebug "tail candidates" (List.take 10 $ List.reverse $ candidates)
111 listSize = 400 :: Double
112 (candidatesHead, candidatesTail0) = List.splitAt 3 candidates
114 (mono, multi) = List.partition (\t -> (size . fst) t < 2) candidatesTail0
115 (monoHead , monoTail ) = List.splitAt (round $ 0.60 * listSize) mono
116 (multiHead, multiTail) = List.splitAt (round $ 0.40 * listSize) multi
118 termList = (map (toGargList ((isStopTerm s) . fst) CandidateTerm) candidatesHead)
119 <> (map (toGargList ((isStopTerm s) . fst) MapTerm) (monoHead <> multiHead))
120 <> (map (toGargList ((isStopTerm s) . fst) CandidateTerm) (monoTail <> multiTail))
123 $ map toNgramsElement
125 $ map (\(listType, (t,d)) -> ( ngramsGroup l n m t
126 , GroupedText listType t d Set.empty
130 pure $ Map.fromList [(NgramsTerms, ngs)]
132 type Group = Lang -> Int -> Int -> Text -> Text
135 data GroupedText = GroupedText { _gt_listType :: ListType
137 , _gt_score :: Double
138 , _gt_group :: Set Text
140 groupStems :: [(Stem, GroupedText)] -> [GroupedText]
141 groupStems = Map.elems . Map.fromListWith grouping
143 grouping (GroupedText lt1 label1 score1 group1)
144 (GroupedText lt2 label2 score2 group2)
145 | score1 >= score2 = GroupedText lt label1 score1 (Set.insert label2 gr)
146 | otherwise = GroupedText lt label2 score2 (Set.insert label1 gr)
149 gr = Set.union group1 group2
151 toNgramsElement :: GroupedText -> [NgramsElement]
152 toNgramsElement (GroupedText listType label _ setNgrams) =
153 [parentElem] <> childrenElems
156 children = Set.toList setNgrams
157 parentElem = mkNgramsElement parent
160 (mSetFromList children)
161 childrenElems = map (\t -> mkNgramsElement t listType
162 (Just $ RootParent parent parent)
167 toGargList :: (b -> Bool) -> ListType -> b -> (ListType, b)
168 toGargList isStop l n = case isStop n of
169 True -> (StopTerm, n)
173 isStopTerm :: StopSize -> Text -> Bool
174 isStopTerm (StopSize n) x = Text.length x < n || any isStopChar (Text.unpack x)
176 isStopChar c = not (c `elem` ("- /()%" :: [Char]) || Char.isAlpha c)