Created
July 14, 2014 08:45
-
-
Save tomfotherby/ed798af37ef1c6dd725d to your computer and use it in GitHub Desktop.
PPH PredictionIO import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PredictionIO\PredictionIOClient; | |
class ReengagementHourlieCommand extends ConsoleCommand | |
{ | |
private $_client; | |
public function init() | |
{ | |
parent::init(); | |
$this->_client = PredictionIOClient::factory(["appkey" => "T9iitaXRTWNOBEZzd0PiCAkaTuGn9XDr8Qdp6Mdy0vCoKoUyzdObMrCcI8yi0Zca"]); | |
} | |
/** | |
* Import data to the Prediction.io service | |
* | |
* Usage: | |
* ./yiic ReengagementHourlie import | |
*/ | |
public function actionImport() | |
{ | |
$this->actionImportMembers(); | |
$this->actionImportHourlies(); | |
$this->actionImportViews(); | |
} | |
/** | |
* Tell Prediction.io about all our members - in their terminology, users | |
*/ | |
public function actionImportMembers() | |
{ | |
$page = 0; | |
while(true) { | |
$dataProvider = new CActiveDataProvider("Members", array( | |
"criteria" => array( | |
"select" => "mem_id", | |
"scopes" => array("active"), | |
), | |
"pagination" => array( | |
"pageSize" => 2000, | |
"currentPage" => $page, | |
) | |
) | |
); | |
foreach($dataProvider->data as $model) { | |
echo "Add user ". $model->mem_id . "\n"; | |
$command = $this->_client->getCommand('create_user', array('pio_uid' => $model->mem_id)); | |
$response = $this->_client->execute($command); | |
} | |
if ($page + 1 >= $dataProvider->pagination->pageCount) { | |
break; | |
} | |
$page++; | |
} | |
} | |
/** | |
* Tell Prediction.io about all our Hourlies - in their terminology, items | |
*/ | |
public function actionImportHourlies() | |
{ | |
$page = 0; | |
while(true) { | |
$dataProvider = new CActiveDataProvider("Hourlie", array( | |
"criteria" => array( | |
"select" => "id", | |
"scopes" => array("active","visible"), | |
), | |
"pagination" => array( | |
"pageSize" => 2000, | |
"currentPage" => $page, | |
) | |
) | |
); | |
foreach($dataProvider->data as $model) { | |
echo "Add item ". $model->id . "\n"; | |
// TODO - set pio_itypes, see http://docs.prediction.io/current/apis/item.html | |
$command = $this->_client->getCommand('create_item', array('pio_iid' => $model->id, 'pio_itypes' => 'hourlies')); | |
$response = $this->_client->execute($command); | |
} | |
if ($page + 1 >= $dataProvider->pagination->pageCount) { | |
break; | |
} | |
$page++; | |
} | |
} | |
/** | |
* | |
*/ | |
public function actionImportViews() | |
{ | |
$page = 0; | |
while(true) { | |
$dataProvider = new EMongoDocumentDataProvider('MHourlieLogDocument', array( | |
"criteria" => array( | |
'conditions'=>array('memId'=>array('>' => 0)), | |
), | |
"pagination" => array( | |
"pageSize" => 2000, | |
"currentPage" => $page, | |
) | |
)); | |
foreach($dataProvider->data as $model) { | |
echo "User ". $model->memId . " views item ". $model->hourlieId ."\n"; | |
try { | |
$this->_client->identify($model->memId); | |
$command = $this->_client->getCommand('record_action_on_item', ['pio_action' => 'view', 'pio_iid' => $model->hourlieId]); | |
$response = $this->_client->execute($command); | |
} catch (PredictionIO\UnidentifiedUserException $e) { | |
echo "Don't know about User ".$model->memId."\n"; | |
} | |
} | |
if ($page + 1 >= $dataProvider->pagination->pageCount) { | |
break; | |
} | |
$page++; | |
} | |
} | |
/** | |
* | |
* Usage: | |
* ./yiic ReengagementHourlie show --u=199099 | |
*/ | |
public function actionShow($u=199099,$engine='engine1') | |
{ | |
echo "Retrieve top 5 recommendations for user ". $u . "\n"; | |
try { | |
// Identify the user ID that will be used by all subsequent actions | |
$this->_client->identify($u); | |
$rec = $this->_client->execute($this->_client->getCommand('itemrec_get_top_n', array('pio_engine' => $engine, 'pio_n' => 15))); | |
print_r($rec); | |
} catch (Exception $e) { | |
echo 'Caught exception: ', $e->getMessage(), "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment