class SlopeOneRecommender in Recommender API 6.2
Slopeone algorihtm. Doesn't support similarity calculation. Only support making predictions.
Hierarchy
- class \Recommender
- class \SlopeOneRecommender
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');
}
}