]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Phylo/Example.hs
Merge branch 'patch-1' of ssh://gitlab.iscpif.fr:20022/gargantext/haskell-gargantext...
[gargantext.git] / src / Gargantext / Viz / Phylo / Example.hs
1 {-|
2 Module : Gargantext.Viz.Phylo.Example
3 Description : Phylomemy example based on history of Cleopatre.
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -- | Cesar et Cleôpatre
11 -- Exemple de phylomemie
12 -- French without accents
13
14
15 TODO:
16 - split the functions : RAW -> Document -> Ngrams
17
18 -- reverse history: antechronologique
19 -- metrics support
20
21
22 -}
23
24 {-# LANGUAGE NoImplicitPrelude #-}
25 {-# LANGUAGE FlexibleContexts #-}
26 {-# LANGUAGE OverloadedStrings #-}
27 {-# LANGUAGE MultiParamTypeClasses #-}
28
29 module Gargantext.Viz.Phylo.Example where
30
31 import Data.GraphViz.Types.Generalised (DotGraph)
32
33 import Control.Lens hiding (both, Level)
34 import Data.Text (Text, toLower)
35 import Data.List ((++))
36 import Data.Map (Map,empty)
37 import Data.Tuple (fst)
38 import Data.Vector (Vector)
39 import Gargantext.Prelude
40 import Gargantext.Text.Context (TermList)
41 import Gargantext.Viz.Phylo
42 import Gargantext.Viz.Phylo.Cluster
43 import Gargantext.Viz.Phylo.Aggregates
44 import Gargantext.Viz.Phylo.BranchMaker
45 import Gargantext.Viz.Phylo.LevelMaker
46 import Gargantext.Viz.Phylo.LinkMaker
47 import Gargantext.Viz.Phylo.Tools
48 import Gargantext.Viz.Phylo.View.ViewMaker
49 import Gargantext.Viz.Phylo.View.Export
50 import Gargantext.Viz.Phylo.Main (writePhylo)
51 import GHC.IO (FilePath)
52 import qualified Data.List as List
53
54 ------------------------------------------------------
55 -- | STEP 12 | -- Create a PhyloView from a user Query
56 ------------------------------------------------------
57
58
59 export :: IO ()
60 export = dotToFile "/home/qlobbe/data/phylo/output/cesar_cleopatre.dot" phyloDot
61
62 phyloDot :: DotGraph DotId
63 phyloDot = viewToDot phyloView
64
65
66 phyloExport :: FilePath -> IO FilePath
67 phyloExport fp = writePhylo fp phyloView
68
69 phyloView :: PhyloView
70 phyloView = toPhyloView (queryParser' queryViewEx) phyloFromQuery
71
72 -- | To do : create an other request handler and an other query parser
73 queryParser' :: [Char] -> PhyloQueryView
74 queryParser' _q = phyloQueryView
75
76 queryViewEx :: [Char]
77 queryViewEx = "level=3"
78 ++ "&childs=false"
79 ++ "&filter=LonelyBranchFilter"
80 ++ "&metric=BranchAge"
81 ++ "&tagger=BranchPeakFreq"
82 ++ "&tagger=GroupLabelCooc"
83
84
85 phyloQueryView :: PhyloQueryView
86 phyloQueryView = PhyloQueryView 1 Merge False 1 [BranchAge,BranchBirth,BranchGroups] [] [BranchPeakInc,GroupLabelIncDyn] (Just (ByBranchBirth,Asc)) Json Flat True
87
88
89 --------------------------------------------------
90 -- | STEP 11 | -- Create a Phylo from a user Query
91 --------------------------------------------------
92
93
94 phyloFromQuery :: Phylo
95 phyloFromQuery = toPhylo phyloQueryBuild docs termList empty
96
97 -- | To do : create a request handler and a query parser
98 queryParser :: [Char] -> PhyloQueryBuild
99 queryParser _q = phyloQueryBuild
100
101 queryEx :: [Char]
102 queryEx = "title=Cesar et Cleôpatre"
103 ++ "&desc=An example of Phylomemy (french without accent)"
104 ++ "grain=5&steps=3"
105 ++ "cluster=FrequentItemSet"
106 ++ "interTemporalMatching=WeightedLogJaccard"
107 ++ "nthLevel=2"
108 ++ "nthCluster=RelatedComponents"
109 ++ "nthProximity=Filiation"
110
111 phyloQueryBuild :: PhyloQueryBuild
112 phyloQueryBuild = PhyloQueryBuild "Cesar et Cleôpatre" "An example of Phylomemy (french without accent)"
113 3 1 defaultFis [] [] (WeightedLogJaccard $ WLJParams 0.9 10) 5 0.8 0.5 4 1 (RelatedComponents $ RCParams $ WeightedLogJaccard $ WLJParams 0.3 0)
114
115
116
117 ----------------------------------------------------------------------------------------------------------------------------
118 -- | STEP 10 | -- Incrementaly cluster the PhyloGroups n times, link them through the Periods and build level n of the Phylo
119 ----------------------------------------------------------------------------------------------------------------------------
120
121
122 phylo6 :: Phylo
123 phylo6 = toNthLevel 6 defaultWeightedLogJaccard (RelatedComponents (initRelatedComponents (Just defaultWeightedLogJaccard))) phylo3
124
125
126 phylo3 :: Phylo
127 phylo3 = setPhyloBranches 3
128 $ interTempoMatching Descendant 3 defaultWeightedLogJaccard
129 $ interTempoMatching Ascendant 3 defaultWeightedLogJaccard
130 $ setLevelLinks (2,3)
131 $ addPhyloLevel 3
132 (phyloToClusters 2 (RelatedComponents (initRelatedComponents (Just defaultWeightedLogJaccard))) phyloBranch2)
133 phyloBranch2
134
135
136 --------------------------------
137 -- | STEP 9 | -- Cluster the Fis
138 --------------------------------
139
140
141 phyloBranch2 :: Phylo
142 phyloBranch2 = setPhyloBranches 2 phylo2_c
143
144
145 phylo2_c :: Phylo
146 phylo2_c = interTempoMatching Descendant 2 defaultWeightedLogJaccard phylo2_p
147
148
149 phylo2_p :: Phylo
150 phylo2_p = interTempoMatching Ascendant 2 defaultWeightedLogJaccard phylo2_1_2
151
152
153 phylo2_1_2 :: Phylo
154 phylo2_1_2 = setLevelLinks (1,2) phylo2
155
156
157 -- | phylo2 allready contains the LevelChilds links from 2 to 1
158 phylo2 :: Phylo
159 phylo2 = addPhyloLevel 2 phyloCluster phyloBranch1
160
161
162 phyloCluster :: Map (Date,Date) [PhyloCluster]
163 phyloCluster = phyloToClusters 2 (RelatedComponents $ RCParams $ WeightedLogJaccard $ WLJParams 0.05 10) phyloBranch1
164
165
166 ----------------------------------
167 -- | STEP 8 | -- Find the Branches
168 ----------------------------------
169
170
171 phyloBranch1 :: Phylo
172 phyloBranch1 = setPhyloBranches 1 phylo1_c
173
174
175 --------------------------------------------------------------------
176 -- | STEP 7 | -- Link the PhyloGroups of level 1 through the Periods
177 --------------------------------------------------------------------
178
179
180 phylo1_c :: Phylo
181 phylo1_c = interTempoMatching Descendant 1 defaultWeightedLogJaccard phylo1_p
182
183
184 phylo1_p :: Phylo
185 phylo1_p = interTempoMatching Ascendant 1 defaultWeightedLogJaccard phylo1_0_1
186
187
188 -----------------------------------------------
189 -- | STEP 6 | -- Build the level 1 of the Phylo
190 -----------------------------------------------
191
192
193 phylo1_0_1 :: Phylo
194 phylo1_0_1 = setLevelLinks (0,1) phylo1
195
196
197 -- phylo1_1_0 :: Phylo
198 -- phylo1_1_0 = setLevelLinks (1,0) phylo1
199
200
201 phylo1 :: Phylo
202 phylo1 = addPhyloLevel (1) phyloFis phylo'
203
204
205 -------------------------------------------------------------------
206 -- | STEP 5 | -- Create lists of Frequent Items Set and filter them
207 -------------------------------------------------------------------
208
209 phylo' :: Phylo
210 phylo' = phylo & phylo_fis .~ phyloFis
211
212 phyloFis :: Map (Date, Date) [PhyloFis]
213 phyloFis = refineFis (docsToFis phyloDocs phylo) True 1 1
214
215 ----------------------------------------
216 -- | STEP 2 | -- Init a Phylo of level 0
217 ----------------------------------------
218
219
220 phylo :: Phylo
221 phylo = addPhyloLevel 0 phyloDocs phyloBase
222
223
224 phyloDocs :: Map (Date, Date) [Document]
225 phyloDocs = groupDocsByPeriod date (getPhyloPeriods phyloBase) docs
226
227
228 ------------------------------------------------------------------------
229 -- | STEP 1 | -- Init the Base of the Phylo from Periods and Foundations
230 ------------------------------------------------------------------------
231
232
233 phyloBase :: Phylo
234 phyloBase = toPhyloBase phyloQueryBuild phyloParam docs termList empty
235
236 phyloParam :: PhyloParam
237 phyloParam = (initPhyloParam (Just defaultPhyloVersion) (Just defaultSoftware) (Just phyloQueryBuild))
238
239 docs :: [Document]
240 docs = parseDocs foundationsRoots corpus
241
242 foundationsRoots :: Vector Ngrams
243 foundationsRoots = initFoundationsRoots (termListToNgrams termList)
244
245
246 --------------------------------------------
247 -- | STEP 0 | -- Let's start with an example
248 --------------------------------------------
249
250 termList :: TermList
251 termList = map (\a -> ([toLower a],[])) actants
252
253 actants :: [Ngrams]
254 actants = [ "Cleopatre" , "Ptolemee", "Ptolemee-XIII", "Ptolemee-XIV"
255 , "Marc-Antoine", "Cesar" , "Antoine" , "Octave" , "Rome"
256 , "Alexandrie" , "Auguste" , "Pompee" , "Cassius" , "Brutus"]
257
258 corpus :: [(Date, Text)]
259 corpus = List.sortOn fst [ (-51,"Cleopatre règne sur l’egypte entre 51 et 30 av. J.-C. avec ses frères-epoux Ptolemee-XIII et Ptolemee-XIV, puis aux côtes du general romain Marc-Antoine. Elle est celèbre pour avoir ete la compagne de Jules Cesar puis d'Antoine, avec lesquels elle a eu plusieurs enfants. Partie prenante dans la guerre civile opposant Antoine à Octave, elle est vaincue à la bataille d'Actium en 31 av. J.-C. Sa defaite va permettre aux Romains de mener à bien la conquête de l’egypte, evenement qui marquera la fin de l'epoque hellenistique."), (-40,"Il existe relativement peu d'informations sur son sejour à Rome, au lendemain de l'assassinat de Cesar, ou sur la periode passee à Alexandrie durant l'absence d'Antoine, entre -40 et -37."), (-48,"L'historiographie antique lui est globalement defavorable car inspiree par son vainqueur, l'empereur Auguste, et par son entourage, dont l'interêt est de la noircir, afin d'en faire l'adversaire malfaisant de Rome et le mauvais genie d'Antoine. On observe par ailleurs que Cesar ne fait aucune mention de sa liaison avec elle dans les Commentaires sur la Guerre civile"), (-69,"Cleopatre est nee au cours de l'hiver -69/-686 probablement à Alexandrie."), (-48,"Pompee a en effet ete le protecteur de Ptolemee XII, le père de Cleopatre et de Ptolemee-XIII dont il se considère comme le tuteur."), (-48,"Ptolemee-XIII et Cleopatre auraient d'ailleurs aide Pompee par l'envoi d'une flotte de soixante navires."), (-48,"Mais le jeune roi Ptolemee-XIII et ses conseillers jugent sa cause perdue et pensent s'attirer les bonnes graces du vainqueur en le faisant assassiner à peine a-t-il pose le pied sur le sol egyptien, près de Peluse, le 30 juillet 48 av. J.-C., sous les yeux de son entourage."), (-48,"Cesar fait enterrer la tête de Pompee dans le bosquet de Nemesis en bordure du mur est de l'enceinte d'Alexandrie. Pour autant la mort de Pompee est une aubaine pour Cesar qui tente par ailleurs de profiter des querelles dynastiques pour annexer l’egypte."), (-48,"Il est difficile de se prononcer clairement sur les raisons qui ont pousse Cesar à s'attarder à Alexandrie. Il y a des raisons politiques, mais aussi des raisons plus sentimentales (Cleopatre ?). Il tente d'abord d'obtenir le remboursement de dettes que Ptolemee XII"), (-46,"Les deux souverains sont convoques par Cesar au palais royal d'Alexandrie. Ptolemee-XIII s'y rend après diverses tergiversations ainsi que Cleopatre."), (-47,"A Rome, Cleopatre epouse alors un autre de ses frères cadets, à Alexandrie, Ptolemee-XIV, sur l'injonction de Jules Cesar"), (-46,"Cesar a-t-il comme objectif de montrer ce qu'il en coûte de se revolter contre Rome en faisant figurer dans son triomphe la sœur de Cleopatre et de Ptolemee-XIV, Arsinoe, qui s'est fait reconnaître reine par les troupes de Ptolemee-XIII ?"), (-44,"Au debut de l'annee -44, Cesar est assassine par Brutus. Profitant de la situation confuse qui s'ensuit, Cleopatre quitte alors Rome à la mi-avril, faisant escale en Grèce. Elle parvient à Alexandrie en juillet -44."), (-44,"La guerre que se livrent les assassins de Cesar, Cassius et Brutus et ses heritiers, Octave et Marc-Antoine, oblige Cleopatre à des contorsions diplomatiques."), (-41,"Nous ignorons depuis quand Cleopatre, agee de 29 ans en -41, et Marc-Antoine, qui a une quarantaine d'annees, se connaissent. Marc-Antoine est l'un des officiers qui ont participe au retablissement de Ptolemee XII. Il est plus vraisemblable qu'ils se soient frequentes lors du sejour à Rome de Cleopatre."), (-42,"Brutus tient la Grèce tandis que Cassius s'installe en Syrie. Le gouverneur de Cleopatre à Chypre, Serapion, vient en aide à Cassius."), (-42,"Cassius aurait envisage de s'emparer d'Alexandrie quand le 'debarquement' en Grèce d'Antoine et d'Octave l'oblige à renoncer à ses projets")]