note 85378 added to function.levenshtein
| From: | paulroweatinamedotcom at osu1 dot php dot net | Date: | Wed, 27 Aug 2008 21:14:04 +0000 |
| Subject: | note 85378 added to function.levenshtein | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143896@lists.php.net to get a copy of this message | ||
I just noticed that there is a flaw in the logic.
That is, the code that says:
<?php
$nsDistance[$nRightPos] = min($nsDistance[$nRightPos], $nsDistance[$nRightPos - 1], $nDiagonal) +
$nCost;
?>
should say:
<?php
$nsDistance[$nRightPos] = min($nsDistance[$nRightPos] + 1, $nsDistance[$nRightPos - 1] + 1,
$nDiagonal + $nCost);
?>
Also, you can reduce memory usage by first checking for the shorter string and using that as the
"right-hand" string (on which the size of the array is based). I have tested this
function with two 532-character strings and it completed in 0.6-0.8 seconds.
----
Server IP: 208.69.120.35
Probable Submitter: 69.9.134.158
----
Manual Page -- http://www.php.net/manual/en/function.levenshtein.php
Edit -- https://master.php.net/note/edit/85378
Del: integrated -- https://master.php.net/note/delete/85378/integrated
Del: useless -- https://master.php.net/note/delete/85378/useless
Del: bad code -- https://master.php.net/note/delete/85378/bad+code
Del: spam -- https://master.php.net/note/delete/85378/spam
Del: non-english -- https://master.php.net/note/delete/85378/non-english
Del: in docs -- https://master.php.net/note/delete/85378/in+docs
Del: other reasons-- https://master.php.net/note/delete/85378
Reject -- https://master.php.net/note/reject/85378
Search -- https://master.php.net/manage/user-notes.php