You are here

protected function Recommender::loadDirectMatrix in Recommender API 6.2

Load matrix from the database into a matrix class in memory

Return value

unknown_type

1 call to Recommender::loadDirectMatrix()
Recommender::prepareData in ./Recommender.php
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

File

./Recommender.php, line 134

Class

Recommender
The super class for all other Recommender algorithms.

Code

protected function loadDirectMatrix($sparse = FALSE) {

  // retrieve value from the database. setup program.
  watchdog('recommender', "Please be patient while loading data into memory. This step may fail if you don't have enough memory");
  if (stripos($this->tableName, 'SELECT ') === 0) {
    $sql = $this->tableName;

    // if $tableName is a SQL query, we'll just load it into memory w/o making it to
  }
  else {

    // Note: (fieldMouse, fieldCheese) should be unique key, thus we shouldn't use SUM (which is only for fault tolerance).
    $sql = "SELECT {$this->fieldMouse}, {$this->fieldCheese}, SUM({$this->fieldWeight}) {$this->fieldWeight}\n              FROM {{$this->tableName}} GROUP BY {$this->fieldMouse}, {$this->fieldCheese}";
  }
  $result = db_query($sql);
  $type = $sparse ? 'SparseMatrix' : 'RealMatrix';

  // create the matrix, might fail if not enough memory.
  $this->directMatrix = Matrix::create($type, $this
    ->getMouseNum(), $this
    ->getCheeseNum());
  $this->mouseMap = array();
  $this->cheeseMap = array();

  // build the matrix
  while ($line = db_fetch_array($result)) {
    $id_mouse = $line[$this->fieldMouse];
    $id_cheese = $line[$this->fieldCheese];
    $weight = $line[$this->fieldWeight];
    if (!array_key_exists($id_mouse, $this->mouseMap)) {
      $this->mouseMap[$id_mouse] = count($this->mouseMap);
    }
    if (!array_key_exists($id_cheese, $this->cheeseMap)) {
      $this->cheeseMap[$id_cheese] = count($this->cheeseMap);
    }
    $this->directMatrix
      ->set($this->mouseMap[$id_mouse], $this->cheeseMap[$id_cheese], $weight);
  }
}