Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Levenshtein Percentages

Can you explain why I need to use both the input string and the matching string when determining the levenshtein percentage?

$str1len = strlen($str1);        
$str2len = strlen($str2);    
if($str1len < $str2len){    
    $pct = ($str1len - $lev) / $str1len;    
} else {    
    $pct = ($str2len - $lev) / $str2len;    
}   
$pct = $pct * 100;      
like image 882
somejkuser Avatar asked Dec 08 '25 12:12

somejkuser


1 Answers

Because it's a percentage. You need to compare one number against another.

A levenshtein distance is the number of single character changes between two strings needed to change the first string into the second string. The percentage is how many of the original characters got changed. e.g. a lev. distance of 2 on a 10 character word (20%) is a smaller percentage than a lev. distance of 2 on a 2 character word (100%), even though both lev. distances are the same.

like image 64
Marc B Avatar answered Dec 11 '25 02:12

Marc B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!