/
filters.php
354 lines (321 loc) · 14.7 KB
/
filters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Calls all the verify functions in order.
* @author José Díaz
* @param String $tweet contains the tweet to check.
*/
function verifyAll ($tweet){
verifyMail($tweet);
verifyTelephone($tweet);
verifyAge($tweet);
verifyVaccine($tweet);
verifyNeutered($tweet);
verifyRace($tweet);
}
/**
* Checks if a tweet contains an e-mail.
* @author Jesús Castro
* @param String $tweet contains the tweet to check.
* @return String with an e-mail if it contains one, 0 if it doesn't.
*/
function verifyMail ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
foreach ($wordArray as $word){
//Verificar si es un correo
if (
//Hay un solo '@'
strpos($word, "@") != false &&
strpos(substr($word, strpos($word, "@") + 1), "@") == false &&
//Los puntos están ubicados correctamente
checkDots( substr($word, strpos($word, "@")+1) )
)
{
// echo "Esto es un correo: " . $word . "</br>";
return $word;
}
}
return 'No especificado';
}
/**
* Auxiliary function to check the dots in an e-mail are valid. Used in verifyMail.
* @author Jesús Castro
*/
function checkDots ($string){
$aux = false;
if( $string[0] <> '.' &&
$string[strlen($string) - 1] <> '.' ) {
for ($j=0; $j < strlen($string); $j++) {
if ($string[$j] == '.') {
if ($aux == true) {
return false;
}
$aux = true;
} else {
$aux = false;
}
}
return true;
}
return false;
}
/**
* Checks if a tweet contains a telephone number.
* @author Jesús Castro
* @param String $tweet contains the tweet to check.
* @return String with a telephone number if it contains one, 0 if it doesn't.
*/
function verifyTelephone ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
foreach ($wordArray as $word){
if (strlen($word) >= 7){
$aux = true;
for ($j=0; $j < strlen($word); $j++){
if ( ( ($word[$j] < '0') || ($word[$j] > '9') ) &&
($word[$j] != '-') &&
($word[$j] != '+') ){
$aux = false;
}
}
if($aux == true){
// echo "Esto es un teléfono: " . $word . "</br>";
return $word;
}
}
}
return 'No especificado';
}
/**
* Checks if a tweet contains an age.
* @author Jesús Castro
* @param String $tweet contains the tweet to check.
* @return String with the age if it contains one, 0 if it doesn't.
*/
function verifyAge ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$count = 0;
$wordArraylength = count($wordArray);
foreach ($wordArray as $word){
if ( $count < ($wordArraylength - 1) ){
if ((strlen($word) <= 2) &&
(strlen($word) > 0) &&
((strstr($wordArray[$count + 1], "año")) || (strstr($wordArray[$count + 1], "mes")))
){
$aux = true;
for ($j=0; $j < strlen($word); $j++){
if (($word[$j] < '0') ||
($word[$j] > '9')){
$aux = false;;
}
}
if($aux == true)
// echo "Esto es una edad: " . $word . " " . $wordArray[$count + 1] . "</br>";
return $word . " " . $wordArray[$count + 1];
}
$count++;
}
}
return 'No especificado';
}
/**
* Checks if the tweet contains information about vaccines.
* @author José Díaz
* @param String $tweet The tweet to check.
* @return 1 if animal is vaccinated, 0 if not.
*/
function verifyVaccine ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$count = 0;
foreach ($wordArray as $word) {
if ( (strstr($word, "vacuna")) || (similar_text($word, "vacuna") >= 5) ){
if ($count >= 2){
if (($wordArray[$count-1] != "no") &&
($wordArray[$count-1] != "ni") &&
($wordArray[$count-2] != "no") &&
($wordArray[$count-1] != "sin") &&
!(strstr("falta", $wordArray[$count-1])))
{
// echo "Sí está vacunado, en la palabra: " . $word . "</br>";
return 'Vacunado';
}
}
}
$count++;
}
return 'No vacunado';
}
/**
* Checks if the tweet contains information about neutered animals.
* @author José Díaz
* @param String $tweet The tweet to check.
* @return 1 if animal is neutered, 0 if not.
*/
function verifyNeutered ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$count = 0;
foreach ($wordArray as $word) {
if ( (strstr($word, "castrado")) || (similar_text($word, "castrado") >= 6) || (strstr($word, "esteril")) || (strstr($word, "capado")) ) {
if ($count >= 2){
if (($wordArray[$count-1] != "no") &&
($wordArray[$count-1] != "ni") &&
($wordArray[$count-2] != "no") &&
($wordArray[$count-1] != "sin") &&
!(strstr("falta", $wordArray[$count-1])))
{
// echo "Sí está castrado, en la palabra: " . $word . "</br>";
return 'Castrado';
}
}
}
$count++;
}
return 'No castrado';
}
/**
* Checks if the tweet contains information about a specific race.
* @author José Díaz
* @param String $tweet The tweet to check.
* @return String with the dogs race, 0 if not found.
*/
function verifyRace ($tweet){
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$wordArraylength = count($wordArray);
$count = 0;
$races = readRacesFile();
foreach ($wordArray as $word) {
$count2 = 1;
if (!strstr($word, "http")){
$wordLength = strlen($word);
foreach ($races as $race){
$raceLength = strlen($race);
if ( abs($wordLength - $raceLength) <= 1 ){ // Checks the difference in size isn't too big.
if ( ($count2 <= 12) && ($count < ($wordArraylength - 1)) && (!strstr($wordArray[$count + 1], "http")) ){
$raceAuxArray = explode(" ", $race);
$raceLength1 = strlen($raceAuxArray[0]);
$raceLength2 = strlen($raceAuxArray[1]);
$percent = 0;
if ($raceLength1 <= 4){
if ( (similar_text($word, $raceAuxArray[0]) >= 3) ){ // Checks similarity in the first word of the race.
if ($raceLength2 <= 4){
if (similar_text($wordArray[$count + 1], $raceAuxArray[1]) >= 3){ // Checks similarity in the second word of the race.
// echo "La raza es: " . $race . " en la palabra " . $word . " " . $wordArray[$count + 1] . "</br>";
return $race;
}
}
else{
similar_text($wordArray[$count + 1], $raceAuxArray[1], $percent); // Stores in $percent the percentage of similarity between the word and the second word of the race.
if ( $percent >= 80 ){ // Checks similarity in the second word of the race is at least of 80%.
// echo "La raza es: " . $race . " en la palabra " . $word . " " . $wordArray[$count + 1] . "</br>";
return $race;
}
}
}
}
else{
similar_text($word, $raceAuxArray[0], $percent); // Stores in $percent the percentage of similarity between the word and the first word of the race.
if ( $percent >= 80 ){ // Checks similarity in the first word of the race is at least of 80%.
if ($raceLength2 <= 4){
if (similar_text($wordArray[$count + 1], $raceAuxArray[1]) >= 3){ // Checks similarity in the second word of the race.
// echo "La raza es: " . $race . " en la palabra " . $word . " " . $wordArray[$count + 1] . "</br>";
return $race;
}
}
else{
similar_text($wordArray[$count + 1], $raceAuxArray[1], $percent); // Stores in $percent the percentage of similarity between the word and the second word of the race.
if ( $percent >= 80 ){ // Checks similarity in the second word of the race is at least of 80%.
// echo "La raza es: " . $race . " en la palabra " . $word . " " . $wordArray[$count + 1] . "</br>";
return $race;
}
}
}
}
}
elseif ($count2 > 12){
$raceLength = strlen($race);
if ($raceLength <= 4){
if (similar_text($word, $race) >= 3){ // Checks similarity between the word and the race.
// echo "La raza es: " . $race . " en la palabra " . $word . "</br>";
return $race;
}
}
else{
similar_text($word, $race, $percent); // Stores in $percent the percentage of similarity between the word and race.
if ( $percent >= 80 ){ // Checks similarity between the word and the race is at least of 80%.
// echo "La raza es: " . $race . " en la palabra " . $word . "</br>";
return $race;
}
}
}
}
$count2++;
}
}
$count++;
}
return 'No especificado';
}
/**
* Auxialiary function that trims the received string and removes ',' and '.'
* @author José Díaz
* @param String $arrayString The string to trim
*/
function trimArray($arrayString){
return trim($arrayString, '.,');
}
/**
* Reads a file (called dog_races.txt at root folder) with a list of dog races, stores it in an array and returns it.
* @author José Díaz
* @return Array containing a list of dog races, each index contains a single race.
*/
function readRacesFile (){
$racesFile = fopen("dog_races.txt", "r") or die("Failed opening dog_races.txt");
$races = array();
while( !feof($racesFile) ) {
array_push($races, strtolower(trim(fgets($racesFile))));
}
fclose($racesFile);
return $races;
}
function verifySex ($tweet) {
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$count = 0;
foreach ($wordArray as $word) {
if ( (strstr($word, "macho")) || strstr($word, "perro") || strstr($word, "perrito") || strstr($word, "gato") || strstr($word, "gatito")) {
return 'M';
}
if ( (strstr($word, "hembra")) || strstr($word, "perra") || strstr($word, "perrita") || strstr($word, "gata") || strstr($word, "gatita")) {
return 'F';
}
$count++;
}
return 'No especificado';
}
function verifyEsp ($tweet) {
$lowerTweet = strtolower($tweet);
$wordArray = explode(" ", $lowerTweet);
$wordArray = array_map('trimArray', $wordArray); // Applies trimArray() to all the strings in the array.
$count = 0;
foreach ($wordArray as $word) {
if ( (strstr($word, "perro")) || strstr($word, "perrito") || strstr($word, "canino") || strstr($word, "perra") || strstr($word, "perrita") || strstr($word, "canina")) {
return 'Perro';
}
if ( (strstr($word, "gato")) || strstr($word, "gatito") || strstr($word, "gata") || strstr($word, "gatita") || strstr($word, "felino") || strstr($word, "felina")) {
return 'Gato';
}
$count++;
}
return 'Otro';
}
?>