You are here

class SlopeOneRecommender in Recommender API 6.2

Slopeone algorihtm. Doesn't support similarity calculation. Only support making predictions.

Hierarchy

Expanded class hierarchy of SlopeOneRecommender

File

./Recommender.php, line 691

View source
class SlopeOneRecommender extends Recommender {
  private $extention;
  protected function initialize() {
    $this->extension = isset($this->options['extension']) ? $this->options['extension'] : 'weighted';

    // could be 'weighted', 'bipolar'
  }
  public function computePrediction() {
    $this
      ->prepareData('database');
    $this
      ->computePredictionDatabase();
  }

  // TODO: this is almost directly copied from 1.x. Needs to make more readable
  protected function computePredictionDatabase() {

    // re-create the local variables from class variables.
    $app_id = $this->appId;
    $table_name = $this->tableName;
    $field_mouse = $this->fieldMouse;
    $field_cheese = $this->fieldCheese;
    $field_weight = $this->fieldWeight;
    $created = $this->created;
    $duplicate = $this->duplicate;
    db_query("DELETE FROM {recommender_slopeone_dev} WHERE app_id=%d", $app_id);

    // create dev(i,j)
    db_query("INSERT INTO {recommender_slopeone_dev}(app_id, cheese1_id, cheese2_id, count, dev)\n              SELECT %d, n1.{$field_cheese}, n2.{$field_cheese},\n              COUNT(*), AVG(n1.{$field_weight}-n2.{$field_weight}) FROM {{$table_name}} n1\n              INNER JOIN {{$table_name}} n2 ON n1.{$field_mouse}=n2.{$field_mouse}\n              AND n1.{$field_cheese} <> n2.{$field_cheese}\n              GROUP BY n1.{$field_cheese}, n2.{$field_cheese}", $app_id);

    // create P(u,j)
    if ($this->extension == 'basic') {
      $extension_sql = "AVG(t.{$field_weight}+p.dev)";
    }
    else {
      if ($this->extension == 'weighted') {

        // the 'weighted slope one'
        $extension_sql = "SUM((t.{$field_weight}+p.dev)*p.count)/SUM(p.count)";
      }
    }

    // haven't implemented the "bipolar" extension of Slope One.
    // generate predictions.
    db_query("INSERT INTO {recommender_prediction}(app_id, mouse_id, cheese_id, prediction, created)\n              SELECT %d, t.{$field_mouse}, p.cheese1_id, {$extension_sql}, %d\n              FROM {recommender_slopeone_dev} p INNER JOIN {{$table_name}} t ON p.cheese2_id=t.{$field_cheese}\n              GROUP BY t.{$field_mouse}, p.cheese1_id", $app_id, $created);

    // remove duplicate prediction
    if ($duplicate == 'remove') {
      db_query("DELETE FROM {recommender_prediction} WHERE app_id=%d AND created=%d AND (mouse_id, cheese_id)\n                IN (SELECT {$field_mouse}, {$field_cheese} FROM {{$table_name}})", $app_id, $created);
    }
    $this
      ->purgeOutdatedRecords('prediction');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Recommender::$appId protected property
Recommender::$appName protected property
Recommender::$cheeseMap protected property
Recommender::$cheeseNum protected property
Recommender::$created protected property
Recommender::$directMatrix protected property
Recommender::$duplicate protected property
Recommender::$fieldCheese protected property
Recommender::$fieldMouse protected property
Recommender::$fieldWeight protected property
Recommender::$missing protected property
Recommender::$mouseMap protected property
Recommender::$mouseNum protected property
Recommender::$options protected property
Recommender::$performance protected property
Recommender::$predictionMatrix protected property
Recommender::$similarityMatrix protected property
Recommender::$tableName protected property
Recommender::batchInsert protected function
Recommender::cleanupMemory protected function
Recommender::computePredictionJava protected function
Recommender::computePredictionMemory protected function 1
Recommender::computeSimilarity public function 1
Recommender::computeSimilarityDatabase protected function 1
Recommender::computeSimilarityJava protected function
Recommender::computeSimilarityMemory protected function 1
Recommender::convertAppId static function
Recommender::getAppId public function
Recommender::getCheeseNum protected function
Recommender::getEntityNum protected function
Recommender::getMouseNum protected function
Recommender::loadDirectMatrix protected function Load matrix from the database into a matrix class in memory
Recommender::loadSimilarityMatrix protected function
Recommender::prepareData protected function After calling this function, data would be ready to process. Could be: 1) if it's in database, then $->tableName, $this->$field* would store the correct info. 2) if it's in memory, then $this->directMatrix will be the matrix
Recommender::processTable protected function
Recommender::purgeApp static function
Recommender::purgeOutdatedRecords protected function
Recommender::retrievePrediction public function
Recommender::retrieveSimilarity public function Return the similarity between $mouse1 and $mouse2.
Recommender::saveSimilarityMatrix protected function
Recommender::topPrediction public function
Recommender::topSimilarity public function
Recommender::__construct function 1
SlopeOneRecommender::$extention private property
SlopeOneRecommender::computePrediction public function Overrides Recommender::computePrediction
SlopeOneRecommender::computePredictionDatabase protected function Overrides Recommender::computePredictionDatabase
SlopeOneRecommender::initialize protected function Overrides Recommender::initialize