1. Go to this page and download the library: Download graphaware/reco4php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
graphaware / reco4php example snippets
namespace GraphAware\Reco4PHP\Tests\Example\Discovery;
use GraphAware\Common\Cypher\Statement;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Context\Context;
use GraphAware\Reco4PHP\Engine\SingleDiscoveryEngine;
class RatedByOthers extends SingleDiscoveryEngine
{
public function discoveryQuery(Node $input, Context $context)
{
$query = 'MATCH (input:User) WHERE id(input) = {id}
MATCH (input)-[:RATED]->(m)<-[:RATED]-(o)
WITH distinct o
MATCH (o)-[:RATED]->(reco)
RETURN distinct reco LIMIT 500';
return Statement::create($query, ['id' => $input->identity()]);
}
public function name()
{
return "rated_by_others";
}
}
namespace GraphAware\Reco4PHP\Tests\Example\Filter;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Filter\Filter;
class ExcludeOldMovies implements Filter
{
public function doInclude(Node $input, Node $item)
{
$title = $item->value("title");
preg_match('/(?:\()\d+(?:\))/', $title, $matches);
if (isset($matches[0])) {
$y = str_replace('(','',$matches[0]);
$y = str_replace(')','', $y);
$year = (int) $y;
if ($year < 1999) {
return false;
}
return true;
}
return false;
}
}
namespace GraphAware\Reco4PHP\Tests\Example\Filter;
use GraphAware\Common\Cypher\Statement;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Filter\BaseBlacklistBuilder;
class AlreadyRatedBlackList extends BaseBlacklistBuilder
{
public function blacklistQuery(Node $input)
{
$query = 'MATCH (input) WHERE id(input) = {inputId}
MATCH (input)-[:RATED]->(movie)
RETURN movie as item';
return Statement::create($query, ['inputId' => $input->identity()]);
}
public function name()
{
return 'already_rated';
}
}
namespace GraphAware\Reco4PHP\Tests\Example\PostProcessing;
use GraphAware\Common\Cypher\Statement;
use GraphAware\Common\Result\Record;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Post\RecommendationSetPostProcessor;
use GraphAware\Reco4PHP\Result\Recommendation;
use GraphAware\Reco4PHP\Result\Recommendations;
use GraphAware\Reco4PHP\Result\SingleScore;
class RewardWellRated extends RecommendationSetPostProcessor
{
public function buildQuery(Node $input, Recommendations $recommendations)
{
$query = 'UNWIND {ids} as id
MATCH (n) WHERE id(n) = id
MATCH (n)<-[r:RATED]-(u)
RETURN id(n) as id, sum(r.rating) as score';
$ids = [];
foreach ($recommendations->getItems() as $item) {
$ids[] = $item->item()->identity();
}
return Statement::create($query, ['ids' => $ids]);
}
public function postProcess(Node $input, Recommendation $recommendation, Record $record)
{
$recommendation->addScore($this->name(), new SingleScore($record->get('score'), 'total_ratings_relationships'));
}
public function name()
{
return "reward_well_rated";
}
}
namespace GraphAware\Reco4PHP\Tests\Example;
use GraphAware\Reco4PHP\Engine\BaseRecommendationEngine;
use GraphAware\Reco4PHP\Tests\Example\Filter\AlreadyRatedBlackList;
use GraphAware\Reco4PHP\Tests\Example\Filter\ExcludeOldMovies;
use GraphAware\Reco4PHP\Tests\Example\PostProcessing\RewardWellRated;
use GraphAware\Reco4PHP\Tests\Example\Discovery\RatedByOthers;
class ExampleRecommendationEngine extends BaseRecommendationEngine
{
public function name()
{
return "example";
}
public function discoveryEngines()
{
return array(
new RatedByOthers()
);
}
public function blacklistBuilders()
{
return array(
new AlreadyRatedBlackList()
);
}
public function postProcessors()
{
return array(
new RewardWellRated()
);
}
public function filters()
{
return array(
new ExcludeOldMovies()
);
}
}
namespace GraphAware\Reco4PHP\Tests\Example;
use GraphAware\Reco4PHP\Context\SimpleContext;
use GraphAware\Reco4PHP\RecommenderService;
class ExampleRecommenderService
{
/**
* @var \GraphAware\Reco4PHP\RecommenderService
*/
protected $service;
/**
* ExampleRecommenderService constructor.
* @param string $databaseUri
*/
public function __construct($databaseUri)
{
$this->service = RecommenderService::create($databaseUri);
$this->service->registerRecommendationEngine(new ExampleRecommendationEngine());
}
/**
* @param int $id
* @return \GraphAware\Reco4PHP\Result\Recommendations
*/
public function recommendMovieForUserWithId($id)
{
$input = $this->service->findInputBy('User', 'id', $id);
$recommendationEngine = $this->service->getRecommender("user_movie_reco");
return $recommendationEngine->recommend($input, new SimpleContext());
}
}