Last active
February 24, 2018 06:31
-
-
Save phpcodertop/2691c6213efcc71c5b5a034f7640d75c to your computer and use it in GitHub Desktop.
Item-item collaborative filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// code by Mahmoud Gamal | |
function similarity($str1, $str2) | |
{ | |
$str2 = strtoupper($str2); | |
$str1 = strtoupper($str1); | |
$len1 = strlen($str1); | |
$len2 = strlen($str2); | |
$max = max($len1, $len2); | |
$similarity = $i = $j = 0; | |
while (($i < $len1) && isset($str2[$j])) { | |
if ($str1[$i] == $str2[$j]) { | |
$similarity++; | |
$i++; | |
$j++; | |
} elseif ($len1 < $len2) { | |
$len1++; | |
$j++; | |
} elseif ($len1 > $len2) { | |
$i++; | |
$len1--; | |
} else { | |
$i++; | |
$j++; | |
} | |
} | |
return round($similarity / $max, 2); | |
} | |
$str1 = 'Ram 2 Giga core 2due cpu 3'; | |
$str2 = 'ram 3 g core i5 cpu 2.5'; | |
echo 'Similarity: ' . (similarity($str1, $str2) * 100) . '%'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment