2 Module : Gargantext.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
12 {-# LANGUAGE FlexibleContexts #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE RankNTypes #-}
17 module Gargantext.Text.List
20 import Data.Either (partitionEithers, Either(..))
23 import Data.Text (Text)
24 import Gargantext.API.Ngrams (NgramsElement, mkNgramsElement, RootParent(..), mSetFromList)
25 import Gargantext.API.Ngrams.Tools (getCoocByNgrams', Diagonal(..))
26 import Gargantext.Core (Lang(..))
27 import Gargantext.Core.Types (ListType(..), MasterCorpusId, UserCorpusId, NodeId)
28 import Gargantext.Database.Action.Metrics.NgramsByNode (getTficf', sortTficf, ngramsGroup, getNodesByNgramsUser, groupNodesByNgramsWith)
29 import Gargantext.Database.Admin.Utils (Cmd)
30 import Gargantext.Database.Schema.Ngrams (NgramsType(..))
31 import Gargantext.Prelude
32 import Gargantext.Text.List.Learn (Model(..))
33 import Gargantext.Text.Metrics (takeScored)
34 import qualified Data.Char as Char
35 import qualified Data.List as List
36 import qualified Data.Map as Map
37 import qualified Data.Set as Set
38 import qualified Data.Text as Text
41 data NgramsListBuilder = BuilderStepO { stemSize :: Int
45 | BuilderStep1 { withModel :: Model }
46 | BuilderStepN { withModel :: Model }
47 | Tficf { nlb_lang :: Lang
50 , nlb_stopSize :: StopSize
51 , nlb_userCorpusId :: UserCorpusId
52 , nlb_masterCorpusId :: MasterCorpusId
56 data StopSize = StopSize {unStopSize :: Int}
58 -- | TODO improve grouping functions of Authors, Sources, Institutes..
59 buildNgramsLists :: Lang -> Int -> Int -> StopSize -> UserCorpusId -> MasterCorpusId
60 -> Cmd err (Map NgramsType [NgramsElement])
61 buildNgramsLists l n m s uCid mCid = do
62 ngTerms <- buildNgramsTermsList l n m s uCid mCid
63 othersTerms <- mapM (buildNgramsOthersList uCid identity) [Authors, Sources, Institutes]
64 pure $ Map.unions $ othersTerms <> [ngTerms]
67 buildNgramsOthersList :: UserCorpusId -> (Text -> Text) -> NgramsType
68 -> Cmd err (Map NgramsType [NgramsElement])
69 buildNgramsOthersList uCid groupIt nt = do
70 ngs <- groupNodesByNgramsWith groupIt <$> getNodesByNgramsUser uCid nt
74 all' = List.reverse $ List.sortOn (Set.size . snd . snd) $ Map.toList ngs
75 graphTerms = List.take listSize all'
76 candiTerms = List.drop listSize all'
77 pure $ Map.unionsWith (<>) [ toElements GraphTerm graphTerms
78 , toElements CandidateTerm candiTerms]
80 toElements nType x = Map.fromList [(nt, [ mkNgramsElement t nType Nothing (mSetFromList [])
87 buildNgramsTermsList' :: UserCorpusId
89 -> ((Text, (Set Text, Set NodeId)) -> Bool) -> Int -> Int
90 -> Cmd err (Map NgramsType [NgramsElement])
92 buildNgramsTermsList' uCid groupIt stop gls is = do
93 ngs <- groupNodesByNgramsWith groupIt <$> getNodesByNgramsUser uCid NgramsTerms
96 (stops, candidates) = partitionEithers
97 $ map (\t -> if stop t then Left t else Right t)
99 $ Map.filter ((\s' -> Set.size s' > 1) . snd) ngs
101 (maps, candidates') = takeScored gls is
102 $ getCoocByNgrams' snd (Diagonal True)
103 $ Map.fromList candidates
106 toList' t = (fst t, (fromIntegral $ Set.size $ snd $ snd t, fst $ snd t))
109 , List.filter (\(k,_) -> List.elem k candidates') candidates
110 , List.filter (\(k,_) -> List.elem k maps) candidates
113 let ngs' = List.concat
114 $ map toNgramsElement
115 $ map (\t -> (StopTerm, toList' t)) s
116 <> map (\t -> (CandidateTerm, toList' t)) c
117 <> map (\t -> (GraphTerm, toList' t)) m
119 pure $ Map.fromList [(NgramsTerms, ngs')]
122 buildNgramsTermsList :: Lang -> Int -> Int -> StopSize -> UserCorpusId -> MasterCorpusId
123 -> Cmd err (Map NgramsType [NgramsElement])
124 buildNgramsTermsList l n m s uCid mCid = do
125 candidates <- sortTficf <$> getTficf' uCid mCid NgramsTerms (ngramsGroup l n m)
127 candidatesSize = 2000
130 candidatesHead = List.take candidatesSize candidates
131 candidatesTail = List.drop candidatesSize candidates
132 termList = (toTermList a b ((isStopTerm s) . fst) candidatesHead)
133 <> (map (toList ((isStopTerm s) .fst) CandidateTerm) candidatesTail)
134 let ngs = List.concat $ map toNgramsElement termList
136 pure $ Map.fromList [(NgramsTerms, ngs)]
139 toNgramsElement :: (ListType, (Text, (Double, Set Text))) -> [NgramsElement]
140 toNgramsElement (listType, (_stem, (_score, setNgrams))) =
141 case Set.toList setNgrams of
143 (parent:children) -> [parentElem] <> childrenElems
145 parentElem = mkNgramsElement parent
148 (mSetFromList children)
149 childrenElems = map (\t -> mkNgramsElement t listType
150 (Just $ RootParent parent parent)
155 toList :: (b -> Bool) -> ListType -> b -> (ListType, b)
156 toList stop l n = case stop n of
157 True -> (StopTerm, n)
161 toTermList :: Int -> Int -> (a -> Bool) -> [a] -> [(ListType, a)]
162 toTermList _ _ _ [] = []
163 toTermList a b stop ns = -- trace ("computing toTermList") $
164 map (toList stop CandidateTerm) xs
165 <> map (toList stop GraphTerm) ys
166 <> toTermList a b stop zs
175 isStopTerm :: StopSize -> Text -> Bool
176 isStopTerm (StopSize n) x = Text.length x < n || any isStopChar (Text.unpack x)
178 isStopChar c = not (c `elem` ("- /()%" :: [Char]) || Char.isAlpha c)