note 97909 deleted from function.similar-text by danbrown
| From: | danbrown@php.net | Date: | Sat, 15 May 2010 12:15:07 +0000 |
| Subject: | note 97909 deleted from function.similar-text by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169275@lists.php.net to get a copy of this message | ||
Note Submitter: xandrani
----
@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>";
}
?>