Esempio n. 1
0
 /**
  * Class constructor
  *
  * @param $appKey
  * @param null $apiUrl
  */
 public function __construct($appKey, $apiUrl = null)
 {
     $config = array('appkey' => $appKey);
     if ($apiUrl) {
         $config['apiurl'] = $apiUrl;
     }
     $this->client = PredictionIOClient::factory($config);
 }
 public function testFactoryWithCustomConfigSuccess()
 {
     $config = array('appkey' => 'mock.appkey', 'customconfig' => 'mock.option');
     $client = PredictionIOClient::factory($config);
     $this->assertSame($client->getConfig('customconfig'), $client->getConfig('customconfig'));
 }
if (!isset($argv[1]) || !isset($argv[2])) {
    print "Usage: {$argv['0']} <appkey> <data file>\n";
    exit(1);
}
$appkey = $argv[1];
$input = $argv[2];
if (!file_exists($input)) {
    print "{$input} not found!\n";
    exit(1);
}
if (!is_readable($input)) {
    print "{$input} cannot be read!\n";
    exit(1);
}
// Instantiate a client
$client = PredictionIOClient::factory(array("appkey" => $appkey));
// Scan sample data file and import ratings
$handle = fopen($input, "r");
while (!feof($handle)) {
    $line = fgets($handle);
    $tuple = explode("\t", $line);
    if (count($tuple) == 3) {
        try {
            $client->identify($tuple[0]);
            $client->execute($client->getCommand("record_action_on_item", array("pio_action" => "rate", "pio_iid" => $tuple[1], "pio_rate" => intval($tuple[2]))));
        } catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
            print $e->getResponse()->getBody() . "\n\n";
        }
    }
}
fclose($handle);
Esempio n. 4
0
<?php

// web/index.php
require_once __DIR__ . '/../vendor/autoload.php';
use PredictionIO\PredictionIOClient;
use Symfony\Component\HttpFoundation\Request;
$client = PredictionIOClient::factory(['apiurl' => 'http://192.168.33.20:8000', 'appkey' => 'fXcxZlrBYZUxzd6wgeZhIQruai8OfUuUDaQGQyZVeigdfn4gQv48A3Q4Dml5Jfpq']);
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views'));
$app->get('/', function () use($app) {
    return $app['twig']->render('index.html.twig', []);
});
$app->post('/user', function (Request $request) use($app, $client) {
    $command = $client->getCommand('create_user', ['pio_uid' => $request->get('user')]);
    $response = $client->execute($command);
    return $app->json(['message' => sprintf('Created user "%s".', $request->get('user'))]);
});
$app->post('/show', function (Request $request) use($app, $client) {
    $command = $client->getCommand('create_item', ['pio_iid' => $request->get('show'), 'pio_itypes' => 1]);
    $response = $client->execute($command);
    $client->identify($request->get('user'));
    $command = $client->getCommand('record_action_on_item', ['pio_action' => 'like', 'pio_iid' => $request->get('show')]);
    $client->execute($command);
    return $app->json(['message' => sprintf('You liked %s', $request->get('show'))]);
});
$app->post('/recommend', function (Request $request) use($app, $client) {
    try {
        $client->identify($request->get('user'));
        $command = $client->getCommand('itemrec_get_top_n', ['pio_engine' => 'itemrec', 'pio_n' => 5]);
        $rec = $client->execute($command);
 public function setupClient(Model $model, $client = null)
 {
     $this->_client = $client === null ? \PredictionIO\PredictionIOClient::factory(array('appkey' => Configure::read('predictionIO.appkey'), 'apiurl' => Configure::read('predictionIO.apiurl'))) : $client;
 }