Ciao a tutti! Sto cercando di fare il confronto in percentuale della differenza tra due stringhe, e funziona tutto. Uso questa funzione:
Codice PHP:
function strings_compare($str1, $str2) {
$count = 0;
$str1 = ereg_replace("[^a-z]", ' ', strtolower($str1));
while(strstr($str1, ' ')) {
$str1 = str_replace(' ', ' ', $str1);
}
$str1 = explode(' ', $str1);
$str2 = ereg_replace("[^a-z]", ' ', strtolower($str2));
while(strstr($str2, ' ')) {
$str2 = str_replace(' ', ' ', $str2);
}
$str2 = explode(' ', $str2);
if(count($str1)<count($str2)) {
$tmp = $str1;
$str1 = $str2;
$str2 = $tmp;
unset($tmp);
}
for($i=0; $i<count($str1); $i++) {
if(in_array($str1[$i], $str2)) {
$count++;
}
}
return $count/count($str2)*100;
}
che ci mette circa 3 secondi (!!!) per una stringa di 25/30 caratteri! Chiaramente se gli passo un file di testo (circa 3/4000 caratteri) lo script va in timeout...
Ora, dal momento che più che cambiare il timeout mi piacerebbe trovare una soluzione più rapida (anche a costo di perdere di precisione), come fareste voi?
Ho guardato anche similar_text() ma è ricorsiva, e per files grandi è (forse) anche peggio, come scritto anche sul manuale:
This calculates the similarity between two strings as described in Oliver [1993]. Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of the longest string.
Grazie