Minkowski   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 16 4
1
<?php
2
3
namespace stojg\recommend\strategy;
4
5
/**
6
 * Computes the Minkowski distance.
7
 * 
8
 * If the data is dense (almost all attributes have a non
9
 * zero value) and the magnitude of the attributes values
10
 * are important, this is a good similarity comparisator
11
 */
12
class Minkowski
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $r = 1;
18
19
    /**
20
     * @param int $r
21
     */
22
    public function __construct($r = 1)
23
    {
24
        $this->r = $r;
25
    }
26
27
    /**
28
     * Both rating1 and rating2 are an array of the form
29
     * ['The Strokes'=> 3.0, 'Slightly Stoopid' =>  2.5, ...].
30
     * 
31
     * @param array $rating1
32
     * @param array $rating2
33
     */
34
    public function run($rating1, $rating2)
35
    {
36
        $distance = 0;
37
        $commonRatings = false;
38
        foreach ($rating1 as $key => $rating) {
39
            if (isset($rating2[$key])) {
40
                $distance += pow(abs($rating - $rating2[$key]), $this->r);
41
                $commonRatings = true;
42
            }
43
        }
44
        if ($commonRatings) {
45
            return pow($distance, 1 / $this->r);
46
        }
47
48
        return false;
49
    }
50
}
51