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 NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
14 {-# LANGUAGE RankNTypes #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
17 {-# LANGUAGE TemplateHaskell #-}
19 module Gargantext.API.Ngrams.NTree
22 import Data.Text (Text)
23 import Gargantext.Prelude
24 import GHC.Generics (Generic)
25 import Data.Aeson.TH (deriveJSON)
26 import Gargantext.Core.Utils.Prefix (unPrefix)
27 import Gargantext.Core.Types (ListType(..), NodeId)
28 import Gargantext.API.Ngrams
30 import Data.Maybe (catMaybes)
33 import qualified Data.Set as Set
34 import qualified Data.Map as Map
35 import qualified Data.List as List
40 data MyTree = MyTree { mt_label :: Text
42 , mt_children :: [MyTree]
43 } deriving (Generic, Show)
45 toMyTree :: Tree (Text,Double) -> MyTree
46 toMyTree (Node (l,v) xs) = MyTree l v (map toMyTree xs)
48 deriveJSON (unPrefix "mt_") ''MyTree
51 toTree :: ListType -> Map Text (Set NodeId) -> Map Text NgramsRepoElement -> [MyTree]
52 toTree lt vs m = map toMyTree $ unfoldForest buildNode roots
54 buildNode r = maybe ((r, value r),[]) (\x -> ((r, value r), mSetToList $ _nre_children x)) (Map.lookup r m)
56 value l = maybe 0 (fromIntegral . Set.size) $ Map.lookup l vs
58 rootsCandidates = catMaybes
60 $ map (\(c,c') -> case _nre_root c' of
62 _ -> _nre_root c' ) (Map.toList m)
65 $ filter (\(_,l) -> l == lt)
67 $ map (\c -> (,) <$> Just c <*> (_nre_list <$> Map.lookup c m)) rootsCandidates