2 Module : Gargantext.API.Ngrams.NgramsTree
3 Description : Tree of Ngrams
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
14 module Gargantext.API.Ngrams.NgramsTree
17 import Data.Aeson.TH (deriveJSON)
18 import Data.HashMap.Strict (HashMap)
19 import Data.Maybe (catMaybes)
22 import Data.Text (Text)
24 import GHC.Generics (Generic)
25 import Gargantext.API.Ngrams.Types
26 import Gargantext.Core.Types (ListType(..), NodeId)
27 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
28 import Gargantext.Prelude
29 import Test.QuickCheck
30 import qualified Data.HashMap.Strict as HashMap
31 import qualified Data.List as List
32 import qualified Data.Set as Set
37 data NgramsTree = NgramsTree { mt_label :: Text
39 , mt_children :: [NgramsTree]
41 deriving (Generic, Show)
43 toNgramsTree :: Tree (NgramsTerm,Double) -> NgramsTree
44 toNgramsTree (Node (NgramsTerm l,v) xs) = NgramsTree l v (map toNgramsTree xs)
46 deriveJSON (unPrefix "mt_") ''NgramsTree
48 instance ToSchema NgramsTree where
49 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "mt_")
50 instance Arbitrary NgramsTree
52 arbitrary = NgramsTree <$> arbitrary <*> arbitrary <*> arbitrary
55 -> HashMap NgramsTerm (Set NodeId)
56 -> HashMap NgramsTerm NgramsRepoElement
58 toTree lt vs m = map toNgramsTree $ unfoldForest buildNode roots
60 buildNode r = maybe ((r, value r),[])
61 (\x -> ((r, value r), mSetToList $ _nre_children x))
64 value l = maybe 0 (fromIntegral . Set.size) $ HashMap.lookup l vs
66 rootsCandidates :: [NgramsTerm]
67 rootsCandidates = catMaybes
69 $ map (\(c, c') -> case _nre_root c' of
75 $ filter (\(_,l) -> l == lt)
77 $ map (\c -> (,) <$> Just c <*> (_nre_list <$> HashMap.lookup c m))