Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 436 | Rev 459 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 436 Rev 437
1
<?php
1
<?php
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
3
class Description {
3
class Description {
4
 
4
 
5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
6
	const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
6
	const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
8
	const DOSSIER_LOG = 'log/';
8
	const DOSSIER_LOG = 'log/';
9
 
9
 
10
	private $atueurs = array();
10
	private $atueurs = array();
11
	private $dossierBase = '';
11
	private $dossierBase = '';
12
	private $conteneur = null;
12
	private $conteneur = null;
13
	private $outils = null;
13
	private $outils = null;
14
	private $messages = null;
14
	private $messages = null;
15
	private $action = '';
15
	private $action = '';
16
	private $nomFichier = '';
16
	private $nomFichier = '';
17
	private $nomDossier = '';
17
	private $nomDossier = '';
18
	private $listeFichiers = array();
18
	private $listeFichiers = array();
19
	private $fichierNum = '';
19
	private $fichierNum = '';
20
	private $fichier = '';
20
	private $fichier = '';
21
	private $log = '';
21
	private $log = '';
22
	private $correspondance = array();
22
	private $correspondance = array();
23
	private $infosCorrespondanceBase = array(
23
	private $infosCorrespondanceBase = array(
24
		'nom_sci' => '',
24
		'nom_sci' => '',
25
		'auteur' => '',
25
		'auteur' => '',
26
		'nom_addendum' => '',
26
		'nom_addendum' => '',
27
		'annee' => '',
27
		'annee' => '',
28
		'biblio_coste' => '',
28
		'biblio_origine' => '',
29
		'rang' => 290,
29
		'rang' => 290,
30
		'nom_francais' => '',
30
		'nom_francais' => '',
31
		'nom_coste' => '',
31
		'nom_coste' => '',
32
		'auteur_coste' => '',
32
		'auteur_coste' => '',
-
 
33
		'biblio_coste' => '',
33
		'num_nom_coste' => '',
34
		'num_nom_coste' => '',
34
		'num_nom_retenu_coste' => '',
35
		'num_nom_retenu_coste' => '',
35
		'num_tax_sup_coste' => '',
36
		'num_tax_sup_coste' => '',
36
		'tome' => '',
37
		'tome' => '',
37
		'page' => '',
38
		'page' => '',
38
		'synonymie_coste' => '',
39
		'synonymie_coste' => '',
39
		'bdnff_nn' => '',
40
		'bdnff_nn' => '',
40
		'bdnff_nt' => '');
41
		'bdnff_nt' => '');
41
 
42
 
42
	public function __construct(Conteneur $conteneur) {
43
	public function __construct(Conteneur $conteneur) {
-
 
44
		mb_internal_encoding('UTF-8');
-
 
45
		setlocale(LC_ALL, 'fr_FR.UTF-8');
43
		$this->conteneur = $conteneur;
46
		$this->conteneur = $conteneur;
44
		$this->outils = $conteneur->getOutils();
47
		$this->outils = $conteneur->getOutils();
45
		$this->messages = $conteneur->getMessages();
48
		$this->messages = $conteneur->getMessages();
46
		$this->dossierBase = dirname(__FILE__).'/';
49
		$this->dossierBase = dirname(__FILE__).'/';
47
	}
50
	}
48
 
51
 
49
	public function genererDescriptionTxt() {
52
	public function genererDescriptionTxt() {
50
		$this->chargerFichiers();
53
		$this->chargerFichiers();
51
 
54
 
52
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
55
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
53
			$this->messages->afficherAvancement("Création des descriptions au format txt");
56
			$this->messages->afficherAvancement("Création des descriptions au format txt");
54
			$this->genererFichier();
57
			$this->genererFichier();
55
		}
58
		}
56
		echo "\n";
59
		echo "\n";
57
	}
60
	}
58
 
61
 
59
	public function verifierDescriptionTxt() {
62
	public function verifierDescriptionTxt() {
60
		$this->chargerFichiers();
63
		$this->chargerFichiers();
61
 
64
 
62
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
65
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
63
			$this->messages->afficherAvancement("Analyse des descriptions");
66
			$this->messages->afficherAvancement("Analyse des descriptions");
64
			$this->verifierFichier();
67
			$this->verifierFichier();
65
		}
68
		}
66
		echo "\n";
69
		echo "\n";
67
		$this->ecrireLogs();
70
		$this->ecrireLogs();
68
	}
71
	}
69
 
72
 
70
	public function genererCorrespondance() {
73
	public function genererCorrespondance() {
71
		$this->chargerFichiers();
74
		$this->chargerFichiers();
72
 
75
 
73
		$this->ajouterLogSyno(implode("\t", array_keys($this->infosCorrespondanceBase)));
76
		$this->ajouterLogSyno(implode("\t", array_keys($this->infosCorrespondanceBase)));
74
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
77
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
75
			$this->messages->afficherAvancement("Création du fichier de correspondance");
78
			$this->messages->afficherAvancement("Création du fichier de correspondance");
76
			$this->extraireInfosCorrespondance();
79
			$this->extraireInfosCorrespondance();
77
		}
80
		}
78
		echo "\n";
81
		echo "\n";
79
 
82
 
80
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
83
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
81
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
84
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
82
		file_put_contents($fichierTxt, $txtCorrespondance);
85
		file_put_contents($fichierTxt, $txtCorrespondance);
-
 
86
 
-
 
87
		ksort($this->auteurs);
-
 
88
		foreach ($this->auteurs as $auteur => $nbre) {
-
 
89
			$this->ajouterLogAuteur("$auteur\t$nbre");
-
 
90
		}
83
 
91
 
84
		$this->ecrireLogs();
92
		$this->ecrireLogs();
85
	}
93
	}
86
 
94
 
87
	private function chargerFichiers() {
95
	private function chargerFichiers() {
88
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
96
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
89
		if (file_exists($this->nomDossier) === true) {
97
		if (file_exists($this->nomDossier) === true) {
90
			if (is_dir($this->nomDossier)) {
98
			if (is_dir($this->nomDossier)) {
91
				if ($dossierOuvert = opendir($this->nomDossier)) {
99
				if ($dossierOuvert = opendir($this->nomDossier)) {
92
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
100
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
93
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
101
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
94
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
102
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
95
						}
103
						}
96
					}
104
					}
97
					closedir($dossierOuvert);
105
					closedir($dossierOuvert);
98
				} else {
106
				} else {
99
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
107
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
100
				}
108
				}
101
			} else {
109
			} else {
102
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
110
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
103
			}
111
			}
104
		} else {
112
		} else {
105
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
113
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
106
		}
114
		}
107
 
115
 
108
		arsort($this->listeFichiers);
116
		arsort($this->listeFichiers);
109
	}
117
	}
110
 
118
 
111
	private function verifierFichier() {
119
	private function verifierFichier() {
112
		$donnees = $this->analyserFichier();
120
		$donnees = $this->analyserFichier();
113
		$txt = $donnees['texte'];
121
		$txt = $donnees['texte'];
114
		$this->verifierOuvertureFermetureBalise($txt);
122
		$this->verifierOuvertureFermetureBalise($txt);
115
		$this->verifierAbscenceEgale($txt);
123
		$this->verifierAbscenceEgale($txt);
116
		$this->verifierNumero($txt);
124
		$this->verifierNumero($txt);
117
		// TODO : vérifier les noms vernauclaire qui sont abrégés
125
		// TODO : vérifier les noms vernauclaire qui sont abrégés
118
	}
126
	}
119
 
127
 
120
	private function verifierOuvertureFermetureBalise($txt) {
128
	private function verifierOuvertureFermetureBalise($txt) {
121
		$b_ouvert = substr_count($txt, '<B>');
129
		$b_ouvert = substr_count($txt, '<B>');
122
		$b_ferme = substr_count($txt, '</B>');
130
		$b_ferme = substr_count($txt, '</B>');
123
		if ($b_ouvert != $b_ferme) {
131
		if ($b_ouvert != $b_ferme) {
124
			$message = "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées";
132
			$message = "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées";
125
			$this->ajouterLogAnalyse($message);
133
			$this->ajouterLogAnalyse($message);
126
		}
134
		}
127
		$i_ouvert = substr_count($txt, '<I>');
135
		$i_ouvert = substr_count($txt, '<I>');
128
		$i_ferme = substr_count($txt, '</I>');
136
		$i_ferme = substr_count($txt, '</I>');
129
		if ($i_ouvert != $i_ferme) {
137
		if ($i_ouvert != $i_ferme) {
130
			$message = "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées";
138
			$message = "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées";
131
			$this->ajouterLogAnalyse($message);
139
			$this->ajouterLogAnalyse($message);
132
		}
140
		}
133
	}
141
	}
134
 
142
 
135
	private function verifierAbscenceEgale($txt) {
143
	private function verifierAbscenceEgale($txt) {
136
		if (strripos($txt, '=') === false) {
144
		if (strripos($txt, '=') === false) {
137
			$message = "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)";
145
			$message = "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)";
138
			$this->ajouterLogAnalyse($message);
146
			$this->ajouterLogAnalyse($message);
139
		}
147
		}
140
	}
148
	}
141
 
149
 
142
	private function verifierNumero($txt) {
150
	private function verifierNumero($txt) {
143
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
151
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
144
			$message = "{$this->fichierNum} ne contient pas un numéro bien formaté";
152
			$message = "{$this->fichierNum} ne contient pas un numéro bien formaté";
145
			$this->ajouterLogAnalyse($message);
153
			$this->ajouterLogAnalyse($message);
146
		}
154
		}
147
	}
155
	}
148
 
156
 
149
	private function extraireInfosCorrespondance() {
157
	private function extraireInfosCorrespondance() {
150
		$donnees = $this->analyserFichier();
158
		$donnees = $this->analyserFichier();
151
		$infos = $this->infosCorrespondanceBase;
159
		$infos = $this->infosCorrespondanceBase;
152
 
160
 
153
		$titre = $donnees['titre'];
161
		$titre = $donnees['titre'];
154
		if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
162
		if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
155
			$infos['num_nom_coste'] = $match[1];
163
			$infos['num_nom_coste'] = $match[1];
156
			$infos['nom_sci'] = $match[2];
164
			$infos['nom_sci'] = $match[2];
157
			$infos['num_tax_sup_coste'] = $match[3];
165
			$infos['num_tax_sup_coste'] = $match[3];
158
			$infos['tome'] = $match[4];
166
			$infos['tome'] = $match[4];
159
			$infos['page'] = $match[5];
167
			$infos['page'] = $match[5];
160
		} else {
168
		} else {
161
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
169
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
162
		}
170
		}
163
 
171
 
164
		$corres = $donnees['correspondance'];
172
		$corres = $donnees['correspondance'];
165
		if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
173
		if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
166
			$infos['bdnff_nn'] = $match[1];
174
			$infos['bdnff_nn'] = $match[1];
167
			$infos['bdnff_nt'] = $match[2];
175
			$infos['bdnff_nt'] = $match[2];
168
		} else {
176
		} else {
169
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
177
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
170
		}
178
		}
171
 
179
 
172
		$txt = $donnees['texte'];
180
		$txt = $donnees['texte'];
173
		$txt = $this->corrigerDescription($txt);
181
		$txt = $this->corrigerDescription($txt);
174
		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)) {
182
		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)) {
175
			$infos['nom_coste'] = trim($match[1]);
183
			$infos['nom_coste'] = trim($match[1]);
176
			$infos['auteur_coste'] = trim($match[2]);
184
			$infos['auteur_coste'] = trim($match[2]);
177
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
185
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
178
			$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
186
			$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
179
			if (strpos($infos['auteur_coste'], '(' ) !== false) {
187
			if (strpos($infos['auteur_coste'], '(' ) !== false) {
180
				if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
188
				if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
181
					$infos['auteur_coste'] = trim($match[1]);
189
					$infos['auteur_coste'] = $this->traiterAuteur(trim($match[1]));
182
					$infos['auteur'] = $this->traiterAuteur($infos['auteur_coste']);
190
					$infos['auteur'] = $this->normaliserAuteur($infos['auteur_coste']);
183
					$parentheseContenu = trim($match[2]);
191
					$parentheseContenu = trim($match[2]);
184
					if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
192
					if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
185
						$infos['annee'] = $parentheseContenu;
193
						$infos['annee'] = $parentheseContenu;
186
					} else {
194
					} else {
187
						$infos['synonymie_coste'] = $parentheseContenu;
195
						$infos['synonymie_coste'] = $parentheseContenu;
188
						$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
196
						$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
189
					}
197
					}
190
				} else {
198
				} else {
191
					$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
199
					$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
192
				}
200
				}
193
			}
201
			}
194
		} else {
202
		} else {
195
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
203
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
196
		}
204
		}
197
 
205
 
198
		$this->correspondance[] = $infos;
206
		$this->correspondance[] = $infos;
199
 
207
 
200
		if ($infos['synonymie_coste'] != '') {
208
		if ($infos['synonymie_coste'] != '') {
201
			$this->traiterSynonymie($infos);
209
			$this->traiterSynonymie($infos);
202
		}
210
		}
203
	}
211
	}
204
 
212
 
205
	private function traiterAuteur($auteurCoste) {
213
	private function normaliserAuteur($auteurCoste) {
206
		$auteur = '';
214
		$auteur = '';
207
		if ($auteurCoste != '') {
215
		if ($auteurCoste != '') {
208
			$auteur = str_replace('et', '&', $auteurCoste);
216
			$auteur = str_replace(' et ', ' & ', $auteurCoste);
209
		}
217
		}
210
		return $auteur;
218
		return $auteur;
211
	}
219
	}
212
 
220
 
213
	private function traiterSynonymie($infos) {
221
	private function traiterSynonymie($infos) {
214
		$synoCoste = $infos['synonymie_coste'];
222
		$synoCoste = $infos['synonymie_coste'];
215
		$synoCoste = preg_replace('/^et /', '', $synoCoste);
223
		$synoCoste = preg_replace('/^et /', '', $synoCoste);
-
 
224
		$synoCoste = preg_replace('/^(([A-Z][.]|[A-Z]{3,}) [A-Z]{3,}(?:(?! et ).+)) et ([A-Z]{3,}) ((?![A-Z]{3,}).+)$/', '$1 ; $2 $3 $4', $synoCoste);
216
		$synoCoste = preg_replace('/ et ([A-Z]{2,}|[A-Z][.] [A-Z]{2,})/', ' ; $1', $synoCoste);
225
		$synoCoste = preg_replace('/ et ((?:[A-Z][.]|[A-Z]{3,}) [A-Z]{3,})/', ' ; $1', $synoCoste);
217
		$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.]|part[.]|centr[.])[^,]+))/', ' ;$1', $synoCoste);
226
		$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.]|part[.]|centr[.])[^,]+))/', ' ;$1', $synoCoste);
218
 
227
 
219
		$synonymes = explode(';', $synoCoste);
228
		$synonymes = explode(';', $synoCoste);
220
 
229
 
221
		foreach ($synonymes as $num => $syno) {
230
		foreach ($synonymes as $num => $syno) {
222
			$synoTraite = $this->traiterNomSyno($syno);
231
			$synoTraite = $this->traiterNomSyno($syno);
-
 
232
			$complementNom = $this->obtenirComplementNom($synoTraite);
-
 
233
			$auteurCoste = $this->obtenirAuteur($synoTraite);
-
 
234
			$auteur = $this->normaliserAuteur($auteurCoste);
-
 
235
			$this->ajouterAuteur($auteur);
-
 
236
			$biblioCoste = $this->obtenirBiblio($complementNom);
-
 
237
 
223
			$infosSyno = $this->infosCorrespondanceBase;
238
			$infosSyno = $this->infosCorrespondanceBase;
224
			$infosSyno['rang'] = $this->obtenirRangSyno($syno);
-
 
225
			$infosSyno['nom_sci'] = $this->obtenirNomSci($synoTraite, $infos, $synonymes, $num);
239
			$infosSyno['nom_sci'] = $this->obtenirNomSci($synoTraite, $infos, $synonymes, $num);
226
			$infosSyno['nom_coste'] = $synoTraite;
240
			$infosSyno['auteur'] = $auteur;
227
			$infosSyno['auteur'] =  $this->obtenirAuteur($synoTraite);
241
			$infosSyno['biblio_origine'] = $biblioCoste;
228
			$infosSyno['nom_addendum'] =  $this->obtenirNomAddendum($syno);
242
			$infosSyno['nom_addendum'] =  $this->obtenirNomAddendum($syno);
-
 
243
 
-
 
244
			$infosSyno['rang'] = $this->obtenirRangSyno($syno);
-
 
245
			$infosSyno['nom_coste'] = $synoTraite;
-
 
246
			$infosSyno['auteur_coste'] = $auteurCoste;
-
 
247
			$infosSyno['biblio_coste'] =  $biblioCoste;
229
			$infosSyno['num_nom_coste'] = $infos['num_nom_coste'].'.'.($num + 1);
248
			$infosSyno['num_nom_coste'] = $infos['num_nom_coste'].'.'.($num + 1);
230
			$infosSyno['num_nom_retenu_coste'] = $infos['num_nom_coste'];
249
			$infosSyno['num_nom_retenu_coste'] = $infos['num_nom_coste'];
231
			$this->ajouterLogSyno(implode("\t", $infosSyno));
250
			$this->ajouterLogSyno(implode("\t", $infosSyno));
232
			$this->correspondance[] = $infosSyno;
251
			$this->correspondance[] = $infosSyno;
233
		}
252
		}
234
	}
253
	}
-
 
254
 
-
 
255
	private function traiterNomSyno($syno) {
-
 
256
		$syno = $this->nettoyerEspacesNomSyno($syno);
-
 
257
		$syno = preg_replace('/^(?:avec|(?:compr|incl)[.]) /', '', $syno);
-
 
258
		return $syno;
-
 
259
	}
-
 
260
 
-
 
261
	private function nettoyerEspacesNomSyno($syno) {
-
 
262
		$syno = trim($syno);
-
 
263
		$syno = trim($syno, ' ');
-
 
264
		return $syno;
-
 
265
	}
235
 
266
 
236
	private function obtenirAuteur($syno) {
267
	private function obtenirComplementNom($syno) {
237
		$auteur = '';
268
		$complementNom = '';
-
 
269
		if (preg_match('/^(?:[^ ]+ [^ ]+ (?:var|V)[.] [^ ]+|[^ ]+ [^ ]+) (.+)$/', $syno, $match)) {
-
 
270
			$complementNom = $match[1];
-
 
271
		}
-
 
272
		return $complementNom;
-
 
273
	}
-
 
274
 
-
 
275
	private function obtenirBiblio($complementNom) {
238
		$nomSciRE = '(?:[^ ]+ [^ ]+ var. [^ ]+|[^ ]+ [^ ]+)';
276
		$biblioCoste = '';
239
		if (preg_match("/^$nomSciRE ((?!(?:auct.+|, non.+|p[.] p[.])$).+)$/", $syno, $match)) {
277
		if (preg_match("/ (p[.] [0-9]{1,}|in .+||Fl[.] fr[.]|(?: Sp[.])? ed[.] [1-2])$/", $complementNom, $match)) {
-
 
278
			$biblioCoste = $match[1];
-
 
279
		}
-
 
280
		return $biblioCoste;
-
 
281
	}
-
 
282
 
-
 
283
	private function obtenirAuteur($complementNom) {
240
			$auteur = $match[1];
284
		$auteurCoste = '';
241
			$auteur = preg_replace('/(?: p[.] p[.]|, non .+|(?: Sp[.])? ed[.] [1-2])$/', '', $auteur);
285
		if (preg_match("/^((?!(?:auct.+|(?:, )?(?:non|an) .+|p[.] p[.])$).+)$/", $complementNom, $match)) {
242
			$this->ajouterAuteur($auteur);
286
			$auteurCoste = $this->traiterAuteur($match[1]);
243
		} else {
287
		} else {
244
			$message = "Impossible de récupérer l'auteur pour le synonyme ($syno).";
288
			$message = "Impossible de récupérer l'auteur pour le complément de nom ($complementNom).";
245
			$this->ajouterLogProbleme($message);
289
			$this->ajouterLogProbleme($message);
-
 
290
		}
-
 
291
		return $auteurCoste;
-
 
292
	}
-
 
293
 
-
 
294
	private function traiterAuteur($auteurCoste) {
-
 
295
		$auteur = '';
-
 
296
		if ($auteurCoste != '') {
-
 
297
			$remplacementTxt = array(' p. p.', ' saltem part.', 'Fl. fr.');
-
 
298
			$auteurCoste = str_replace($remplacementTxt, '', $auteurCoste);
-
 
299
			$remplacementsRegExp = array(
-
 
300
				' [0-9]{4}',
-
 
301
				' auct.+',
-
 
302
				',? part[.]',
-
 
303
				' p[.] [0-9]{1,}',
-
 
304
				' in .+|,? (?:non|an) .+',
-
 
305
				'(?: Sp[.])? ed[.] [1-2]');
-
 
306
			$auteur = preg_replace('/(?:'.implode('|', $remplacementsRegExp).')$/', '', $auteurCoste);
246
		}
307
		}
247
		return $auteur;
308
		return $auteur;
248
	}
309
	}
249
 
310
 
250
	private function ajouterAuteur($auteur) {
311
	private function ajouterAuteur($auteur) {
251
		if (!isset($this->auteurs[$auteur])) {
312
		if (!isset($this->auteurs[$auteur])) {
252
			$this->auteurs[$auteur] = 1;
313
			$this->auteurs[$auteur] = 1;
253
			$this->ajouterLogAuteur($auteur);
-
 
254
		} else {
314
		} else {
255
			$this->auteurs[$auteur]++;
315
			$this->auteurs[$auteur]++;
256
		}
316
		}
257
	}
317
	}
258
 
318
 
259
	private function obtenirNomAddendum($syno) {
319
	private function obtenirNomAddendum($syno) {
-
 
320
		$syno = $this->nettoyerEspacesNomSyno($syno);
260
		$nomAddendum = array();
321
		$nomAddendum = array();
261
		if (preg_match('/^((?:compr|incl)[.]) /', $syno, $match)) {
322
		if (preg_match('/^((?:compr|incl)[.]) /', $syno, $match)) {
262
			$nomAddendum[] = '['.$match[1].']';
323
			$nomAddendum[] = '['.$match[1].']';
263
		}
324
		}
264
		if (preg_match('/ (auct[.] .+)$/', $syno, $match)) {
325
		if (preg_match('/ (auct[.] .+)$/', $syno, $match)) {
265
			$nomAddendum[] = '['.$match[1].']';
326
			$nomAddendum[] = '['.$match[1].']';
266
		} else if (preg_match('/, (non .+)$/', $syno, $match)) {
327
		} else if (preg_match('/,? ((?:non|an) .+)$/', $syno, $match)) {
267
			$nomAddendum[] = '['.$match[1].']';
328
			$nomAddendum[] = '['.$match[1].']';
268
		} else if (preg_match('/ (p[.] p[.])$/', $syno, $match)) {
329
		} else if (preg_match('/ (p[.] p[.])$/', $syno, $match)) {
269
			$nomAddendum[] = '['.$match[1].']';
330
			$nomAddendum[] = '['.$match[1].']';
270
		} else if (preg_match('/ (saltem part[.])$/', $syno, $match)) {
331
		} else if (preg_match('/ (saltem part[.])$/', $syno, $match)) {
271
			$nomAddendum[] = '['.$match[1].']';
332
			$nomAddendum[] = '['.$match[1].']';
-
 
333
		} else if (preg_match('/,? (part[.])$/', $syno, $match)) {
-
 
334
			$nomAddendum[] = '['.$match[1].']';
272
		}
335
		}
273
 
336
 
274
		$nomAddendum = implode(' ; ', $nomAddendum);
337
		$nomAddendum = implode(' ; ', $nomAddendum);
275
		return $nomAddendum;
338
		return $nomAddendum;
276
	}
339
	}
277
 
-
 
278
	private function obtenirSynoPrecedent($synonymes, $num) {
-
 
279
		$synoPrecedent = '';
-
 
280
		if (isset($synonymes[($num - 1 )])) {
-
 
281
			$synoPrecedent = $this->traiterNomSyno($synonymes[($num - 1 )]);
-
 
282
		}
-
 
283
		return $synoPrecedent;
-
 
284
	}
-
 
285
 
340
 
286
	private function remplacerAbreviationGenre($syno, $infos, $synonymes, $num) {
341
	private function remplacerAbreviationGenre($syno, $infos, $synonymes, $num) {
287
		$nomSci = $syno;
342
		$nomSci = $syno;
288
 
343
 
289
		if (preg_match('/^(([A-Zƌ])[.]) /', $syno, $matchSyno)) {
344
		if (preg_match('/^(([A-Zƌ])[.]) /', $syno, $matchSyno)) {
290
			if ($matchSyno[2] == substr($infos['nom_coste'], 0, 1)) {
345
			if ($matchSyno[2] == substr($infos['nom_coste'], 0, 1)) {
291
				if (preg_match('/^([^ ]+) /', $infos['nom_coste'], $matchNomRetenu)) {
346
				if (preg_match('/^([^ ]+) /', $infos['nom_coste'], $matchNomRetenu)) {
292
					$nomSci = str_replace($matchSyno[1], $matchNomRetenu[1], $syno);
347
					$nomSci = str_replace($matchSyno[1], $matchNomRetenu[1], $syno);
293
				}
348
				}
294
			} else {
349
			} else {
295
				$synoPrecedent = $this->obtenirSynoAvecGenreComplet($synonymes, $num);
350
				$synoPrecedent = $this->obtenirSynoAvecGenreComplet($synonymes, $num);
296
 
351
 
297
				if ($matchSyno[2] == substr($synoPrecedent, 0, 1)) {
352
				if ($matchSyno[2] == substr($synoPrecedent, 0, 1)) {
298
					if (preg_match('/^([^ ]+) /', $synoPrecedent, $matchSynoPrec)) {
353
					if (preg_match('/^([^ ]+) /', $synoPrecedent, $matchSynoPrec)) {
299
						$nomSci = str_replace($matchSyno[1], $matchSynoPrec[1], $syno);
354
						$nomSci = str_replace($matchSyno[1], $matchSynoPrec[1], $syno);
300
					}
355
					}
301
				} else {
356
				} else {
302
					$message = "L'initiale du synonyme ($syno) ne correspondant pas au nom retenu {$infos['num_nom_coste']} ({$infos['nom_coste']}) ".
357
					$message = "L'initiale du synonyme ($syno) ne correspondant pas au nom retenu {$infos['num_nom_coste']} ({$infos['nom_coste']}) ".
303
							"ni au synonyme précédent ($synoPrecedent).";
358
							"ni au synonyme précédent ($synoPrecedent).";
304
					$this->ajouterLogProbleme($message);
359
					$this->ajouterLogProbleme($message);
305
				}
360
				}
306
			}
361
			}
307
		}
362
		}
308
		return $nomSci;
363
		return $nomSci;
309
	}
364
	}
310
 
365
 
311
	private function obtenirSynoAvecGenreComplet($synonymes, $num) {
366
	private function obtenirSynoAvecGenreComplet($synonymes, $num) {
312
		$synoPrecedent = '';
367
		$synoPrecedent = '';
313
		$synoOk = false;
368
		$synoOk = false;
314
		while ($synoOk == false) {
369
		while ($synoOk == false) {
315
			if ($num < 0) {
370
			if ($num < 0) {
316
				$synoOk = true;
371
				$synoOk = true;
317
			}
372
			}
318
			$synoPrecedent = $this->obtenirSynoPrecedent($synonymes, $num);
373
			$synoPrecedent = $this->obtenirSynoPrecedent($synonymes, $num);
319
			if (preg_match('/^[A-Zƌ][.] /', $synoPrecedent)) {
374
			if (preg_match('/^[A-Zƌ][.] /', $synoPrecedent)) {
320
				$num = $num - 1;
375
				$num = $num - 1;
321
			} else {
376
			} else {
322
				$synoOk = true;
377
				$synoOk = true;
323
			}
378
			}
324
		}
379
		}
325
		return $synoPrecedent;
380
		return $synoPrecedent;
326
	}
381
	}
-
 
382
 
-
 
383
	private function obtenirSynoPrecedent($synonymes, $num) {
-
 
384
		$synoPrecedent = '';
-
 
385
		if (isset($synonymes[($num - 1 )])) {
-
 
386
			$synoPrecedent = $this->traiterNomSyno($synonymes[($num - 1 )]);
-
 
387
		}
-
 
388
		return $synoPrecedent;
-
 
389
	}
-
 
390
 
327
 
391
 
328
	private function obtenirNomSci($syno, $infos, $synonymes, $num) {
392
	private function obtenirNomSci($syno, $infos, $synonymes, $num) {
329
		$nomSci = $this->remplacerAbreviationGenre($syno, $infos, $synonymes, $num);
393
		$nomSci = $this->remplacerAbreviationGenre($syno, $infos, $synonymes, $num);
330
 
394
 
331
		if (preg_match('/^([^ ]+) ([^ ]+) var. ([^ ]+)/', $nomSci, $match)) {
395
		if (preg_match('/^([^ ]+) ([^ ]+) (?:(?:var|V)[.] ([^ ]+)|(STELATUM) )/', $nomSci, $match)) {
332
			$genre = ucfirst(strtolower($match[1]));
396
			$genre = $this->normaliserGenre($match[1]);
333
			$sp = strtolower($match[2]);
397
			$sp = $this->normaliserEpithete($match[2]);
-
 
398
			$infrasp = isset($match[4]) ? $match[4] : $match[3];
334
			$infrasp = strtolower($match[3]);
399
			$infrasp = $this->normaliserEpithete($infrasp);
335
			$nomSci = "$genre $sp var. $infrasp";
400
			$nomSci = "$genre $sp var. $infrasp";
336
		} else if (preg_match('/^([^ ]+) ([^ ]+)(?: |$)/', $nomSci, $match)) {
401
		} else if (preg_match('/^([^ ]+) ([^ ]+)(?: |$)/', $nomSci, $match)) {
337
			$genre = ucfirst(strtolower($match[1]));
402
			$genre = $this->normaliserGenre($match[1]);
338
			$sp = strtolower($match[2]);
403
			$sp = $this->normaliserEpithete($match[2]);
339
			$nomSci = "$genre $sp";
404
			$nomSci = "$genre $sp";
340
		} else {
405
		} else {
341
			$message = "Le synonyme ($nomSci) du nom {$infos['num_nom_coste']} a une structure étrange.";
406
			$message = "Le synonyme ($nomSci) du nom {$infos['num_nom_coste']} a une structure étrange.";
342
			$this->ajouterLogProbleme($message);
407
			$this->ajouterLogProbleme($message);
343
		}
408
		}
344
		return $nomSci;
409
		return $nomSci;
345
	}
410
	}
-
 
411
 
-
 
412
	private function normaliserGenre($genre) {
-
 
413
		$genre = mb_convert_case(mb_strtolower($genre), MB_CASE_TITLE);
-
 
414
		$genre = str_replace('Æ', 'æ', $genre);
-
 
415
		return $genre;
-
 
416
	}
-
 
417
 
-
 
418
	private function normaliserEpithete($sp) {
-
 
419
		$sp = mb_strtolower($sp);
-
 
420
		$sp = str_replace('Æ', 'æ', $sp);
-
 
421
		return $sp;
-
 
422
	}
346
 
423
 
347
	private function obtenirRangSyno($syno) {
424
	private function obtenirRangSyno($syno) {
348
		$rang = 290;
425
		$rang = 290;
349
		if (strpos($syno, ' var. ')) {
426
		if (strpos($syno, ' var. ')) {
350
			$rang = 340;
427
			$rang = 340;
351
		}
428
		}
352
		return $rang;
429
		return $rang;
353
	}
430
	}
354
 
-
 
355
	private function traiterNomSyno($syno) {
-
 
356
		$syno = trim($syno);
-
 
357
		$syno = trim($syno, ' ');
-
 
358
		$syno = preg_replace('/^(?:compr|incl)[.] /', '', $syno);
-
 
359
		return $syno;
-
 
360
	}
-
 
361
 
431
 
362
	private function creerFichierCsvCorrespondance() {
432
	private function creerFichierCsvCorrespondance() {
363
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
433
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
364
		foreach ($this->correspondance as $infos) {
434
		foreach ($this->correspondance as $infos) {
365
			$lignes[] = implode("\t", $infos);
435
			$lignes[] = implode("\t", $infos);
366
		}
436
		}
367
 
437
 
368
		$txt = '';
438
		$txt = '';
369
		$txt = implode("\n", $lignes);
439
		$txt = implode("\n", $lignes);
370
		return $txt;
440
		return $txt;
371
	}
441
	}
372
 
442
 
373
	private function genererFichier() {
443
	private function genererFichier() {
374
		$donnees = $this->analyserFichier();
444
		$donnees = $this->analyserFichier();
375
 
445
 
376
		$txt = $this->nettoyerDescription($donnees['texte']);
446
		$txt = $this->nettoyerDescription($donnees['texte']);
377
		$txt = $this->corrigerDescription($txt);
447
		$txt = $this->corrigerDescription($txt);
378
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
448
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
379
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
449
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
380
		file_put_contents($fichierTxt, $txt);
450
		file_put_contents($fichierTxt, $txt);
381
 
451
 
382
		unset($donnees['texte']);
452
		unset($donnees['texte']);
383
		$txt = implode("\n", $donnees);
453
		$txt = implode("\n", $donnees);
384
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
454
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
385
		file_put_contents($fichierTxt, $txt);
455
		file_put_contents($fichierTxt, $txt);
386
	}
456
	}
387
 
457
 
388
	private function analyserFichier() {
458
	private function analyserFichier() {
389
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
459
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
390
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
460
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
391
			$i = 1;
461
			$i = 1;
392
			while ($ligne = fgets($fichierOuvert)) {
462
			while ($ligne = fgets($fichierOuvert)) {
393
				if ($i == 24) {
463
				if ($i == 24) {
394
					$donnees['titre'] = $this->supprimerHtml($ligne);
464
					$donnees['titre'] = $this->supprimerHtml($ligne);
395
				} elseif ($i >= 45 && $i <= 60) {
465
				} elseif ($i >= 45 && $i <= 60) {
396
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
466
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
397
				} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
467
				} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
398
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
468
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
399
				}
469
				}
400
				$i++;
470
				$i++;
401
			}
471
			}
402
			fclose($fichierOuvert);
472
			fclose($fichierOuvert);
403
		} else {
473
		} else {
404
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
474
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
405
		}
475
		}
406
		return $donnees;
476
		return $donnees;
407
	}
477
	}
408
 
478
 
409
	private function supprimerHtml($txt) {
479
	private function supprimerHtml($txt) {
410
		$txt = strip_tags($txt,'<b>,<i>');
480
		$txt = strip_tags($txt,'<b>,<i>');
411
		$txt = str_replace('&#173;', '', $txt);
481
		$txt = str_replace('&#173;', '', $txt);
412
		$txt = str_ireplace('&nbsp;', ' ', $txt);
482
		$txt = str_ireplace('&nbsp;', ' ', $txt);
413
		$txt = trim($txt);
483
		$txt = trim($txt);
414
		$txt = preg_replace('/^<\/I>/', '', $txt);
484
		$txt = preg_replace('/^<\/I>/', '', $txt);
415
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
485
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
416
		return $txt;
486
		return $txt;
417
	}
487
	}
418
 
488
 
419
	private function traiterLigneDescription($txt) {
489
	private function traiterLigneDescription($txt) {
420
		$txt = $this->supprimerHtml($txt);
490
		$txt = $this->supprimerHtml($txt);
421
		$txt = $txt."\n";
491
		$txt = $txt."\n";
422
		return $txt;
492
		return $txt;
423
	}
493
	}
424
 
494
 
425
	private function nettoyerDescription($txt) {
495
	private function nettoyerDescription($txt) {
426
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
496
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
427
		$txt = trim($txt);
497
		$txt = trim($txt);
428
		return $txt;
498
		return $txt;
429
	}
499
	}
430
 
500
 
431
	private function corrigerDescription($txt) {
501
	private function corrigerDescription($txt) {
432
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
502
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
433
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
503
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
434
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
504
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
435
		return $txt;
505
		return $txt;
436
	}
506
	}
437
 
507
 
438
	private function remplacerHtmlParSyntaxeWiki($txt) {
508
	private function remplacerHtmlParSyntaxeWiki($txt) {
439
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
509
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
440
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
510
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
441
		return $txt;
511
		return $txt;
442
	}
512
	}
443
 
513
 
444
	private function ajouterLogAnalyse($txt) {
514
	private function ajouterLogAnalyse($txt) {
445
		if (isset($this->log['analyse']) == false) {
515
		if (isset($this->log['analyse']) == false) {
446
			$this->log['analyse'] = '';
516
			$this->log['analyse'] = '';
447
		}
517
		}
448
		$this->log['analyse'] .= "$txt\n";
518
		$this->log['analyse'] .= "$txt\n";
449
	}
519
	}
450
 
520
 
451
	private function ajouterLogSyno($txt) {
521
	private function ajouterLogSyno($txt) {
452
		if (isset($this->log['synonymes']) == false) {
522
		if (isset($this->log['synonymes']) == false) {
453
			$this->log['synonymes'] = '';
523
			$this->log['synonymes'] = '';
454
		}
524
		}
455
		$this->log['synonymes'] .= "$txt\n";
525
		$this->log['synonymes'] .= "$txt\n";
456
	}
526
	}
457
 
527
 
458
	private function ajouterLogAuteur($txt) {
528
	private function ajouterLogAuteur($txt) {
459
		if (isset($this->log['auteurs']) == false) {
529
		if (isset($this->log['auteurs']) == false) {
460
			$this->log['auteurs'] = '';
530
			$this->log['auteurs'] = '';
461
		}
531
		}
462
		$this->log['auteurs'] .= "$txt\n";
532
		$this->log['auteurs'] .= "$txt\n";
463
	}
533
	}
464
 
534
 
465
	private function ajouterLogProbleme($txt) {
535
	private function ajouterLogProbleme($txt) {
466
		if (isset($this->log['problemes']) == false) {
536
		if (isset($this->log['problemes']) == false) {
467
			$this->log['problemes'] = '';
537
			$this->log['problemes'] = '';
468
		}
538
		}
469
		$this->log['problemes'] .= "$txt\n";
539
		$this->log['problemes'] .= "$txt\n";
470
	}
540
	}
471
 
541
 
472
	private function ecrireLogs() {
542
	private function ecrireLogs() {
473
		foreach ($this->log as $nom => $log) {
543
		foreach ($this->log as $nom => $log) {
474
			$fichier = $this->dossierBase.self::DOSSIER_LOG.$nom.'.log';
544
			$fichier = $this->dossierBase.self::DOSSIER_LOG.$nom.'.log';
475
			file_put_contents($fichier, $log);
545
			file_put_contents($fichier, $log);
476
		}
546
		}
477
	}
547
	}
478
}
548
}
479
?>
549
?>