Skip to content

Instantly share code, notes, and snippets.

@phpcodertop
Last active February 24, 2018 06:31
Show Gist options
  • Save phpcodertop/2691c6213efcc71c5b5a034f7640d75c to your computer and use it in GitHub Desktop.
Save phpcodertop/2691c6213efcc71c5b5a034f7640d75c to your computer and use it in GitHub Desktop.
Item-item collaborative filtering
<?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