2 Module : Gargantext.Viz.Phylo.Main
3 Description : Phylomemy Main
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 #-}
16 {-# LANGUAGE ViewPatterns #-}
18 module Gargantext.Viz.Phylo.Main
21 import Debug.Trace (trace)
22 import qualified Data.Text as Text
23 import Data.Text (Text)
26 import GHC.IO (FilePath)
28 import Gargantext.Prelude
29 import Gargantext.Text.Context (TermList)
30 import qualified Data.Map as Map
31 import qualified Data.List as List
32 import Gargantext.Viz.Phylo.View.Export
33 import Gargantext.Viz.Phylo.Tools
34 import Gargantext.Viz.Phylo.LevelMaker
35 import Gargantext.Core.Types
36 import Gargantext.Text.Terms.WithList
37 -- import Gargantext.Database.Config (userMaster)
38 import Gargantext.Database.Schema.Node (defaultList)
39 import Gargantext.Database.Schema.NodeNode (selectDocs)
40 import Gargantext.Database.Schema.Ngrams (NgramsType(..))
41 -- import Gargantext.Database.Metrics.NgramsByNode (getNodesByNgramsOnlyUser)
42 -- import Gargantext.Database.Node.Select (selectNodesWithUsername)
43 import Gargantext.Database.Flow
44 import Gargantext.API.Ngrams.Tools (getTermsWith)
45 -- TODO : git mv ViewMaker Maker
46 import Gargantext.Viz.Phylo.View.ViewMaker
47 import Gargantext.Viz.Phylo hiding (Svg, Dot)
48 import Control.Monad.IO.Class (liftIO)
49 import qualified Data.ByteString as DB
51 type MinSizeBranch = Int
53 flowPhylo :: FlowCmdM env ServantErr m
55 -> Level -> MinSizeBranch
58 flowPhylo cId l m fp = do
60 list <- defaultList cId
61 -- listMaster <- selectNodesWithUsername NodeList userMaster
62 termList <- Map.toList <$> getTermsWith Text.words [list] NgramsTerms GraphTerm
63 --printDebug "termList" termList
65 --x <- mapTermListRoot [list] NgramsTerms
66 --printDebug "mapTermListRoot" x
68 -- TODO optimize unwords
70 docs' <- catMaybes <$> map (\h -> (,) <$> _hyperdataDocument_publication_year h
71 <*> _hyperdataDocument_abstract h
74 let patterns = buildPatterns termList
75 let docs = map ( (\(y,t) -> Document y t) . filterTerms patterns) docs'
76 --printDebug "docs" docs
77 --printDebug "docs" termList
79 liftIO $ flowPhylo' (List.sortOn date docs) termList l m fp
84 parse :: TermList -> [(Date, Text)] -> IO [Document]
86 let patterns = buildPatterns l
87 pure $ map ( (\(y,t) -> Document y t) . filterTerms patterns) c
90 -- | To filter the Ngrams of a document based on the termList
91 filterTerms :: Patterns -> (Date, Text) -> (Date, [Text])
92 filterTerms patterns (y,d) = (y,termsInText patterns d)
94 --------------------------------------
95 termsInText :: Patterns -> Text -> [Text]
96 termsInText pats txt = List.nub $ List.concat $ map (map Text.unwords) $ extractTermsWithList pats txt
97 --------------------------------------
100 -- TODO SortedList Document
101 flowPhylo' :: [Document] -> TermList -- ^Build
102 -> Level -> MinSizeBranch -- ^View
105 flowPhylo' corpus terms l m fp = do
107 phylo = buildPhylo corpus terms
108 phVie = viewPhylo l m phylo
113 defaultQuery :: PhyloQueryBuild
114 defaultQuery = defaultQueryBuild'
116 "Default Description"
118 buildPhylo :: [Document] -> TermList -> Phylo
119 buildPhylo = trace (show defaultQuery) $ buildPhylo' defaultQuery
121 buildPhylo' :: PhyloQueryBuild -> [Document] -> TermList -> Phylo
122 buildPhylo' q corpus termList = toPhylo q corpus termList Map.empty
124 queryView :: Level -> MinSizeBranch -> PhyloQueryView
125 queryView level _minSizeBranch = PhyloQueryView level Merge False 2
128 -- [SizeBranch $ SBParams minSizeBranch]
129 [BranchPeakFreq,GroupLabelCooc]
130 (Just (ByBranchAge,Asc))
133 viewPhylo :: Level -> MinSizeBranch -> Phylo -> PhyloView
134 viewPhylo l b phylo = toPhyloView (queryView l b) phylo
136 writePhylo :: FilePath -> PhyloView -> IO FilePath
137 writePhylo fp phview = runGraphviz (viewToDot phview) Svg fp
139 viewPhylo2Svg :: PhyloView -> IO DB.ByteString
140 viewPhylo2Svg p = graphvizWithHandle Dot (viewToDot p) Svg DB.hGetContents