Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 428 | Rev 435 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
424 jpm 1
<?php
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
3
class Description {
4
 
428 jpm 5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
6
	const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
8
	const DOSSIER_LOG = 'log/';
424 jpm 9
 
428 jpm 10
	private $dossierBase = '';
424 jpm 11
	private $conteneur = null;
12
	private $outils = null;
13
	private $messages = null;
428 jpm 14
	private $action = '';
15
	private $nomFichier = '';
16
	private $nomDossier = '';
17
	private $listeFichiers = array();
424 jpm 18
	private $fichierNum = '';
428 jpm 19
	private $fichier = '';
20
	private $log = '';
21
	private $correspondance = array();
22
	private $infosCorrespondanceBase = array(
23
		'nom_sci' => '',
24
		'rang' => 290,
25
		'auteur' => '',
430 jpm 26
		'annee' => '',
27
		'biblio_coste' => '',
428 jpm 28
		'nom_francais' => '',
29
		'nom_coste' => '',
430 jpm 30
		'auteur_coste' => '',
428 jpm 31
		'num_nom_coste' => '',
430 jpm 32
		'num_nom_retenu_coste' => '',
428 jpm 33
		'num_tax_sup_coste' => '',
34
		'tome' => '',
35
		'page' => '',
430 jpm 36
		'synonymie_coste' => '',
428 jpm 37
		'bdnff_nn' => '',
38
		'bdnff_nt' => '');
424 jpm 39
 
40
	public function __construct(Conteneur $conteneur) {
41
		$this->conteneur = $conteneur;
42
		$this->outils = $conteneur->getOutils();
43
		$this->messages = $conteneur->getMessages();
428 jpm 44
		$this->dossierBase = dirname(__FILE__).'/';
424 jpm 45
	}
46
 
47
	public function genererDescriptionTxt() {
428 jpm 48
		$this->chargerFichiers();
49
 
50
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
51
			$this->messages->afficherAvancement("Création des descriptions au format txt");
52
			$this->genererFichier();
53
		}
54
		echo "\n";
424 jpm 55
	}
56
 
428 jpm 57
	public function verifierDescriptionTxt() {
58
		$this->chargerFichiers();
59
 
60
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
61
			$this->messages->afficherAvancement("Analyse des descriptions");
62
			$this->verifierFichier();
63
		}
64
		echo "\n";
65
 
66
		$fichierTxt = $this->dossierBase.self::DOSSIER_LOG.'analyse.log';
67
		file_put_contents($fichierTxt, $this->log);
68
	}
69
 
70
	public function genererCorrespondance() {
71
		$this->chargerFichiers();
72
 
73
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
74
			$this->messages->afficherAvancement("Création du fichier de correspondance");
75
			$this->extraireInfosCorrespondance();
76
		}
77
		echo "\n";
78
 
79
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
80
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
430 jpm 81
		file_put_contents($fichierTxt, $txtCorrespondance);
82
 
83
		$fichierLog = $this->dossierBase.self::DOSSIER_LOG.'synonymes.log';
84
		file_put_contents($fichierLog, $this->log);
428 jpm 85
	}
86
 
87
	private function chargerFichiers() {
88
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
89
		if (file_exists($this->nomDossier) === true) {
90
			if (is_dir($this->nomDossier)) {
91
				if ($dossierOuvert = opendir($this->nomDossier)) {
92
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
93
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
94
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
424 jpm 95
						}
96
					}
97
					closedir($dossierOuvert);
98
				} else {
428 jpm 99
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
424 jpm 100
				}
101
			} else {
428 jpm 102
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
424 jpm 103
			}
104
		} else {
428 jpm 105
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
424 jpm 106
		}
428 jpm 107
 
430 jpm 108
		arsort($this->listeFichiers);
424 jpm 109
	}
110
 
428 jpm 111
	private function verifierFichier() {
112
		$donnees = $this->analyserFichier();
113
		$txt = $donnees['texte'];
114
		$this->verifierOuvertureFermetureBalise($txt);
115
		$this->verifierAbscenceEgale($txt);
116
		$this->verifierNumero($txt);
430 jpm 117
		// TODO : vérifier les noms vernauclaire qui sont abrégés
428 jpm 118
	}
119
 
120
	private function verifierOuvertureFermetureBalise($txt) {
121
		$b_ouvert = substr_count($txt, '<B>');
122
		$b_ferme = substr_count($txt, '</B>');
123
		if ($b_ouvert != $b_ferme) {
124
			$this->log .= "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées\n";
125
		}
126
		$i_ouvert = substr_count($txt, '<I>');
127
		$i_ferme = substr_count($txt, '</I>');
128
		if ($i_ouvert != $i_ferme) {
129
			$this->log .= "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées\n";
130
		}
131
	}
132
 
133
	private function verifierAbscenceEgale($txt) {
134
		if (strripos($txt, '=') === false) {
135
			$this->log .= "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)\n";
136
		}
137
	}
138
 
139
	private function verifierNumero($txt) {
140
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
141
			$this->log .= "{$this->fichierNum} ne contient pas un numéro bien formaté\n";
142
		}
143
	}
144
 
145
	private function extraireInfosCorrespondance() {
146
		$donnees = $this->analyserFichier();
147
		$infos = $this->infosCorrespondanceBase;
148
 
149
		$titre = $donnees['titre'];
430 jpm 150
		if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
428 jpm 151
			$infos['num_nom_coste'] = $match[1];
152
			$infos['nom_sci'] = $match[2];
153
			$infos['num_tax_sup_coste'] = $match[3];
154
			$infos['tome'] = $match[4];
155
			$infos['page'] = $match[5];
156
		} else {
157
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
158
		}
159
 
160
		$corres = $donnees['correspondance'];
430 jpm 161
		if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
428 jpm 162
			$infos['bdnff_nn'] = $match[1];
163
			$infos['bdnff_nt'] = $match[2];
164
		} else {
165
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
166
		}
167
 
168
		$txt = $donnees['texte'];
169
		$txt = $this->corrigerDescription($txt);
430 jpm 170
		if (preg_match('/^<B>[0-9]{1,4}[.] – ([^<]+)<\/B> ([^–]*)– (?:<I>([^<]+)<\/I>[.] – |<I>([^<]+)<\/I>( \([^)]+\))[.] – |[A-Z]|<I>(?:Sous-|Espèce|Turion|Souche|Plante|Feuille|Racine))/u', $txt, $match)) {
171
			$infos['nom_coste'] = trim($match[1]);
172
			$infos['auteur_coste'] = trim($match[2]);
173
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
174
			$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
175
			if (strpos($infos['auteur_coste'], '(' ) !== false) {
176
				if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
177
					$infos['auteur_coste'] = trim($match[1]);
178
					$infos['auteur'] = $this->traiterAuteur($infos['auteur_coste']);
179
					$parentheseContenu = trim($match[2]);
180
					if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
181
						$infos['annee'] = $parentheseContenu;
182
					} else {
183
						$infos['synonymie_coste'] = $parentheseContenu;
184
						$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
185
					}
186
				} else {
187
					$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
188
				}
189
			}
428 jpm 190
		} else {
191
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
192
		}
193
 
194
		$this->correspondance[] = $infos;
430 jpm 195
 
196
		if ($infos['synonymie_coste'] != '') {
197
			$this->traiterSynonymie($infos);
198
		}
428 jpm 199
	}
200
 
430 jpm 201
	private function traiterAuteur($auteurCoste) {
202
		$auteur = '';
203
		if ($auteurCoste != '') {
204
			$auteur = str_replace('et', '&', $auteurCoste);
205
		}
206
		return $auteur;
207
	}
208
 
209
	private function traiterSynonymie($infos) {
210
		$synoCoste = $infos['synonymie_coste'];
211
		$synoCoste = preg_replace('/^et /', '', $synoCoste);
212
		$synoCoste = preg_replace('/ et ([A-Z]{2,}|[A-Z][.] [A-Z]{2,})/', ' ; $1', $synoCoste);
213
		$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.])[^,]+))/', ' ;$1', $synoCoste);
214
 
215
		$synonymes = explode(';', $synoCoste);
216
 
217
		foreach ($synonymes as $syno) {
218
			$syno = trim($syno);
219
			$syno = trim($syno, ' ');
220
			$infosSyno = $this->infosCorrespondanceBase;
221
			$this->log .= $infos['num_nom_coste']."\t$syno\n";
222
			$this->correspondance[] = $infosSyno;
223
		}
224
	}
225
 
428 jpm 226
	private function creerFichierCsvCorrespondance() {
227
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
228
		foreach ($this->correspondance as $infos) {
229
			$lignes[] = implode("\t", $infos);
230
		}
231
 
232
		$txt = '';
233
		$txt = implode("\n", $lignes);
234
		return $txt;
235
	}
236
 
237
	private function genererFichier() {
238
		$donnees = $this->analyserFichier();
239
 
240
		$txt = $this->nettoyerDescription($donnees['texte']);
241
		$txt = $this->corrigerDescription($txt);
242
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
243
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
244
		file_put_contents($fichierTxt, $txt);
245
 
246
		unset($donnees['texte']);
247
		$txt = implode("\n", $donnees);
248
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
424 jpm 249
		file_put_contents($fichierTxt, $txt);
250
	}
251
 
428 jpm 252
	private function analyserFichier() {
253
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
254
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
424 jpm 255
			$i = 1;
256
			while ($ligne = fgets($fichierOuvert)) {
257
				if ($i == 24) {
428 jpm 258
					$donnees['titre'] = $this->supprimerHtml($ligne);
430 jpm 259
				} elseif ($i >= 45 && $i <= 60) {
428 jpm 260
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
430 jpm 261
				} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
428 jpm 262
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
263
				}
424 jpm 264
				$i++;
265
			}
266
			fclose($fichierOuvert);
267
		} else {
428 jpm 268
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
424 jpm 269
		}
270
		return $donnees;
271
	}
272
 
428 jpm 273
	private function supprimerHtml($txt) {
274
		$txt = strip_tags($txt,'<b>,<i>');
275
		$txt = str_replace('&#173;', '', $txt);
276
		$txt = str_ireplace('&nbsp;', ' ', $txt);
277
		$txt = trim($txt);
278
		$txt = preg_replace('/^<\/I>/', '', $txt);
279
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
280
		return $txt;
424 jpm 281
	}
282
 
428 jpm 283
	private function traiterLigneDescription($txt) {
284
		$txt = $this->supprimerHtml($txt);
285
		$txt = $txt."\n";
286
		return $txt;
287
	}
288
 
289
	private function nettoyerDescription($txt) {
290
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
424 jpm 291
		$txt = trim($txt);
292
		return $txt;
293
	}
428 jpm 294
 
295
	private function corrigerDescription($txt) {
296
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
297
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
298
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
299
		return $txt;
300
	}
301
 
302
	private function remplacerHtmlParSyntaxeWiki($txt) {
303
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
304
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
305
		return $txt;
306
	}
424 jpm 307
}
308
?>