You are here

protected function CFRecommender::saveMatrix in Recommender API 7.6

2 calls to CFRecommender::saveMatrix()
CFRecommender::finalize in classes/Recommender.php
Save data to database, etc.
ItemBasedRecommender::finalize in classes/Recommender.php
Save data to database, etc.

File

classes/Recommender.php, line 154

Class

CFRecommender
This is the classical collaborative filtering implementation.

Code

protected function saveMatrix(&$map1, &$map2, &$result_matrix, $table_name, $source_field, $target_field, $score_field, $timestamp_field, $skip_self = FALSE) {
  $r_map1 = array_flip($map1);
  $r_map2 = array_flip($map2);
  $values = $result_matrix
    ->raw_values();
  $insert = db_insert($table_name)
    ->fields(array(
    $source_field,
    $target_field,
    $score_field,
    $timestamp_field,
  ));
  foreach ($r_map1 as $v1 => $entity1) {
    foreach ($r_map2 as $v2 => $entity2) {
      if (!isset($values[$v1][$v2])) {
        continue;
      }

      // we might skip if it's undefined.
      if ($skip_self && $entity1 == $entity2) {
        continue;
      }

      // for similarity data, we want to skip self.
      $score = $values[$v1][$v2];
      if (!is_nan($score)) {
        $insert
          ->values(array(
          $source_field => $entity1,
          $target_field => $entity2,
          $score_field => $score,
          $timestamp_field => $this->timestamp,
        ));
      }

      // end of if (score)
    }

    // end of for($v2)
  }

  // end of for($v1)
  $insert
    ->execute();
}