You are here

function RecommenderTestCase::testCooccurrenceRecommender in Recommender API 6.2

File

./recommender.test, line 174

Class

RecommenderTestCase

Code

function testCooccurrenceRecommender() {

  // test the simple case w/ weight
  $rec1 = new CooccurrenceRecommender('recommender_cooccur_1', 'recommender_grouplens', 'user_id', 'item_id', 'score');
  $rec1
    ->computeSimilarity();
  $this
    ->assertEqual(12, 12);
  $this
    ->assertEqual(12, $rec1
    ->retrieveSimilarity(1, 2));
  $this
    ->assertEqual(12, $rec1
    ->retrieveSimilarity(2, 1));
  $this
    ->assertEqual(6, $rec1
    ->retrieveSimilarity(3, 1));
  $this
    ->assertEqual(9.5, $rec1
    ->retrieveSimilarity(2, 3));
  $this
    ->assertTrue(is_nan($rec1
    ->retrieveSimilarity(6, 2)));

  // test just the # of co-occurs with SQL query
  $sql = "SELECT user_id, item_id, 1 as score FROM {recommender_grouplens}";
  $rec2 = new CooccurrenceRecommender('recommender_cooccur_2', $sql, 'user_id', 'item_id', 'score');
  $rec2
    ->computeSimilarity();
  $this
    ->assertEqual(4, $rec2
    ->retrieveSimilarity(1, 2));
  $this
    ->assertEqual(4, $rec2
    ->retrieveSimilarity(2, 1));
  $this
    ->assertEqual(3, $rec2
    ->retrieveSimilarity(2, 3));
  $this
    ->assertTrue(is_nan($rec2
    ->retrieveSimilarity(5, 10)));

  // test topSimilarity/topPrediction
  $items = $rec2
    ->topSimilarity(1, 5);
  $this
    ->assertEqual(3, count($items));
  $this
    ->assertEqual(2, $items[0]['id']);
  $this
    ->assertEqual(3, $items[2]['id']);
  function test($item) {
    return $item['score'] >= 3 ? FALSE : TRUE;
  }
  $items = $rec2
    ->topSimilarity(1, 1, 'test');
  $this
    ->assertEqual(1, count($items));
  $this
    ->assertEqual(3, $items[0]['id']);
  recommender_purge_app('recommender_cooccur_1');
  recommender_purge_app('recommender_cooccur_2');
}