note 97909 added to function.similar-text
| From: | xandrani at osu1 dot php dot net | Date: | Sat, 15 May 2010 08:35:47 +0000 |
| Subject: | note 97909 added to function.similar-text | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169272@lists.php.net to get a copy of this message | ||
@georgesk at hotmail dot com [09-Mar-2002 04:14]
1) Your functions didn't deal with strings longer than 128 (mine now sets up the arrays
dynamically so the strings can be any length).
2) It does NOT return the same values as similar_text() as you claim (I have added debug code in the
similar_text2() function to show the differences).
3) Fixed bug... the for ($i = 1; $i < $m; $i++) should have read for ($i = 1; $i <= $m;
$i++) [notice then less than equal rather than just less than].
<?php
function LCS_Length($s1, $s2)
{
$m = strlen($s1);
$n = strlen($s2);
$LCS_Length_Table = array();
//reset the 2 cols in the table
for ($i = 1; $i <= $m; $i++)
{
$LCS_Length_Table[$i] = array();
$LCS_Length_Table[$i][0] = 0;
}
$LCS_Length_Table[0] = array();
for ($j = 0; $j <= $n; $j++)
{
$LCS_Length_Table[0][$j] = 0;
}
for ($i = 1; $i <= $m; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
if (!isset($LCS_Length_Table[$i]))
{
$LCS_Length_Table[$i] = array();
}
if (!isset($LCS_Length_Table[$i - 1]))
{
$LCS_Length_Table[$i - 1] = array();
}
if ($s1[$i-1] == $s2[$j-1])
{
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i-1][$j-1] + 1;
}
else if ($LCS_Length_Table[$i-1][$j] >= $LCS_Length_Table[$i][$j-1])
{
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i-1][$j];
}
else
{
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i][$j-1];
}
}
}
return $LCS_Length_Table[$m][$n];
}
function similar_text2($s1, $s2, &$similarity2)
{
$s1 = strtolower($s1);
$s2 = strtolower($s2);
$lcs = LCS_Length($s1,$s2); //longest common sub sequence
$ms = (strlen($s1) + strlen($s2)) / 2;
similar_text($s1, $s2, $similarity);
$similarity2 = (($lcs * 100) / $ms);
echo "similar_text() = $similarity - similar_text2() = $similarity2<br>";
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 86.128.232.70
----
Manual Page -- http://www.php.net/manual/en/function.similar-text.php
Edit -- https://master.php.net/note/edit/97909
Del: integrated -- https://master.php.net/note/delete/97909/integrated
Del: useless -- https://master.php.net/note/delete/97909/useless
Del: bad code -- https://master.php.net/note/delete/97909/bad+code
Del: spam -- https://master.php.net/note/delete/97909/spam
Del: non-english -- https://master.php.net/note/delete/97909/non-english
Del: in docs -- https://master.php.net/note/delete/97909/in+docs
Del: other reasons-- https://master.php.net/note/delete/97909
Reject -- https://master.php.net/note/reject/97909
Search -- https://master.php.net/manage/user-notes.php