Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 1026 → Rev 1027

/tags/v5.5-arbousiere/scripts/modules/coste/Description.php
New file
0,0 → 1,570
<?php
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
class Description {
 
const DOSSIER_V0 = '../../../donnees/coste/0.00/';
const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
const DOSSIER_LOG = 'log/';
 
private $auteurs = array();
private $dossierBase = '';
private $conteneur = null;
private $outils = null;
private $messages = null;
private $action = '';
private $nomFichier = '';
private $nomDossier = '';
private $listeFichiers = array();
private $fichierNum = '';
private $fichier = '';
private $log = '';
private $correspondance = array();
private $infosCorrespondanceBase = array(
'nom_sci' => '',
'auteur' => '',
'nom_addendum' => '',
'annee' => '',
'biblio_origine' => '',
'rang' => 290,
'nom_francais' => '',
'nom_coste' => '',
'auteur_coste' => '',
'biblio_coste' => '',
'num_nom_coste' => '',
'num_nom_retenu_coste' => '',
'num_tax_sup_coste' => '',
'tome' => '',
'page' => '',
'synonymie_coste' => '',
'flore_bdtfx_nn' => '',
'flore_bdtfx_nt' => '');
 
public function __construct(Conteneur $conteneur) {
mb_internal_encoding('UTF-8');
setlocale(LC_ALL, 'fr_FR.UTF-8');
$this->conteneur = $conteneur;
$this->outils = $conteneur->getOutils();
$this->messages = $conteneur->getMessages();
$this->dossierBase = dirname(__FILE__).'/';
}
 
public function genererDescriptionTxt() {
$this->chargerFichiers();
 
foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
$this->messages->afficherAvancement("Création des descriptions au format txt");
$this->genererFichier();
}
echo "\n";
}
 
public function verifierDescriptionTxt() {
$this->chargerFichiers();
 
foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
$this->messages->afficherAvancement("Analyse des descriptions");
$this->verifierFichier();
}
echo "\n";
$this->ecrireLogs();
}
 
public function genererCorrespondance() {
$this->chargerFichiers();
 
$this->ajouterLogSyno(implode("\t", array_keys($this->infosCorrespondanceBase)));
foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
$this->messages->afficherAvancement("Création du fichier de correspondance");
$this->extraireInfosCorrespondance();
}
echo "\n";
 
$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
$txtCorrespondance = $this->creerFichierCsvCorrespondance();
file_put_contents($fichierTxt, $txtCorrespondance);
 
ksort($this->auteurs);
foreach ($this->auteurs as $auteur => $nbre) {
$this->ajouterLogAuteur("$auteur\t$nbre");
}
 
$this->ecrireLogs();
}
 
 
private function chargerFichiers() {
$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
if (file_exists($this->nomDossier) === true) {
if (is_dir($this->nomDossier)) {
if ($dossierOuvert = opendir($this->nomDossier)) {
while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
}
}
closedir($dossierOuvert);
} else {
$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
}
} else {
$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
}
} else {
$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
}
 
asort($this->listeFichiers);
}
 
private function verifierFichier() {
$donnees = $this->analyserFichier();
$txt = $donnees['texte'];
$this->verifierOuvertureFermetureBalise($txt);
$this->verifierAbscenceEgale($txt);
$this->verifierNumero($txt);
// TODO : vérifier les noms vernauclaire qui sont abrégés
}
 
private function verifierOuvertureFermetureBalise($txt) {
$b_ouvert = substr_count($txt, '<B>');
$b_ferme = substr_count($txt, '</B>');
if ($b_ouvert != $b_ferme) {
$message = "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées";
$this->ajouterLogAnalyse($message);
}
$i_ouvert = substr_count($txt, '<I>');
$i_ferme = substr_count($txt, '</I>');
if ($i_ouvert != $i_ferme) {
$message = "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées";
$this->ajouterLogAnalyse($message);
}
}
 
private function verifierAbscenceEgale($txt) {
if (strripos($txt, '=') === false) {
$message = "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)";
$this->ajouterLogAnalyse($message);
}
}
 
private function verifierNumero($txt) {
if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
$message = "{$this->fichierNum} ne contient pas un numéro bien formaté";
$this->ajouterLogAnalyse($message);
}
}
 
private function extraireInfosCorrespondance() {
$donnees = $this->analyserFichier();
$infos = $this->infosCorrespondanceBase;
 
$titre = $donnees['titre'];
if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
$infos['num_nom_coste'] = $match[1];
$infos['num_nom_retenu_coste'] = $match[1];
$infos['nom_sci'] = $match[2];
$infos['num_tax_sup_coste'] = $match[3];
$infos['tome'] = $match[4];
$infos['page'] = $match[5];
} else {
$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
}
 
$corres = $donnees['correspondance'];
if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
$infos['flore_bdtfx_nn'] = $match[1];
$infos['flore_bdtfx_nt'] = $match[2];
} else {
$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
}
 
$txt = $donnees['texte'];
$txt = $this->corrigerDescription($txt);
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)) {
$infos['nom_coste'] = trim($match[1]);
$infos['rang'] = $this->obtenirRangNom($infos['nom_coste']);
$infos['auteur_coste'] = trim($match[2]);
$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
if (strpos($infos['auteur_coste'], '(' ) !== false) {
if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
$infos['auteur_coste'] = $this->traiterAuteur(trim($match[1]));
$infos['auteur'] = $this->normaliserAuteur($infos['auteur_coste']);
$parentheseContenu = trim($match[2]);
if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
$infos['annee'] = $parentheseContenu;
} else {
$infos['synonymie_coste'] = $parentheseContenu;
$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
}
} else {
$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
}
}
} else {
$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
}
 
$this->correspondance[] = $infos;
 
if ($infos['synonymie_coste'] != '') {
$this->traiterSynonymie($infos);
}
}
 
private function normaliserAuteur($auteurCoste) {
$auteur = '';
if ($auteurCoste != '') {
$auteur = str_replace(' et ', ' & ', $auteurCoste);
}
return $auteur;
}
 
private function traiterSynonymie($infos) {
$synoCoste = $infos['synonymie_coste'];
$synoCoste = preg_replace('/, etc[.]$/', '', $synoCoste);
$synoCoste = preg_replace('/^et /', '', $synoCoste);
$synoCoste = preg_replace('/^(([A-Z][.]|[A-Z]{3,}) [A-Z]{3,}(?:(?! et ).+)) et ([A-Z]{3,}) ((?![A-Z]{3,}).+)$/', '$1 ; $2 $3 $4', $synoCoste);
$synoCoste = preg_replace('/ et ((?:[A-Z][.]|[A-Z]{3,}) [A-Z]{3,})/', ' ; $1', $synoCoste);
$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.]|part[.]|centr[.])[^,]+))/', ' ;$1', $synoCoste);
 
$synonymes = explode(';', $synoCoste);
 
foreach ($synonymes as $num => $syno) {
$synoTraite = $this->traiterNomSyno($syno);
$nomSci = $this->obtenirNomSci($synoTraite, $infos, $synonymes, $num);
$rang = $this->obtenirRangNom($nomSci);
$complementNom = $this->obtenirComplementNom($synoTraite);
$auteurCoste = $this->obtenirAuteur($complementNom);
$auteur = $this->normaliserAuteur($auteurCoste);
$this->ajouterAuteur($auteur);
$biblioCoste = $this->obtenirBiblio($complementNom);
$annee = $this->extraireAnnee($complementNom);
 
$infosSyno = $this->infosCorrespondanceBase;
$infosSyno['nom_sci'] = $nomSci;
$infosSyno['auteur'] = $auteur;
$infosSyno['biblio_origine'] = $biblioCoste;
$infosSyno['annee'] = $annee;
$infosSyno['nom_addendum'] = $this->obtenirNomAddendum($syno);
 
$infosSyno['rang'] = $rang;
$infosSyno['nom_coste'] = $synoTraite;
$infosSyno['auteur_coste'] = $auteurCoste;
$infosSyno['biblio_coste'] = $biblioCoste;
$infosSyno['num_nom_coste'] = $infos['num_nom_coste'].'.'.($num + 1);
$infosSyno['num_nom_retenu_coste'] = $infos['num_nom_coste'];
$this->ajouterLogSyno(implode("\t", $infosSyno));
$this->correspondance[] = $infosSyno;
}
}
 
private function traiterNomSyno($syno) {
$syno = $this->nettoyerEspacesNomSyno($syno);
$syno = preg_replace('/^(?:avec|(?:compr|incl)[.]) /', '', $syno);
return $syno;
}
 
private function nettoyerEspacesNomSyno($syno) {
$syno = trim($syno);
$syno = trim($syno, ' ');
return $syno;
}
 
private function obtenirComplementNom($syno) {
$complementNom = '';
if (preg_match('/^(?:[^ ]+ [^ ]+ (?:(?:var|V)[.] [^ ]+|STELATUM|MINUS)|[^ ]+ [^ ]+) (.+)$/', $syno, $match)) {
$complementNom = $match[1];
}
return $complementNom;
}
 
private function obtenirBiblio($complementNom) {
$biblioCoste = '';
if (preg_match("/ (p[.] [0-9]{1,}|in .+||Fl[.] fr[.]|(?: Sp[.])? ed[.] [1-2])$/", $complementNom, $match)) {
$biblioCoste = $match[1];
}
return $biblioCoste;
}
 
private function extraireAnnee($complementNom) {
$annee = '';
if (preg_match('/(?:^| )([0-9]+)$/', $complementNom, $match)) {
$annee = $match[1];
}
return $annee;
}
 
private function obtenirAuteur($complementNom) {
$auteurCoste = '';
if ($complementNom != '') {
if (preg_match("/^((?!(?:auct.+|(?:, )?(?:non|an) .+|p[.] p[.])$).+)$/", $complementNom, $match)) {
$auteurCoste = $this->traiterAuteur($match[1]);
} else {
$message = "Impossible de récupérer l'auteur pour le complément de nom ($complementNom).";
$this->ajouterLogProbleme($message);
}
}
return $auteurCoste;
}
 
private function traiterAuteur($auteurCoste) {
$auteur = '';
if ($auteurCoste != '') {
$remplacementTxt = array(' p. p.', ' saltem part.', 'Fl. fr.');
$auteurCoste = str_replace($remplacementTxt, '', $auteurCoste);
$remplacementsRegExp = array(
' [0-9]{4}',
'[^ ]+ et auct[.], .+',
' auct.+',
',? part[.]',
' p[.] [0-9]{1,}',
' in .+|,? (?:non|an) .+',
'(?: Sp[.])? ed[.] [1-2]');
$auteur = preg_replace('/(?:'.implode('|', $remplacementsRegExp).')$/', '', $auteurCoste);
}
return $auteur;
}
 
private function ajouterAuteur($auteur) {
if (!isset($this->auteurs[$auteur])) {
$this->auteurs[$auteur] = 1;
} else {
$this->auteurs[$auteur]++;
}
}
 
private function obtenirNomAddendum($syno) {
$syno = $this->nettoyerEspacesNomSyno($syno);
$nomAddendum = array();
if (preg_match('/^((?:compr|incl)[.]) /', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
}
if (preg_match('/ ([^ ]+ et auct[.], .+)$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
} elseif (preg_match('/ (auct[.],? .+)$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
} else if (preg_match('/,? ((?:non|an) .+)$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
} else if (preg_match('/ (p[.] p[.])$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
} else if (preg_match('/ (saltem part[.])$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
} else if (preg_match('/,? (part[.])$/', $syno, $match)) {
$nomAddendum[] = '['.$match[1].']';
}
 
$nomAddendum = implode(' ; ', $nomAddendum);
return $nomAddendum;
}
 
private function remplacerAbreviationGenre($syno, $infos, $synonymes, $num) {
$nomSci = $syno;
 
if (preg_match('/^(([A-Zƌ])[.]) /', $syno, $matchSyno)) {
if ($matchSyno[2] == substr($infos['nom_coste'], 0, 1)) {
if (preg_match('/^([^ ]+) /', $infos['nom_coste'], $matchNomRetenu)) {
$nomSci = str_replace($matchSyno[1], $matchNomRetenu[1], $syno);
}
} else {
$synoPrecedent = $this->obtenirSynoAvecGenreComplet($synonymes, $num);
 
if ($matchSyno[2] == substr($synoPrecedent, 0, 1)) {
if (preg_match('/^([^ ]+) /', $synoPrecedent, $matchSynoPrec)) {
$nomSci = str_replace($matchSyno[1], $matchSynoPrec[1], $syno);
}
} else {
$message = "L'initiale du synonyme ($syno) ne correspondant pas au nom retenu {$infos['num_nom_coste']} ({$infos['nom_coste']}) ".
"ni au synonyme précédent ($synoPrecedent).";
$this->ajouterLogProbleme($message);
}
}
}
return $nomSci;
}
 
private function obtenirSynoAvecGenreComplet($synonymes, $num) {
$synoPrecedent = '';
$synoOk = false;
while ($synoOk == false) {
if ($num < 0) {
$synoOk = true;
}
$synoPrecedent = $this->obtenirSynoPrecedent($synonymes, $num);
if (preg_match('/^[A-Zƌ][.] /', $synoPrecedent)) {
$num = $num - 1;
} else {
$synoOk = true;
}
}
return $synoPrecedent;
}
 
private function obtenirSynoPrecedent($synonymes, $num) {
$synoPrecedent = '';
if (isset($synonymes[($num - 1 )])) {
$synoPrecedent = $this->traiterNomSyno($synonymes[($num - 1 )]);
}
return $synoPrecedent;
}
 
 
private function obtenirNomSci($syno, $infos, $synonymes, $num) {
$nomSci = $this->remplacerAbreviationGenre($syno, $infos, $synonymes, $num);
 
if (preg_match('/^([^ ]+) ([^ ]+) (?:(?:var|V)[.] ([^ ]+)|(STELATUM||MINUS) )/', $nomSci, $match)) {
$genre = $this->normaliserGenre($match[1]);
$sp = $this->normaliserEpithete($match[2]);
$infrasp = isset($match[4]) ? $match[4] : $match[3];
$infrasp = $this->normaliserEpithete($infrasp);
$nomSci = "$genre $sp var. $infrasp";
} else if (preg_match('/^([^ ]+) ([^ ]+)(?: |$)/', $nomSci, $match)) {
$genre = $this->normaliserGenre($match[1]);
$sp = $this->normaliserEpithete($match[2]);
$nomSci = "$genre $sp";
} else {
$message = "Le synonyme ($nomSci) du nom {$infos['num_nom_coste']} a une structure étrange.";
$this->ajouterLogProbleme($message);
}
return $nomSci;
}
 
private function normaliserGenre($genre) {
$genre = mb_convert_case(mb_strtolower($genre), MB_CASE_TITLE);
$genre = str_replace('Æ', 'æ', $genre);
return $genre;
}
 
private function normaliserEpithete($sp) {
$sp = mb_strtolower($sp);
$sp = str_replace('Æ', 'æ', $sp);
return $sp;
}
 
private function obtenirRangNom($syno) {
$rang = 290;
if (strpos($syno, ' var. ')) {
$rang = 340;
}
return $rang;
}
 
private function creerFichierCsvCorrespondance() {
$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
foreach ($this->correspondance as $infos) {
$lignes[] = implode("\t", $infos);
}
 
$txt = '';
$txt = implode("\n", $lignes);
return $txt;
}
 
private function genererFichier() {
$donnees = $this->analyserFichier();
 
$txt = $this->nettoyerDescription($donnees['texte']);
$txt = $this->corrigerDescription($txt);
$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
file_put_contents($fichierTxt, $txt);
 
unset($donnees['texte']);
$txt = implode("\n", $donnees);
$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
file_put_contents($fichierTxt, $txt);
}
 
private function analyserFichier() {
$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
if ($fichierOuvert = fopen($this->fichier, 'r')) {
$i = 1;
while ($ligne = fgets($fichierOuvert)) {
if ($i == 24) {
$donnees['titre'] = $this->supprimerHtml($ligne);
} elseif ($i >= 45 && $i <= 60) {
$donnees['texte'] .= $this->traiterLigneDescription($ligne);
} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
$donnees['correspondance'] = $this->supprimerHtml($ligne);
}
$i++;
}
fclose($fichierOuvert);
} else {
$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
}
return $donnees;
}
 
private function supprimerHtml($txt) {
$txt = strip_tags($txt,'<b>,<i>');
$txt = str_replace('&#173;', '', $txt);
$txt = str_ireplace('&nbsp;', ' ', $txt);
$txt = trim($txt);
$txt = preg_replace('/^<\/I>/', '', $txt);
$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
return $txt;
}
 
private function traiterLigneDescription($txt) {
$txt = $this->supprimerHtml($txt);
$txt = $txt."\n";
return $txt;
}
 
private function nettoyerDescription($txt) {
$txt = preg_replace("/\n{2,}+/", "\n", $txt);
$txt = trim($txt);
return $txt;
}
 
private function corrigerDescription($txt) {
$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
return $txt;
}
 
private function remplacerHtmlParSyntaxeWiki($txt) {
$txt = str_replace(array('<B>', '</B>'), '**', $txt);
$txt = str_replace(array('<I>', '</I>'), '//', $txt);
return $txt;
}
 
private function ajouterLogAnalyse($txt) {
if (isset($this->log['analyse']) == false) {
$this->log['analyse'] = '';
}
$this->log['analyse'] .= "$txt\n";
}
 
private function ajouterLogSyno($txt) {
if (isset($this->log['synonymes']) == false) {
$this->log['synonymes'] = '';
}
$this->log['synonymes'] .= "$txt\n";
}
 
private function ajouterLogAuteur($txt) {
if (isset($this->log['auteurs']) == false) {
$this->log['auteurs'] = '';
}
$this->log['auteurs'] .= "$txt\n";
}
 
private function ajouterLogProbleme($txt) {
if (isset($this->log['problemes']) == false) {
$this->log['problemes'] = '';
}
$this->log['problemes'] .= "$txt\n";
}
 
private function ecrireLogs() {
foreach ($this->log as $nom => $log) {
$fichier = $this->dossierBase.self::DOSSIER_LOG.$nom.'.log';
file_put_contents($fichier, $log);
}
}
}
?>