2 Module : Gargantext.API.Ngrams.NTree
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.NTree
17 import Data.Text (Text)
18 import Gargantext.Prelude
19 import GHC.Generics (Generic)
20 import Data.Aeson.TH (deriveJSON)
21 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
22 import Gargantext.Core.Types (ListType(..), NodeId)
23 import Gargantext.API.Ngrams
25 import Data.Maybe (catMaybes)
29 import qualified Data.Set as Set
30 import qualified Data.Map as Map
31 import qualified Data.List as List
32 import Test.QuickCheck
37 data MyTree = MyTree { mt_label :: Text
39 , mt_children :: [MyTree]
40 } deriving (Generic, Show)
42 toMyTree :: Tree (Text,Double) -> MyTree
43 toMyTree (Node (l,v) xs) = MyTree l v (map toMyTree xs)
45 deriveJSON (unPrefix "mt_") ''MyTree
47 instance ToSchema MyTree where
48 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "mt_")
49 instance Arbitrary MyTree
51 arbitrary = MyTree <$> arbitrary <*> arbitrary <*> arbitrary
53 toTree :: ListType -> Map Text (Set NodeId) -> Map Text NgramsRepoElement -> [MyTree]
54 toTree lt vs m = map toMyTree $ unfoldForest buildNode roots
56 buildNode r = maybe ((r, value r),[]) (\x -> ((r, value r), mSetToList $ _nre_children x)) (Map.lookup r m)
58 value l = maybe 0 (fromIntegral . Set.size) $ Map.lookup l vs
60 rootsCandidates = catMaybes
62 $ map (\(c,c') -> case _nre_root c' of
64 _ -> _nre_root c' ) (Map.toList m)
67 $ filter (\(_,l) -> l == lt)
69 $ map (\c -> (,) <$> Just c <*> (_nre_list <$> Map.lookup c m)) rootsCandidates