Skip to content

Commit

Permalink
If no ratings are common, dont recommend
Browse files Browse the repository at this point in the history
  • Loading branch information
Stig Lindqvist committed Dec 10, 2013
1 parent bf02834 commit abbee7f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
>
<testsuites>
<testsuite name="Default">
<directory>./Tests/</directory>
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<directory>./src</directory>
<exclude>
<directory>./Tests</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
Expand Down
6 changes: 6 additions & 0 deletions src/stojg/datamine/Recommender.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ protected function findNearest($strategy)
continue;
}
$distance = $strategy->run($itemData, $this->set[$this->item]);
if($distance === false) {
continue;
}
$distances[] = array('key' => $key, 'value' => $distance);
}
if(!count($distances)) {
return false;
}
$this->sort($distances, true);
return $distances[0]['key'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/stojg/datamine/strategy/Minkowski.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public function run($rating1, $rating2)
if ($commonRatings) {
return pow($distance, 1 / $this->r);
}
return 0;
return false;
}
}
18 changes: 13 additions & 5 deletions tests/RecommenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ public function testRecommendPaersonHailey()
$base = new Recommender('Hailey', $this->set);
$recommendations = $base->recommend(new Paerson());
$this->assertEquals(3, count($recommendations));
$this->assertEquals('Phoenix', $recommendations[0]['key']);
$this->assertEquals(4.0, $recommendations[0]['value']);
$this->assertEquals('Blues Traveler', $recommendations[1]['key']);
$this->assertEquals(3.0, $recommendations[1]['value']);
$this->assertEquals('Blues Traveler', $recommendations[0]['key']);
$this->assertEquals(5.0, $recommendations[0]['value']);
$this->assertEquals('Phoenix', $recommendations[1]['key']);
$this->assertEquals(5.0, $recommendations[1]['value']);
$this->assertEquals('Slightly Stoopid', $recommendations[2]['key']);
$this->assertEquals(2.5, $recommendations[2]['value']);
$this->assertEquals(4.0, $recommendations[2]['value']);
}

public function testRecommendPaersonNoMatch()
{
$set = json_decode(file_get_contents(__DIR__ . '/fixtures/users_nomatch.json'), true);
$base = new Recommender('Andrea', $set);
$recommendations = $base->recommend(new Paerson());
$this->assertEquals(0, count($recommendations));
}

public function testRecommendCosineHailey()
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/users_nomatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Andrea": {
"Blues Traveler": 1.0,
"Broken Bells": 2.0,
"Norah Jones": 3.0,
"The Strokes": 4.0
},
"Bob": {
"Deadmau5": 1.0,
"Phoenix": 2.0,
"Slightly Stoopid": 3.0,
"Vampire Weekend": 4.0
}
}
8 changes: 8 additions & 0 deletions tests/strategy/MinkowskiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public function testMinkowskiAngelicaHailey()
$score = $paerson->run($this->set['Angelica'], $this->set['Hailey']);
$this->assertEquals(2.7386127875258, $score);
}

public function testMinkowskiNoMatch()
{
$set = json_decode(file_get_contents(__DIR__ . '/../fixtures/users_nomatch.json'), true);
$paerson = new Minkowski(2);
$score = $paerson->run($set['Andrea'], $set['Bob']);
$this->assertFalse($score);
}
}

0 comments on commit abbee7f

Please sign in to comment.