Example #1
0
function euclidean($x, $y)
{
    if ($y == 0) {
        return $x;
    }
    return euclidean($y, (int) $x % $y);
}
// Convert feature vector string to array of integers
$feature_array = array();
for ($i = 0; $i < strlen($feature); $i++) {
    $feature_array[$i] = (int) $feature[$i];
}
// echo 'Feature Vector (Array) <br>';
// print_r($feature_array);
// echo '<br> Feature Vector Array Length<br>';
// echo count($feature_array).'<br>';
// Create best array --> For Comparison
$best_array = array();
for ($i = 0; $i < strlen($feature); $i++) {
    $best_array[$i] = 5;
}
// Calculate distance between feature vector and best vector based on Euclidean and Cosine similarity distance
$edist = euclidean($feature_array, $best_array);
$cdist = cosinus($feature_array, $best_array);
// echo '<br>-----------------------------------------------------------------------------------------------------<br>';
// echo '<br> Euclidean Distance (for ranking): '.$edist.'<br>';
// echo '<br> Cosine Similarity Distance (for ranking): '.$cdist.'<br>';
echo '<div style=" width:100%; height:45px; padding:10px; margin:5px -10px; background-color:#1B5FA3; color:white; font-size:20px;">Step 3: Rank based on cosine similarity score <br> Cosine similarity score : ' . $cdist . ' </div>';
echo '</div>';
// Function Implementation of Euclidean Distance and Cosine Similarity Distance
/**
 * Euclidean distance
 * d(a, b) = sqrt( summation{i=1,n}((b[i] - a[i]) ^ 2) )
 *
 * @param array $a
 * @param array $b
 * @return boolean
 */