note 12947 rejected and deleted from function.similar-text.php by zak
| From: | zak@php.net | Date: | Fri, 27 Jul 2001 08:03:28 +0000 |
| Subject: | note 12947 rejected and deleted from function.similar-text.php by zak | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15908@lists.php.net to get a copy of this message | ||
Using similar_text(), I did a comparison of 634,700 english words against one misspelled word,
"stuped", and the closest that it came up with was "district superintendent".
Bear in mind that this vocabulary file DID in fact contain the word "stupid", but
similar_text() evaluated the phrase "district superintendent" as a closer match.
For another string comparison algorithm, try levenshtein()
http://www.php.net/manual/en/function.levenshtein.php
In my experience with the two functions, levenshtein() is more accurate, and the processing time
overhead isn't noticeably different between the two.
Below is the code that I used to benchmark the similar_text() function.
function spellCheck2($inputstring,$mindistance) {
$inputstring = str_replace(","," ",$inputstring);
$trial = explode(" ",$inputstring);
$vocab = file("./vocabulary.txt");
$acc=0;
foreach ($trial as $word) {
$recordmatch = 0;
$recordmatchtext = "";
foreach ($vocab as $testword) {
$rating = similar_text($word,$testword);
if ($rating > $recordmatch) {
$recordmatch = $rating;
$recordmatchtext = $testword;
}
}
if ($recordmatch < $mindistance) {
return($recordmatch);
} else {
return($recordmatchtext);
}
}
}