Case 2: Estimating object relevance

Implementation in pure PHP

Below is a runnable pure PHP example: dot product for features and weights with explicit result calculation.

 
<?php

require_once __DIR__ . '/code.php';

echo 
'Реализация на чистом PHP' . PHP_EOL;
echo 
'----------' . PHP_EOL;

$features = [10, 5, 2];
$weights = [0.3, 0.5, 1.5];

$score = dotProduct($features, $weights);

echo 
'features: [' . implode(', ', $features) . ']' . PHP_EOL;
echo 
'weights: [' . implode(', ', $weights) . ']' . PHP_EOL;
echo 
'score: ' . $score . PHP_EOL . PHP_EOL;
echo 
'Expected result: 8.5' . PHP_EOL;
echo 
'Explanation: 10 * 0.3 + 5 * 0.5 + 2 * 1.5 = 8.5' . PHP_EOL;
Result: Memory: 0.002 Mb Time running: < 0.001 sec.
Реализация на чистом PHP
----------
features: [10, 5, 2]
weights: [0.3, 0.5, 1.5]
score: 8.5

Expected result: 8.5
Explanation: 10 * 0.3 + 5 * 0.5 + 2 * 1.5 = 8.5

Takeaway: in pure PHP this calculation is easy to embed into ranking as a baseline relevance formula.