You are here

function recommender_similarity_coocurrences in Recommender API 5

Same name and namespace in other branches
  1. 6 recommender.module \recommender_similarity_coocurrences()
  2. 6.2 recommender.module \recommender_similarity_coocurrences()

Co-ocurrences algorithm that compute similarity among mice based on how many cheese they share.

Parameters

$app_name the application name that uses this function.:

$table_name the input table name:

$field_mouse the input table field for mouse:

$field_cheese the input table field for cheese:

$options an array of options: 'incremental': whether to rebuild the whole similarity matrix, or incrementally update those got changed. 'rebuild' or 'update'

Return value

null {recommender_similarity} will be filled with similarity data

File

./recommender.module, line 396
Providing generic recommender system algorithms.

Code

function recommender_similarity_coocurrences($app_name, $table_name, $field_mouse, $field_cheese, $options = array()) {

  // get param values
  $app_id = recommender_get_app_id($app_name);
  $created = time();
  $incremental = isset($options['incremental']) ? $options['incremental'] : 'rebuild';

  // the basic skeleton sql
  $basic_sql = "INSERT INTO {recommender_similarity}(app_id, mouse1_id, mouse2_id, similarity, created)\n                SELECT {$app_id}, n1.{$field_mouse}, n2.{$field_mouse}, COUNT(n1.{$field_cheese}), {$created}\n                FROM {{$table_name}} n1 INNER JOIN {{$table_name}} n2 ON n1.{$field_cheese}=n2.{$field_cheese}\n                GROUP BY n1.{$field_mouse}, n2.{$field_mouse}";
  switch ($incremental) {

    // rebuild the similarity index
    case 'rebuild':
    default:
      db_query($basic_sql);
      db_query("DELETE FROM {recommender_updated_queue} WHERE app_id=%d AND created<=%d", $app_id, $created);
      db_query("DELETE FROM {recommender_similarity} WHERE app_id=%d AND created<>%d", $app_id, $created);
      break;

    // incrementally update. might incur more overhead than 'rebuild'
    case 'update':
      $max_timestamp = _recommender_updated_max($app_id);
      if ($max_timestamp == NULL) {
        return;
      }
      $ids = _recommender_updated_list($app_id, $max_timestamp, 'mouse_id');
      db_query("DELETE FROM {recommender_similarity} WHERE app_id=%d AND (mouse1_id IN {$ids} OR mouse2_id IN {$ids})", $app_id);
      db_query($basic_sql . " HAVING (n1.{$field_mouse} IN {$ids} OR n2.{$field_mouse} IN {$ids})");
      _recommender_updated_markdone($app_id, $max_timestamp);
      break;
  }
}