Esempio n. 1
0
 /**
  * Return a list of recommendations.
  *
  * @param string $for      - the item we want recommendations for
  * @param object $strategy
  *
  * @return array - return a list of identifier ordered by closest
  */
 public function recommend($for, $strategy = null)
 {
     if ($strategy === null) {
         $strategy = new Cosine();
     }
     $distances = [];
     foreach ($this->set as $key => $itemData) {
         if ($key == $for) {
             continue;
         }
         $distance = $strategy->run($itemData, $this->set[$for]);
         if ($distance === false) {
             continue;
         }
         $distances[$key] = ['key' => $key, 'value' => $distance];
     }
     if (!count($distances)) {
         return false;
     }
     $this->sort($distances, true);
     $data = [];
     foreach ($distances as $article) {
         if ($article['value'] == 0) {
             continue;
         }
         $data[] = $article['key'];
     }
     return $data;
 }
Esempio n. 2
0
 public function testCosinSimilarity()
 {
     $cosin = new Cosine();
     $users = json_decode(file_get_contents(__DIR__ . '/../fixtures/perfect_users.json'), true);
     $score = $cosin->run($users['Clara'], $users['Robert']);
     $this->assertEquals(0.064846541429478, $score);
 }