3 Description : Gargantext starter binary with Phylo
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
14 {-# LANGUAGE StandaloneDeriving #-}
15 {-# LANGUAGE TypeOperators #-}
16 {-# LANGUAGE Strict #-}
20 import Control.Concurrent.Async as CCA (mapConcurrently)
22 import Data.List ((++),concat)
24 import Data.Text (Text, unwords)
26 import GHC.IO (FilePath)
27 import Gargantext.Database.Admin.Types.Hyperdata
28 import Gargantext.Prelude
29 import Gargantext.Core.Text.Context (TermList)
30 import Gargantext.Core.Text.Corpus.Parsers (FileFormat(..),parseFile)
31 import Gargantext.Core.Text.Corpus.Parsers.CSV (csv_title, csv_abstract, csv_publication_year)
32 import Gargantext.Core.Text.List.Formats.CSV (csvMapTermList)
33 import Gargantext.Core.Text.Terms.WithList
34 import Gargantext.Core.Viz.Phylo
35 import Gargantext.Core.Viz.Phylo.LevelMaker
36 import Gargantext.Core.Viz.Phylo.Tools
37 import Gargantext.Core.Viz.Phylo.View.Export
38 import Gargantext.Core.Viz.Phylo.View.ViewMaker
39 import System.Directory (doesFileExist)
40 import System.Environment
41 import qualified Data.ByteString.Lazy as L
42 import qualified Data.List as DL
43 import qualified Data.Map as DM
44 import qualified Data.Text as DT
45 import qualified Data.Vector as DV
46 import qualified Gargantext.Core.Text.Corpus.Parsers.CSV as CSV
47 import qualified Prelude as P
55 type ListPath = FilePath
56 type FisPath = FilePath
57 type CorpusPath = FilePath
58 data CorpusType = Wos | Csv deriving (Show,Generic)
62 Conf { corpusPath :: CorpusPath
63 , corpusType :: CorpusType
64 , listPath :: ListPath
66 , outputPath :: FilePath
72 , timeFrameTh :: Double
75 , reBranchThr :: Double
78 , clusterSens :: Double
83 , minSizeBranch :: Int
84 } deriving (Show,Generic)
86 instance FromJSON Conf
89 instance FromJSON CorpusType
90 instance ToJSON CorpusType
93 decoder :: P.Either a b -> b
94 decoder (P.Left _) = P.error "Error"
95 decoder (P.Right x) = x
97 -- | Get the conf from a Json file
98 getJson :: FilePath -> IO L.ByteString
99 getJson path = L.readFile path
107 -- | To filter the Ngrams of a document based on the termList
108 filterTerms :: Patterns -> (a, Text) -> (a, [Text])
109 filterTerms patterns (y,d) = (y,termsInText patterns d)
111 --------------------------------------
112 termsInText :: Patterns -> Text -> [Text]
113 termsInText pats txt = DL.nub
116 $ extractTermsWithList pats txt
117 --------------------------------------
120 -- | To transform a Csv nfile into a readable corpus
121 csvToCorpus :: Limit -> CorpusPath -> IO ([(Int,Text)])
122 csvToCorpus limit csv = DV.toList
126 . DV.map (\n -> (csv_publication_year n, (csv_title n) <> " " <> (csv_abstract n)))
127 . snd <$> CSV.readFile csv
130 -- | To transform a Wos nfile into a readable corpus
131 wosToCorpus :: Limit -> CorpusPath -> IO ([(Int,Text)])
132 wosToCorpus limit path = DL.take limit
133 . map (\d -> ((fromJust $_hd_publication_year d)
134 ,(fromJust $_hd_title d) <> " " <> (fromJust $_hd_abstract d)))
135 . filter (\d -> (isJust $_hd_publication_year d)
136 && (isJust $_hd_title d)
137 && (isJust $_hd_abstract d))
139 <$> mapConcurrently (\idx -> parseFile WOS (path <> show(idx) <> ".txt")) [1..20]
142 -- | To use the correct parser given a CorpusType
143 fileToCorpus :: CorpusType -> Limit -> CorpusPath -> IO ([(Int,Text)])
144 fileToCorpus format limit path = case format of
145 Wos -> wosToCorpus limit path
146 Csv -> csvToCorpus limit path
149 -- | To parse a file into a list of Document
150 parse :: CorpusType -> Limit -> CorpusPath -> TermList -> IO [Document]
151 parse format limit path l = do
152 corpus <- fileToCorpus format limit path
153 let patterns = buildPatterns l
154 pure $ map ( (\(y,t) -> Document y t) . filterTerms patterns) corpus
157 -- | To parse an existing Fis file
158 parseFis :: FisPath -> Text -> Int -> Int -> Int -> Int -> IO [PhyloFis]
159 parseFis path name grain step support clique = do
160 fisExists <- doesFileExist (path <> (DT.unpack name) <> "_" <> show(grain) <> "_" <> show(step) <> "_" <> show(support) <> "_" <> show(clique) <> ".json")
163 fisJson <- (eitherDecode <$> getJson (path <> (DT.unpack name) <> "_" <> show(grain) <> "_" <> show(step) <> "_" <> show(support) <> "_" <> show(clique) <> ".json")) :: IO (P.Either P.String [PhyloFis])
168 P.Right fis -> pure fis
171 writeFis :: FisPath -> Text -> Int -> Int -> Int -> Int -> DM.Map (Date,Date) [PhyloFis] -> IO ()
172 writeFis path name grain step support clique fis = do
173 let fisPath = path <> (DT.unpack name) <> "_" <> show(grain) <> "_" <> show(step) <> "_" <> show(support) <> "_" <> show(clique) <> ".json"
174 L.writeFile fisPath $ encode (DL.concat $ DM.elems fis)
184 [jsonPath] <- getArgs
186 confJson <- (eitherDecode <$> getJson jsonPath) :: IO (P.Either P.String Conf)
189 P.Left err -> putStrLn err
192 termList <- csvMapTermList (listPath conf)
194 corpus <- parse (corpusType conf) (limit conf) (corpusPath conf) termList
196 putStrLn $ ("\n" <> show (length corpus) <> " parsed docs")
198 fis <- parseFis (fisPath conf) (phyloName conf) (timeGrain conf) (timeStep conf) (fisSupport conf) (fisClique conf)
200 putStrLn $ ("\n" <> show (length fis) <> " parsed fis")
202 let fis' = DM.fromListWith (++) $ DL.sortOn (fst . fst) $ map (\f -> (getFisPeriod f,[f])) fis
204 let query = PhyloQueryBuild (phyloName conf) "" (timeGrain conf) (timeStep conf)
205 (Fis $ FisParams True (fisSupport conf) (fisClique conf)) [] [] (WeightedLogJaccard $ WLJParams (timeTh conf) (timeSens conf)) (timeFrame conf) (timeFrameTh conf)
206 (reBranchThr conf) (reBranchNth conf) (phyloLevel conf)
207 (RelatedComponents $ RCParams $ WeightedLogJaccard $ WLJParams (clusterTh conf) (clusterSens conf))
209 let queryView = PhyloQueryView (viewLevel conf) Merge False 1 [BranchAge,BranchBirth,BranchGroups] [SizeBranch $ SBParams (minSizeBranch conf)] [GroupLabelIncDyn,BranchPeakInc] (Just (ByBranchBirth,Asc)) Json Flat True
211 let phylo = toPhylo query corpus termList fis'
213 writeFis (fisPath conf) (phyloName conf) (timeGrain conf) (timeStep conf) (fisSupport conf) (fisClique conf) (getPhyloFis phylo)
215 let view = toPhyloView queryView phylo
217 putStrLn $ ("phylo completed until level " <> show (phyloLevel conf) <> ", export at level " <> show (viewLevel conf))
219 let outputFile = (outputPath conf) <> (DT.unpack $ phyloName conf)
220 <> "_" <> show (limit conf) <> "_"
221 <> "_" <> show (timeTh conf) <> "_"
222 <> "_" <> show (timeSens conf) <> "_"
223 <> "_" <> show (clusterTh conf) <> "_"
224 <> "_" <> show (clusterSens conf)
227 P.writeFile outputFile $ dotToString $ viewToDot view