note 44417 deleted from function.levenshtein by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 05 Aug 2004 10:04:50 +0000 |
| Subject: | note 44417 deleted from function.levenshtein by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73995@lists.php.net to get a copy of this message | ||
Note Submitter: paolo.bonzini@polimi.it
----
This function filters $haystack, returning true only if all the words in $keywords can be found,
with fuzzy search. (Of course it would be better to extract the preprocessing pass on the
$keywords, but we're feeling lazy).
Paolo & Andrea
-------------
function filter_words($haystack, $keywords)
{
if (!$keywords)
return true;
if (!$haystack)
return false;
// explode into words
$hwords = preg_split("/[\s\W]+/", $haystack);
$nwords = preg_split("/[\s\W]+/", $keywords);
// associa una chiave fonetica alle parole da cercare e al titolo del master
$hcerca = array ();
foreach ($hwords as $hayword) {
if ($hayword)
$hcerca[$hayword] = metaphone ($hayword);
}
$ncerca = array ();
foreach ($nwords as $nword) {
if ($nword)
$ncerca[$nword] = metaphone ($nword);
}
//se sono state specificate una o piu' parole chiave devo cercarle tutte...
foreach ($ncerca as $nword => $nmp) {
$max = strlen ($nmp) / 3.0;
$nfirst = strtolower(substr($nword, 0, 1));
$nlast = strtolower(substr($nword, -1));
$trovata = false;
// cerca una parola che coincida con nword. se la trova continua
// con la prossima, altrimenti ritorna false
foreach ($hcerca as $hayword => $hmp) {
$hfirst = strtolower(substr($hayword, 0, 1));
$hlast = strtolower(substr($hayword, -1));
// se la prima o l'ultima lettera sono uguali, prosegue con il
// controllo piu' complesso di somiglianza tra le parole (levenshtein)
if (($hfirst == $nfirst || $hlast == $nlast)
&& levenshtein ($hmp, $nmp) < $max) {
$trovata = true;
break;
}
}
if (!$trovata)
return false;
}
return true;
}