146 |
jpm |
1 |
<?php
|
|
|
2 |
class DeterminationFormateur implements Formateur {
|
|
|
3 |
|
159 |
jpm |
4 |
const TPL_VUE = 'determination';
|
146 |
jpm |
5 |
|
|
|
6 |
private $parametres = null;
|
|
|
7 |
private $surligneur = null;
|
|
|
8 |
private $trieur = null;
|
|
|
9 |
private $urls = null;
|
|
|
10 |
private $fusioneur = null;
|
|
|
11 |
private $manipulateurDeChaine = null;
|
|
|
12 |
private $imagesService = null;
|
|
|
13 |
|
|
|
14 |
private $motsASurligner = array();
|
|
|
15 |
private $noms = array();
|
|
|
16 |
private $infosPourTpl = array();
|
617 |
mathilde |
17 |
|
311 |
jpm |
18 |
|
146 |
jpm |
19 |
public function __construct(ParametresResultats $parametres, Array $resultats,
|
|
|
20 |
Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
|
|
|
21 |
ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
|
|
|
22 |
|
|
|
23 |
$this->parametres = $parametres;
|
|
|
24 |
$this->noms = $resultats['resultat'];
|
|
|
25 |
$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
|
|
|
26 |
$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
|
|
|
27 |
$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
|
|
|
28 |
$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
|
|
|
29 |
$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function getTplInfos() {
|
|
|
33 |
return $this->infosPourTpl;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function getTplNom() {
|
|
|
37 |
return self::TPL_VUE;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function formater() {
|
617 |
mathilde |
41 |
$this->obtenirUrlsImagesCoste();
|
146 |
jpm |
42 |
$this->obtenirUrlsPhotos();
|
279 |
gduche |
43 |
$this->chargerRepartition();
|
146 |
jpm |
44 |
$this->extraireInfosNomsPourTplDetermination();
|
|
|
45 |
}
|
617 |
mathilde |
46 |
|
872 |
delphine |
47 |
|
617 |
mathilde |
48 |
private function obtenirUrlsImagesCoste() {
|
|
|
49 |
$this->extraireInfosTaxons();
|
|
|
50 |
$this->imagesService->setProjet('coste');
|
733 |
aurelien |
51 |
$tax = implode(',', $this->infosPourTpl['taxons']);
|
|
|
52 |
$this->imagesService->setNnTaxon($tax);
|
|
|
53 |
$costeImg = $this->imagesService->getInfosImagesTaxons();
|
|
|
54 |
if (!empty($costeImg)) {
|
|
|
55 |
foreach ($costeImg as $infos) {
|
|
|
56 |
$num_taxon = $infos['num_taxonomique'];
|
|
|
57 |
$images[$num_taxon][] = $infos['binaire.href'];
|
|
|
58 |
$this->infosPourTpl['imagesCoste'] = $images;
|
617 |
mathilde |
59 |
}
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
private function extraireInfosTaxons() {
|
|
|
63 |
foreach ($this->noms as $id => $nom ) {
|
|
|
64 |
if (array_key_exists('num_taxonomique', $nom)) {
|
|
|
65 |
$this->infosPourTpl['taxons'][$id] = $nom['num_taxonomique'];
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
872 |
delphine |
70 |
private function obtenirUrlsPhotos() {
|
|
|
71 |
$this->imagesService->setProjet('cel');
|
|
|
72 |
$ids = $this->extraireIdDesNoms();
|
|
|
73 |
$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
|
|
|
74 |
$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
|
279 |
gduche |
75 |
}
|
146 |
jpm |
76 |
private function extraireIdDesNoms() {
|
|
|
77 |
$ids = array();
|
|
|
78 |
foreach ($this->noms as $id => $nom) {
|
|
|
79 |
$idAAjouter = $id;
|
|
|
80 |
if (is_numeric($idAAjouter)) {
|
|
|
81 |
$ids[] = $idAAjouter;
|
|
|
82 |
}
|
|
|
83 |
if (array_key_exists('nom_retenu.id', $nom)) {
|
|
|
84 |
if (in_array($nom['nom_retenu.id'], $ids) == false) {
|
|
|
85 |
$idAAjouter = $nom['nom_retenu.id'];
|
|
|
86 |
if (is_numeric($idAAjouter)) {
|
|
|
87 |
$ids[] = $idAAjouter;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
return $ids;
|
|
|
93 |
}
|
|
|
94 |
private function supprimerCodeReftaxDesIds($urls) {
|
|
|
95 |
$urlsNettoyees = array();
|
|
|
96 |
foreach ($urls as $id => $url) {
|
311 |
jpm |
97 |
$id = $this->supprimerCodeReftax($id);
|
146 |
jpm |
98 |
$urlsNettoyees[$id] = $url;
|
|
|
99 |
}
|
|
|
100 |
return $urlsNettoyees;
|
|
|
101 |
}
|
311 |
jpm |
102 |
private function supprimerCodeReftax($chaine) {
|
|
|
103 |
$codeReftax = $this->parametres->reftaxCourant.'.';
|
|
|
104 |
$chaine = str_replace($codeReftax, '', $chaine);
|
|
|
105 |
return $chaine;
|
|
|
106 |
}
|
872 |
delphine |
107 |
|
|
|
108 |
// TODO : utiliser le conteneur pour charger tous les objets de cette classe
|
|
|
109 |
private function chargerRepartition() {
|
|
|
110 |
$conteneur = new Conteneur();
|
|
|
111 |
$cartesWs = $conteneur->getApiCartes();
|
|
|
112 |
$cartesWs->setProjet('chorodep');
|
|
|
113 |
$cartesWs->setLargeur('108x101');
|
|
|
114 |
$urls = array();
|
|
|
115 |
foreach ($this->noms as $id => $nom) {
|
|
|
116 |
if (array_key_exists('nom_retenu.id', $nom)) {
|
|
|
117 |
$id = $nom['nom_retenu.id'];
|
|
|
118 |
$cartesWs->setId("nn:$id");
|
|
|
119 |
if (array_key_exists($id, $urls) == false) {
|
|
|
120 |
$urls[$id] = $cartesWs->getUrlPng();
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
$this->infosPourTpl['repartition']['urls'] = $urls;
|
|
|
125 |
}
|
311 |
jpm |
126 |
|
872 |
delphine |
127 |
private function extraireInfosNomsPourTplDetermination() {
|
|
|
128 |
$tri = $this->diviserResultats();
|
|
|
129 |
$this->infosPourTpl['nomsSansCorrespondance'] = isset($tri['sansCorres']) ? $tri['sansCorres'] : false;
|
|
|
130 |
$this->infosPourTpl['noms'] = isset($tri['retenus']) ? $tri['retenus'] : false;
|
|
|
131 |
$this->infosPourTpl['synonymes'] = isset($tri['synonymes'] ) ? $tri['synonymes'] : false;
|
|
|
132 |
}
|
792 |
mathilde |
133 |
/**
|
|
|
134 |
* division ordonnée par
|
|
|
135 |
* 1 - noms retenus qui commencent par la requete
|
|
|
136 |
* 2 - requete contenue dans un synonyme dont le nom retenu ne contient pas la requete
|
|
|
137 |
* 3 - requete contenue dans un hybride retenu
|
|
|
138 |
* 4 - requete contenue dans un nom retenu mais pas au début
|
|
|
139 |
* 5 - requete contenue dans un nom sans correspondance
|
|
|
140 |
*
|
|
|
141 |
*/
|
|
|
142 |
private function diviserResultats() {
|
|
|
143 |
$tri = array();
|
|
|
144 |
$sansCorres = array();
|
|
|
145 |
$synonymes = array();
|
|
|
146 |
$retenus = array();
|
|
|
147 |
foreach ($this->noms as $cle => $valeurs) {
|
|
|
148 |
if ($valeurs['nom_retenu.libelle'] == null) {//sans correspondances
|
|
|
149 |
$sansCorres[$cle] = $valeurs['nom_sci'];
|
|
|
150 |
} elseif ($valeurs['retenu'] == 'true') { // retenus
|
|
|
151 |
if (preg_match('/ x |x /',$valeurs['nom_sci'] ) ) {
|
|
|
152 |
//hybrides
|
880 |
aurelien |
153 |
$retenus[2][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
792 |
mathilde |
154 |
} elseif (preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', strtolower($valeurs['nom_sci']) ) ) {
|
|
|
155 |
//commence par
|
880 |
aurelien |
156 |
$retenus[0][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
146 |
jpm |
157 |
} else {
|
792 |
mathilde |
158 |
//contient
|
880 |
aurelien |
159 |
$retenus[3][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
146 |
jpm |
160 |
}
|
792 |
mathilde |
161 |
} else {//synonymes
|
|
|
162 |
$idNomRetenu = $valeurs['nom_retenu.id'];
|
|
|
163 |
if (preg_match('/'.strtolower($this->parametres->masqueRecherche).'/', strtolower($valeurs['nom_retenu.libelle'])) == 0) {
|
|
|
164 |
//synonymes dont le nom retenu ne contient pas la requête
|
880 |
aurelien |
165 |
$retenus[1][$idNomRetenu] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_retenu_complet'], $valeurs['rang.libelle']);
|
792 |
mathilde |
166 |
$this->infosPourTpl['taxons'][$idNomRetenu] = $valeurs['num_taxonomique']; //num taxon pour images coste
|
|
|
167 |
}
|
|
|
168 |
$synonymes[$idNomRetenu][] = $this->retournerInfosSynonyme($cle, $valeurs);
|
146 |
jpm |
169 |
}
|
|
|
170 |
}
|
792 |
mathilde |
171 |
ksort($retenus);
|
|
|
172 |
$tri['retenus'] = $retenus;
|
|
|
173 |
$tri['synonymes'] = $synonymes;
|
|
|
174 |
$tri['sansCorres'] = $sansCorres;
|
|
|
175 |
return $tri;
|
146 |
jpm |
176 |
}
|
792 |
mathilde |
177 |
private function retournerInfosNomRetenu($cle, $nom_sci, $rang) {
|
|
|
178 |
$infos = array();
|
|
|
179 |
$infos['nomSciRetenu'] = $nom_sci;
|
|
|
180 |
$infos['urlFiche'] = $this->urls->obtenirUrlFiche($cle, $this->parametres->typeNom, strtolower($this->parametres->masqueRecherche), $nom_sci);
|
|
|
181 |
$infos['rang'] = $rang;
|
|
|
182 |
return $infos;
|
|
|
183 |
}
|
|
|
184 |
private function retournerInfosSynonyme($cle, $valeurs) {
|
|
|
185 |
$infos = array();
|
|
|
186 |
$infos['nn'] = $cle;
|
|
|
187 |
$infos['nomSci'] = $valeurs['nom_sci'];
|
|
|
188 |
$infos['urlFiche'] = $this->urls->obtenirUrlFiche($cle, $this->parametres->typeNom, $this->parametres->masqueRecherche, $valeurs['nom_retenu.libelle']);
|
|
|
189 |
return $infos;
|
|
|
190 |
}
|
|
|
191 |
public function trier() {
|
|
|
192 |
if ($this->donnerNombreResultatRetenus() <= 3) { // classement par score
|
|
|
193 |
$nomsRetenus = array();
|
|
|
194 |
foreach ($this->infosPourTpl['noms'] as $categorie) { //ote la division des retenus
|
|
|
195 |
$nomsRetenus += $categorie;
|
|
|
196 |
}
|
|
|
197 |
$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
|
|
|
198 |
$this->trieur->setTableau($nomsRetenus);
|
|
|
199 |
$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
|
|
|
200 |
$this->infosPourTpl['noms'] = $this->trieur->trier();
|
|
|
201 |
$this->infosPourTpl['nomsSansCorrespondance'] =
|
|
|
202 |
$this->classerAlphabetiquement('nomSciRetenu', $this->infosPourTpl['nomsSansCorrespondance'] );
|
|
|
203 |
} else { // classement alphabétique
|
|
|
204 |
$nomsRetenus = array();
|
|
|
205 |
foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs ) { //classement alpha par groupes
|
|
|
206 |
$nomsRetenus += $this->classerAlphabetiquement('nomSciRetenu', $valeurs);
|
|
|
207 |
}
|
|
|
208 |
$this->infosPourTpl['noms'] = $nomsRetenus;
|
|
|
209 |
$this->infosPourTpl['nomsSansCorrespondance'] =
|
825 |
mathilde |
210 |
$this->classerAlphabetiquement('nomSciRetenu', $this->infosPourTpl['nomsSansCorrespondance'] );
|
792 |
mathilde |
211 |
}
|
146 |
jpm |
212 |
}
|
872 |
delphine |
213 |
private function donnerNombreResultatRetenus() {
|
|
|
214 |
$nbre = 0;
|
|
|
215 |
foreach ($this->infosPourTpl['noms'] as $lignes) {
|
|
|
216 |
$nbre += count($lignes);
|
|
|
217 |
if ($nbre >= 3 ) {
|
|
|
218 |
break;
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
return $nbre;
|
|
|
222 |
}
|
792 |
mathilde |
223 |
|
146 |
jpm |
224 |
private function ajouterAuxNomsScoreSimilariteAvec($masque) {
|
|
|
225 |
$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
|
|
|
226 |
foreach ($this->infosPourTpl['noms'] as $id => $nom) {
|
|
|
227 |
$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
|
|
|
228 |
$stat = array();
|
|
|
229 |
// Prime pour la ressemblance globale :
|
|
|
230 |
$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
|
|
|
231 |
// On affine
|
|
|
232 |
$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
|
|
|
233 |
$nom['score'] = $score;
|
|
|
234 |
$this->infosPourTpl['noms'][$id] = $nom;
|
|
|
235 |
}
|
|
|
236 |
}
|
872 |
delphine |
237 |
private function classerAlphabetiquement($champs, $valeurs) {
|
|
|
238 |
$this->trieur->setTableau($valeurs);
|
|
|
239 |
$this->trieur->setChampsEtOrdres(array($champs => SORT_NATURAL));
|
|
|
240 |
return $this->trieur->trier();
|
|
|
241 |
}
|
146 |
jpm |
242 |
|
872 |
delphine |
243 |
|
|
|
244 |
|
146 |
jpm |
245 |
public function surligner() {
|
|
|
246 |
$this->definirMotsASurligner();
|
|
|
247 |
foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
|
252 |
delphine |
248 |
$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
|
|
|
249 |
if (isset($this->infosPourTpl['synonymes'][$idNom])) {
|
|
|
250 |
foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
|
|
|
251 |
$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
|
146 |
jpm |
252 |
}
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
private function definirMotsASurligner() {
|
|
|
257 |
$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
|
|
|
258 |
}
|
|
|
259 |
private function surlignerMotsMasqueRecherche($nom) {
|
|
|
260 |
$this->surligneur->setTexte($nom);
|
|
|
261 |
$nom = $this->surligneur->surlignerMots($this->motsASurligner);
|
|
|
262 |
return $nom;
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
?>
|