1 |
dpavlin |
1.1 |
<?php |
2 |
|
|
/* |
3 |
|
|
+----------------------------------------------------------------------+ |
4 |
|
|
| Classe keywords | |
5 |
|
|
+----------------------------------------------------------------------+ |
6 |
|
|
| php4 - 2001 | |
7 |
|
|
+----------------------------------------------------------------------+ |
8 |
|
|
| La classe keywords permet de créer des mots clés que l'on peut | |
9 |
|
|
| ensuite passer à un moteur de recherche de manière simple. | |
10 |
|
|
| La classe va transformer les mots passés pour en faire des | |
11 |
|
|
| arguments exploitables pour un traitement ultérieur. | |
12 |
|
|
| | |
13 |
|
|
| | |
14 |
|
|
| | |
15 |
|
|
+----------------------------------------------------------------------+ |
16 |
|
|
| Auteur: Olivier Meunier | |
17 |
|
|
| Version: 0.8 | |
18 |
|
|
+----------------------------------------------------------------------+ |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
class keywords |
22 |
|
|
{ |
23 |
|
|
/** |
24 |
|
|
* Déclarations des proriétés |
25 |
|
|
* |
26 |
|
|
* @str_chaine string chaine à traiter |
27 |
|
|
* @str_array array chaine pré-traité et transformée en tableau |
28 |
|
|
* @str_result string chaine du résultat final |
29 |
|
|
* @parasites array liste des caractères parasites |
30 |
|
|
* @exceptions array liste des mots à supprimer |
31 |
|
|
* @engine_words array tableau des correspondance des mots clés du moteur de recherche |
32 |
|
|
* @word_size integer taille à laquelle et en dessous de laquelle un mot est supprimé |
33 |
|
|
* |
34 |
|
|
*/ |
35 |
|
|
var $str_chaine; |
36 |
|
|
var $str_array; |
37 |
|
|
var $str_result; |
38 |
|
|
|
39 |
|
|
var $parasites = array(",",".","'","\"","!",":",";","?","/","\\","*"); |
40 |
|
|
|
41 |
|
|
var $exceptions = array("les","une","des","dans","pour","vers","sur","par","que","avec","sans","sous","qui","dont","mais","aussi", "ali"); |
42 |
|
|
|
43 |
|
|
var $engine_words = array( |
44 |
|
|
"AND" => array("and","et","&","i"), |
45 |
|
|
"OR" => array("ou","|","||","or","ili"), |
46 |
|
|
"NOT" => array("not","pas","non","ne","bez") |
47 |
|
|
); |
48 |
|
|
|
49 |
|
|
var $word_size = 1; |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* Constructeur |
54 |
|
|
* |
55 |
|
|
* Initialise l'objet. |
56 |
|
|
* |
57 |
|
|
* @str_chaine string chaine à traiter |
58 |
|
|
* |
59 |
|
|
* return void |
60 |
|
|
*/ |
61 |
|
|
function keywords($str_chaine="",$exceptions="") |
62 |
|
|
{ |
63 |
|
|
$this->str_chaine = $str_chaine; |
64 |
|
|
|
65 |
|
|
if($exceptions != "" && is_array($exceptions)) |
66 |
|
|
{ |
67 |
|
|
$this->exceptions = $exceptions; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Réduction de la casse et supression des accents |
73 |
|
|
* |
74 |
|
|
* Cette méthode réduit la casse et supprime les acents |
75 |
|
|
* des caractères accentués. |
76 |
|
|
* |
77 |
|
|
* return void |
78 |
|
|
* |
79 |
|
|
*/ |
80 |
|
|
function lower_case() |
81 |
|
|
{ |
82 |
|
|
$this->str_chaine = strtolower($this->str_chaine); |
83 |
dpavlin |
1.2 |
$this->str_chaine = strtr($this->str_chaine,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ¹©ðÐèÈæƾ®","aaaaaaaaaaaaooooooooooooeeeeeeeeiiiiiiiiuuuuuuuuynnssddcccczz"); |
84 |
dpavlin |
1.1 |
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* Suppression des caractères parasites |
89 |
|
|
* |
90 |
|
|
* Cette méthode supprime les caractère "parasites" |
91 |
|
|
* connus dans la propriété $this->parasites. |
92 |
|
|
* |
93 |
|
|
* return void |
94 |
|
|
* |
95 |
|
|
*/ |
96 |
|
|
function rem_parasites() |
97 |
|
|
{ |
98 |
|
|
$this->str_chaine = str_replace($this->parasites," ",$this->str_chaine); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* Passage de la chaine en tableau |
104 |
|
|
* |
105 |
|
|
* Cette méthode converti la chaine en tableau ce qui |
106 |
|
|
* permet d'appliquer des traitement mot à mot ensuite. |
107 |
|
|
* |
108 |
|
|
* return void |
109 |
|
|
* |
110 |
|
|
*/ |
111 |
|
|
function str_to_table() |
112 |
|
|
{ |
113 |
|
|
//On positionne des espace au niveau des parenthèses avant |
114 |
|
|
$this->str_chaine = ereg_replace("([()])"," \\1 ",$this->str_chaine); |
115 |
|
|
|
116 |
|
|
$this->str_array = split("([ ]+)",trim($this->str_chaine)); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Conversion des mots du moteur et autres mots |
122 |
|
|
* |
123 |
|
|
* Cette méthode converti les mots du moteur de recherche |
124 |
|
|
* (and, or, not) pour qu'ils soient exploitables. |
125 |
|
|
* Aucun paramètre. |
126 |
|
|
* |
127 |
|
|
*/ |
128 |
|
|
function engine_words_conv() |
129 |
|
|
{ |
130 |
|
|
foreach($this->str_array as $k => $v) |
131 |
|
|
{ |
132 |
|
|
//Conversion des mots du moteur (and, or, not) |
133 |
|
|
foreach($this->engine_words as $wk => $wv) |
134 |
|
|
{ |
135 |
|
|
if(in_array($v,$this->engine_words[$wk])) |
136 |
|
|
{ |
137 |
|
|
$this->str_array[$k] = $wk; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
//Conversion des mots à changer |
142 |
|
|
|
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* Suppression des exceptions |
149 |
|
|
* |
150 |
|
|
* Cette méthode supprimes les mots inutiles. |
151 |
|
|
* Aucun paramètre. |
152 |
|
|
* |
153 |
|
|
*/ |
154 |
|
|
function rem_exceptions() |
155 |
|
|
{ |
156 |
|
|
//foreach($this->str_array as $k => $v) |
157 |
|
|
for($i=0; $i<count($this->str_array); $i++) |
158 |
|
|
{ |
159 |
|
|
$v = $this->str_array[$i]; |
160 |
|
|
if ( in_array($v,$this->exceptions) || ( $this->is_word($v) && strlen($v)<=$this->word_size ) ) |
161 |
|
|
{ |
162 |
|
|
//unset($this->str_array[$k]); |
163 |
|
|
array_splice($this->str_array,$i,1); |
164 |
|
|
$i--; |
165 |
|
|
//$this->str_array[$k] = "§".$v."§"; |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
/** |
172 |
|
|
* Fonction privée de reconnaissance des mots |
173 |
|
|
* |
174 |
|
|
* Cette méthode privé renvoie TRUE si la chaine est considérée |
175 |
|
|
* comme un mot. |
176 |
|
|
* |
177 |
|
|
* @str string chaine à analyser |
178 |
|
|
* |
179 |
|
|
*/ |
180 |
|
|
function is_word($str) |
181 |
|
|
{ |
182 |
|
|
if (in_array($str,array_keys($this->engine_words)) || |
183 |
|
|
ereg("^[()]*$",$str) |
184 |
|
|
) |
185 |
|
|
return FALSE; |
186 |
|
|
else |
187 |
|
|
return TRUE; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
|
|
* Création de la chaine à passer au moteur |
192 |
|
|
* |
193 |
|
|
* Cette méthode transforme les mots pour qu'ils soient |
194 |
|
|
* exploitables par le moteur de recherche. |
195 |
|
|
* Aucun paramètre. |
196 |
|
|
* |
197 |
|
|
*/ |
198 |
|
|
function create_string() |
199 |
|
|
{ |
200 |
|
|
//Ajout des * devant les mots |
201 |
|
|
foreach($this->str_array as $k => $v) |
202 |
|
|
{ |
203 |
|
|
if($this->is_word($v)) |
204 |
|
|
$this->str_array[$k] = trim($v)."*"; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
//Ajout des AND si jamais aucun mot du moteur après |
208 |
|
|
foreach($this->str_array as $k => $v) |
209 |
|
|
{ |
210 |
|
|
if(!empty($this->str_array[$k+1])) |
211 |
|
|
{ |
212 |
|
|
if(($this->is_word($this->str_array[$k+1]) || $this->str_array[$k+1] == '(') && |
213 |
|
|
($this->is_word($v) || $v == ')')) |
214 |
|
|
{ |
215 |
|
|
$this->str_array[$k] = $v." AND"; |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
|
222 |
|
|
/** |
223 |
|
|
* Génératon complète |
224 |
|
|
* |
225 |
|
|
* Cette méthode génére la chaine à passer au moteur, à partir |
226 |
|
|
* des autres méthodes. |
227 |
|
|
* Aucun paramètre. |
228 |
|
|
* |
229 |
|
|
*/ |
230 |
|
|
function generate() |
231 |
|
|
{ |
232 |
|
|
$this->lower_case(); |
233 |
|
|
$this->rem_parasites(); |
234 |
|
|
$this->str_to_table(); |
235 |
|
|
$this->engine_words_conv(); |
236 |
|
|
$this->rem_exceptions(); |
237 |
|
|
|
238 |
|
|
$this->create_string(); |
239 |
|
|
$this->str_result = strtolower(implode(" ",$this->str_array)); |
240 |
|
|
|
241 |
|
|
return $this->str_result; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
/** |
246 |
|
|
* Encodage en url |
247 |
|
|
* |
248 |
|
|
* Cette méthode encode la chaine au format url. |
249 |
|
|
* Aucun paramètre. |
250 |
|
|
* |
251 |
|
|
*/ |
252 |
|
|
function to_url() |
253 |
|
|
{ |
254 |
|
|
return urlencode($this->str_result); |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* Coloration de la chaine |
260 |
|
|
* |
261 |
|
|
* Méthode de débugage qui colorie les mots clés du moteur |
262 |
|
|
* de recherche et les * |
263 |
|
|
* Aucun paramètre. |
264 |
|
|
* |
265 |
|
|
*/ |
266 |
|
|
function colorize() |
267 |
|
|
{ |
268 |
|
|
$str_result = $this->str_result; |
269 |
|
|
foreach($this->engine_words as $k => $v) |
270 |
|
|
{ |
271 |
|
|
$str_result = preg_replace("/(\b)(".strtolower($k).")(\b)/","<font color='red'>$1$2$3</font>",$str_result); |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
$str_result = preg_replace("/(\*)/","<font color='green'>$1</font>",$str_result); |
275 |
|
|
$str_result = preg_replace("/([()])/","<font color='blue'>$1</font>",$str_result); |
276 |
|
|
|
277 |
|
|
return $str_result; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
}//Fin de la classe |
281 |
|
|
?> |