]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Phylo/Example.hs
add the phylopeaks and correct some bugs on the phylodocs
[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 Control.Lens hiding (makeLenses, both, Level)
32
33 import Data.Bool (Bool, not)
34 import Data.List ((\\), notElem, concat, union, intersect, tails, tail, head, last, null, zip, sort, length, any, (++), (!!), nub, sortOn, reverse, splitAt, take, delete, init, groupBy)
35 import Data.Map (Map, elems, insert, member, adjust, singleton, empty, (!), keys, restrictKeys, mapWithKey, filterWithKey, mapKeys, intersectionWith, unionWith)
36 import Data.Maybe (mapMaybe,isJust,fromJust, isNothing)
37 import Data.Semigroup (Semigroup)
38 import Data.Set (Set)
39 import Data.Text (Text, unwords, toLower, words)
40 import Data.Tuple (fst, snd)
41 import Data.Tuple.Extra
42 import Data.Vector (Vector, fromList, elemIndex, (!))
43
44 import Debug.Trace (trace)
45
46 import Gargantext.Prelude hiding (head)
47 import Gargantext.Text.Terms.Mono (monoTexts)
48
49 import Gargantext.Viz.Phylo
50 import Gargantext.Viz.Phylo.Aggregates.Cluster
51 import Gargantext.Viz.Phylo.Aggregates.Cooc
52 import Gargantext.Viz.Phylo.Aggregates.Document
53 import Gargantext.Viz.Phylo.Aggregates.Fis
54 import Gargantext.Viz.Phylo.BranchMaker
55 import Gargantext.Viz.Phylo.LevelMaker
56 import Gargantext.Viz.Phylo.LinkMaker
57 import Gargantext.Viz.Phylo.Metrics.Proximity
58 import Gargantext.Viz.Phylo.Metrics.Clustering
59 import Gargantext.Viz.Phylo.Tools
60 import Gargantext.Viz.Phylo.View.Display
61 import Gargantext.Viz.Phylo.View.Filters
62 import Gargantext.Viz.Phylo.View.Metrics
63 import Gargantext.Viz.Phylo.View.Sort
64 import Gargantext.Viz.Phylo.View.Taggers
65 import Gargantext.Viz.Phylo.View.ViewMaker
66
67
68 import qualified Data.Bool as Bool
69 import qualified Data.List as List
70 import qualified Data.Map as Map
71 import qualified Data.Maybe as Maybe
72 import qualified Data.Set as Set
73 import qualified Data.Tuple as Tuple
74 import qualified Data.Vector as Vector
75
76
77 ------------------------------------------------------
78 -- | STEP 12 | -- Create a PhyloView from a user Query
79 ------------------------------------------------------
80
81
82 phyloView :: PhyloView
83 phyloView = toPhyloView (queryParser' queryViewEx) phyloFromQuery
84
85 -- | To do : create an other request handler and an other query parser
86 queryParser' :: [Char] -> PhyloQueryView
87 queryParser' q = phyloQueryView
88
89 queryViewEx :: [Char]
90 queryViewEx = "level=3"
91 ++ "&childs=false"
92 ++ "&filter=LonelyBranchFilter"
93 ++ "&metric=BranchAge"
94 ++ "&tagger=BranchLabelFreq"
95 ++ "&tagger=GroupLabelCooc"
96
97
98 phyloQueryView :: PhyloQueryView
99 phyloQueryView = PhyloQueryView 3 Merge False 1 [BranchAge] [defaultSmallBranch] [BranchLabelFreq,GroupLabelCooc] (Just (ByBranchAge,Asc)) Flat True
100
101
102 --------------------------------------------------
103 -- | STEP 11 | -- Create a Phylo from a user Query
104 --------------------------------------------------
105
106
107 phyloFromQuery :: Phylo
108 phyloFromQuery = toPhylo (queryParser queryEx) corpus actants actantsTrees
109
110 -- | To do : create a request handler and a query parser
111 queryParser :: [Char] -> PhyloQuery
112 queryParser q = phyloQuery
113
114 queryEx :: [Char]
115 queryEx = "title=Cesar et Cleôpatre"
116 ++ "&desc=An example of Phylomemy (french without accent)"
117 ++ "grain=5&steps=3"
118 ++ "cluster=FrequentItemSet"
119 ++ "interTemporalMatching=WeightedLogJaccard"
120 ++ "nthLevel=2"
121 ++ "nthCluster=RelatedComponents"
122 ++ "nthProximity=Filiation"
123
124 phyloQuery :: PhyloQuery
125 phyloQuery = PhyloQuery "Cesar et Cleôpatre" "An example of Phylomemy (french without accent)"
126 5 3 defaultFis defaultWeightedLogJaccard 3 defaultRelatedComponents
127
128
129
130 ----------------------------------------------------------------------------------------------------------------------------
131 -- | STEP 10 | -- Incrementaly cluster the PhyloGroups n times, link them through the Periods and build level n of the Phylo
132 ----------------------------------------------------------------------------------------------------------------------------
133
134
135 phylo6 :: Phylo
136 phylo6 = toNthLevel 6 defaultWeightedLogJaccard defaultRelatedComponents phylo3
137
138
139 phylo3 :: Phylo
140 phylo3 = setPhyloBranches 3
141 $ interTempoMatching Descendant 3 defaultWeightedLogJaccard
142 $ interTempoMatching Ascendant 3 defaultWeightedLogJaccard
143 $ setLevelLinks (2,3)
144 $ addPhyloLevel 3
145 (phyloToClusters 2 defaultWeightedLogJaccard defaultRelatedComponents phyloBranch2)
146 phyloBranch2
147
148
149 --------------------------------
150 -- | STEP 9 | -- Cluster the Fis
151 --------------------------------
152
153
154 phyloBranch2 :: Phylo
155 phyloBranch2 = setPhyloBranches 2 phylo2_c
156
157
158 phylo2_c :: Phylo
159 phylo2_c = interTempoMatching Descendant 2 defaultWeightedLogJaccard phylo2_p
160
161
162 phylo2_p :: Phylo
163 phylo2_p = interTempoMatching Ascendant 2 defaultWeightedLogJaccard phylo2_1_2
164
165
166 phylo2_1_2 :: Phylo
167 phylo2_1_2 = setLevelLinks (1,2) phylo2
168
169
170 -- | phylo2 allready contains the LevelChilds links from 2 to 1
171 phylo2 :: Phylo
172 phylo2 = addPhyloLevel 2 phyloCluster phyloBranch1
173
174
175 phyloCluster :: Map (Date,Date) [PhyloCluster]
176 phyloCluster = phyloToClusters 1 defaultWeightedLogJaccard defaultRelatedComponents phyloBranch1
177
178
179 ----------------------------------
180 -- | STEP 8 | -- Find the Branches
181 ----------------------------------
182
183
184 phyloBranch1 :: Phylo
185 phyloBranch1 = setPhyloBranches 1 phylo1_c
186
187
188 --------------------------------------------------------------------
189 -- | STEP 7 | -- Link the PhyloGroups of level 1 through the Periods
190 --------------------------------------------------------------------
191
192
193 phylo1_c :: Phylo
194 phylo1_c = interTempoMatching Descendant 1 defaultWeightedLogJaccard phylo1_p
195
196
197 phylo1_p :: Phylo
198 phylo1_p = interTempoMatching Ascendant 1 defaultWeightedLogJaccard phylo1_0_1
199
200
201 -----------------------------------------------
202 -- | STEP 6 | -- Build the level 1 of the Phylo
203 -----------------------------------------------
204
205
206 phylo1_0_1 :: Phylo
207 phylo1_0_1 = setLevelLinks (0,1) phylo1_1_0
208
209
210 phylo1_1_0 :: Phylo
211 phylo1_1_0 = setLevelLinks (1,0) phylo1
212
213
214 phylo1 :: Phylo
215 phylo1 = addPhyloLevel (1) phyloFis phylo
216
217
218 -------------------------------------------------------------------
219 -- | STEP 5 | -- Create lists of Frequent Items Set and filter them
220 -------------------------------------------------------------------
221
222
223 phyloFis :: Map (Date, Date) [PhyloFis]
224 phyloFis = filterFisBySupport False 1 (filterFisByNested (docsToFis phyloDocs))
225
226
227 ----------------------------------------
228 -- | STEP 2 | -- Init a Phylo of level 0
229 ----------------------------------------
230
231
232 phylo :: Phylo
233 phylo = addPhyloLevel 0 phyloDocs phyloBase
234
235
236 phyloDocs :: Map (Date, Date) [Document]
237 phyloDocs = corpusToDocs corpus phyloBase
238
239
240 ------------------------------------------------------------------------
241 -- | STEP 1 | -- Init the Base of the Phylo from Periods and Foundations
242 ------------------------------------------------------------------------
243
244
245 phyloBase :: Phylo
246 phyloBase = initPhyloBase periods foundations peaks defaultPhyloParam
247
248
249 periods :: [(Date,Date)]
250 periods = initPeriods 5 3
251 $ both fst (head corpus,last corpus)
252
253
254 peaks :: PhyloPeaks
255 peaks = initPeaks (map (\t -> alterLabels phyloAnalyzer t) actantsTrees) foundations
256
257
258 foundations :: Vector Ngrams
259 foundations = initFoundations actants
260
261
262 --------------------------------------------
263 -- | STEP 0 | -- Let's start with an example
264 --------------------------------------------
265
266 actantsTrees :: [Tree Ngrams]
267 actantsTrees = [Node "Cite antique" [(Node "Rome" []),(Node "Alexandrie" [])]]
268
269
270 actants :: [Ngrams]
271 actants = [ "Cleopatre" , "Ptolemee", "Ptolemee-XIII", "Ptolemee-XIV"
272 , "Marc-Antoine", "Cesar" , "Antoine" , "Octave" , "Rome"
273 , "Alexandrie" , "Auguste" , "Pompee" , "Cassius" , "Brutus"]
274
275
276 corpus :: [(Date, Text)]
277 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")]