├── .gitignore ├── .scrutinizer.yml ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── Bratao │ └── PredictionIO │ ├── Facade.php │ ├── PredictionIO.php │ └── PredictionServiceProvider.php └── tests ├── Bratao └── Tests │ └── PredictionIO │ └── PredictionIOTest.php └── Endroid └── Tests └── PredictionIO └── PredictionIOTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | composer.phar 4 | phpunit.xml 5 | 6 | .idea/ -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | tools: 2 | php_cs_fixer: true 3 | php_code_sniffer: 4 | config: 5 | standard: PSR2 6 | php_mess_detector: true 7 | php_analyzer: false 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | 6 | before_script: 7 | - wget -nc http://getcomposer.org/composer.phar 8 | - php composer.phar install --dev 9 | 10 | script: phpunit -c phpunit.xml.dist 11 | 12 | notifications: 13 | email: 14 | - bruno.cabral@ufba.br 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Bruno Cabral 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel PredictionIO 2 | ==================== 3 | 4 | *Based on [endroid](http://endroid.nl/)* 5 | 6 | 7 | The Laravel PredictionIO library provides a client which offers easy access to a PredictionIO recommendation engine. 8 | PredictionIO is an open source machine learning server for software developers to create predictive features, such as 9 | personalization, recommendation and content discovery. 10 | 11 | Through a small set of simple calls, all server functionality is exposed to your application. You can add users and items, 12 | register actions between these users and items and retrieve recommendations deduced from this information by any 13 | [`PredictionIO`](http://prediction.io/) recommendation engine. Applications range from showing recommended products in a 14 | web shop to discovering relevant experts in a social collaboration network. 15 | 16 | 17 | ## Installation 18 | * Install library and dependencies: 19 | 20 | ```bash 21 | $ composer require "bratao/prediction-io:1.*@dev" 22 | ``` 23 | 24 | * Add a **provider** in `app/config/app.php`: 25 | 26 | ```php 27 | 'Bratao\PredictionIO\PredictionServiceProvider' 28 | ``` 29 | 30 | * Add an **alias** in `app/config/app.php`: 31 | 32 | ```php 33 | 'Prediction' => 'Bratao\PredictionIO\Facade', 34 | ``` 35 | 36 | * Define your [PredictionIO API endpoint](http://docs.prediction.io/current/tutorials/quickstart-php.html#add-your-app-to-predictionio) in `app/config/services.php`: 37 | 38 | ```php 39 | 'predictionio' => array( 40 | 'api' => 'http://localhost:8000/', 41 | 'key' => '0250b3f85ce33284f77c77f36b41010ef2c4fc5c', 42 | ), 43 | ``` 44 | ## Usage 45 | 46 | ```php 47 | =5.3.0", 17 | "predictionio/predictionio": "~0.7" 18 | }, 19 | "autoload": { 20 | "psr-0": { "Bratao": "src/" } 21 | }, 22 | "extra": { 23 | "branch-alias": { 24 | "dev-master": "1.x-dev" 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ./tests/Bratao/ 7 | 8 | 9 | 10 | 11 | ./src/Bratao/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Bratao/PredictionIO/Facade.php: -------------------------------------------------------------------------------- 1 | 5 | * 6 | * This source file is subject to the MIT license that is bundled 7 | * with this source code in the file LICENSE. 8 | */ 9 | 10 | namespace Bratao\PredictionIO; 11 | 12 | use Buzz\Browser; 13 | use Buzz\Client\MultiCurl; 14 | use PredictionIO\PredictionIOClient; 15 | 16 | class PredictionIO 17 | { 18 | /** 19 | * @var string 20 | */ 21 | protected $apiUrl = 'http://localhost:8000/'; 22 | 23 | /** 24 | * @var PredictionIOClient 25 | */ 26 | protected $client; 27 | 28 | /** 29 | * Class constructor 30 | * 31 | * @param $appKey 32 | * @param null $apiUrl 33 | */ 34 | public function __construct($appKey, $apiUrl = null) 35 | { 36 | $config = array('appkey' => $appKey); 37 | 38 | if ($apiUrl) { 39 | $config['apiurl'] = $apiUrl; 40 | } 41 | 42 | $this->client = PredictionIOClient::factory($config); 43 | } 44 | 45 | /** 46 | * Create a user. 47 | * 48 | * @param $userId 49 | * @return mixed 50 | */ 51 | public function createUser($userId) 52 | { 53 | $command = $this->client->getCommand('create_user', array('pio_uid' => $userId)); 54 | $response = $this->client->execute($command); 55 | 56 | return $response; 57 | } 58 | 59 | /** 60 | * Get a user 61 | * 62 | * @param $userID 63 | * @return mixed 64 | */ 65 | public function getUser($userId) 66 | { 67 | $commanbd = $this->client->getCommand('get_user', array('pio_uid' => $userId)); 68 | $response = $this->client->execute($command); 69 | 70 | return $response; 71 | } 72 | 73 | /** 74 | * Delete an user. 75 | * 76 | * @param $userId 77 | * @return mixed 78 | */ 79 | public function deleteUser($userId) 80 | { 81 | $command = $this->client->getCommand('delete_user', array('pio_iid' => $userId)); 82 | $response = $this->client->execute($command); 83 | 84 | return $response; 85 | } 86 | 87 | 88 | /** 89 | * Create an item. 90 | * 91 | * @param $itemId 92 | * @param int $itemTypes 93 | * @return mixed 94 | */ 95 | public function createItem($itemId, $itemTypes = 1) 96 | { 97 | $command = $this->client->getCommand('create_item', array('pio_iid' => $itemId, 'pio_itypes' => $itemTypes)); 98 | $response = $this->client->execute($command); 99 | 100 | return $response; 101 | } 102 | 103 | /** 104 | * Get an item. 105 | * 106 | * @param $itemId 107 | * @return mixed 108 | */ 109 | public function getItem($itemId) 110 | { 111 | $command = $this->client->getCommand('get_item', array('pio_iid' => $itemId)); 112 | $response = $this->client->execute($command); 113 | 114 | return $response; 115 | } 116 | 117 | /** 118 | * Delete an item. 119 | * 120 | * @param $itemId 121 | * @return mixed 122 | */ 123 | public function deleteItem($itemId) 124 | { 125 | $command = $this->client->getCommand('delete_item', array('pio_iid' => $itemId)); 126 | $response = $this->client->execute($command); 127 | 128 | return $response; 129 | } 130 | 131 | 132 | /** 133 | * Record a user action on an item. 134 | * 135 | * @param $userId 136 | * @param $itemId 137 | * @param string $action 138 | * @return mixed 139 | */ 140 | public function recordAction($userId, $itemId, $action = 'view') 141 | { 142 | $this->client->identify($userId); 143 | $response = $this->client->execute($this->client->getCommand('record_action_on_item', array('pio_action' => $action, 'pio_iid' => $itemId))); 144 | 145 | return $response; 146 | } 147 | 148 | /** 149 | * Returns the recommendations for the given user according to the engine. 150 | * 151 | * @param $userId 152 | * @param $engine 153 | * @param int $count 154 | * @return mixed 155 | */ 156 | public function getRecommendations($userId, $engine, $count = 3) 157 | { 158 | $this->client->identify($userId); 159 | $command = $this->client->getCommand('itemrec_get_top_n', array('pio_engine' => $engine, 'pio_n' => $count)); 160 | $response = $this->client->execute($command); 161 | 162 | return $response['pio_iids']; 163 | } 164 | 165 | /** 166 | * Returns the items similar to the given item according to the engine. 167 | * 168 | * @param $itemId 169 | * @param $engine 170 | * @param int $count 171 | * @return mixed 172 | */ 173 | public function getSimilarItems($itemId, $engine, $count = 3) 174 | { 175 | $command = $this->client->getCommand('itemsim_get_top_n', array('pio_iid' => $itemId, 'pio_engine' => $engine, 'pio_n' => $count)); 176 | $response = $this->client->execute($command); 177 | 178 | return $response['pio_iids']; 179 | } 180 | } -------------------------------------------------------------------------------- /src/Bratao/PredictionIO/PredictionServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('bratao/prediction-io'); 22 | } 23 | 24 | /** 25 | * Register to service provider 26 | * 27 | * @return [type] [description] 28 | */ 29 | public function register() 30 | { 31 | $this->app['predictionio'] = $this->app->share(function($app) 32 | { 33 | return new PredictionIO(\Config::get('services.predictionio.key'), \Config::get('services.predictionio.api')); 34 | }); 35 | } 36 | 37 | /** 38 | * Get the services provided by the provider. 39 | * 40 | * @return array 41 | */ 42 | public function provides() 43 | { 44 | return array('predictionio'); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Bratao/Tests/PredictionIO/PredictionIOTest.php: -------------------------------------------------------------------------------- 1 | 5 | * 6 | * This source file is subject to the MIT license that is bundled 7 | * with this source code in the file LICENSE. 8 | */ 9 | 10 | namespace Bratao\Tests\PredictionIO; 11 | 12 | use Bratao\PredictionIO\PredictionIO; 13 | 14 | class PredictionIOTest extends \PHPUnit_Framework_TestCase 15 | { 16 | public function testNoTestsYet() 17 | { 18 | $this->assertTrue(true); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Endroid/Tests/PredictionIO/PredictionIOTest.php: -------------------------------------------------------------------------------- 1 | 5 | * 6 | * This source file is subject to the MIT license that is bundled 7 | * with this source code in the file LICENSE. 8 | */ 9 | 10 | namespace Endroid\Tests\PredictionIO; 11 | 12 | use Endroid\PredictionIO\PredictionIO; 13 | 14 | class PredictionIOTest extends \PHPUnit_Framework_TestCase 15 | { 16 | public function testNoTestsYet() 17 | { 18 | $this->assertTrue(true); 19 | } 20 | } 21 | --------------------------------------------------------------------------------