PHPackages                             graphaware/reco4php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. graphaware/reco4php

ActiveLibrary[Framework](/categories/framework)

graphaware/reco4php
===================

Neo4j based Recommendation Engine Framework for PHP

2.0.5(8y ago)131219.1k↓46.7%22[2 issues](https://github.com/graphaware/reco4php/issues)[1 PRs](https://github.com/graphaware/reco4php/pulls)Apache-2.0PHPPHP ^7.0

Since Jan 24Pushed 3y ago29 watchersCompare

[ Source](https://github.com/graphaware/reco4php)[ Packagist](https://packagist.org/packages/graphaware/reco4php)[ RSS](/packages/graphaware-reco4php/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (5)Versions (18)Used By (0)

GraphAware Reco4PHP
===================

[](#graphaware-reco4php)

Neo4j based Recommendation Engine Framework for PHP
---------------------------------------------------

[](#neo4j-based-recommendation-engine-framework-for-php)

GraphAware Reco4PHP is a library for building complex recommendation engines atop Neo4j.

[![Build Status](https://camo.githubusercontent.com/4aef425d864b719dd634533234ec11e1578470bac360a2d3ef6e3f5b9f66f0d6/68747470733a2f2f7472617669732d63692e6f72672f677261706861776172652f6e656f346a2d7068702d636c69656e742e737667)](https://travis-ci.org/graphaware/reco4php)

Features:

- Clean and flexible design
- Built-in algorithms and functions
- Ability to measure recommendation quality
- Built-in Cypher transaction management

Requirements:

- PHP7.0+
- Neo4j 2.2.6+ (Neo4j 3.0+ recommended)

The library imposes a specific recommendation engine architecture, which has emerged from our experience building recommendation engines and solves the architectural challenge to run recommendation engines remotely via Cypher. In return it handles all the plumbing so that you only write the recommendation business logic specific to your use case.

### Recommendation Engine Architecture

[](#recommendation-engine-architecture)

#### Discovery Engines and Recommendations

[](#discovery-engines-and-recommendations)

The purpose of a recommendation engine is to `recommend` something, should be users you should follow, products you should buy, articles you should read.

The first part in the recommendation process is to find items to recommend, it is called the `discovery` process.

In Reco4PHP, a `DiscoveryEngine` is responsible for discovering items to recommend in one possible way.

Generally, recommender systems will contains multiple discovery engines, if you would write the `who you should follow on github` recommendation engine, you might end up with the non-exhaustive list of `Discovery Engines` :

- Find people that contributed on the same repositories than me
- Find people that `FOLLOWS` the same people I follow
- Find people that `WATCH` the same repositories I'm watching
- ...

Each `Discovery Engine` will produce a set of `Recommendations` which contains the discovered `Item` as well as the score for this item (more below).

#### Filters and BlackLists

[](#filters-and-blacklists)

The purpose of `Filters` is to compare the original `input` to the `discovered` item and decide whether or not this item should be recommended to the user. A very straightforward filter could be `ExcludeSelf` which would exclude the item if it is the same node as the input, which can relatively happen in a densely connected graph.

`BlackLists` on the other hand are a set of predefined nodes that should not be recommended to the user. An example could be to create a `BlackList` with the already purchased items by the user if you would recommend him products he should buy.

#### PostProcessors

[](#postprocessors)

`PostProcessors` are providing the ability to post process the recommendation after it has passed the filters and blacklisting process.

For example, if you would reward a recommended person if he/she lives in the same city than you, it wouldn't make sense to load all people from the database that live in this city in the discovery phase (this could be millions if you take London as an example).

You would then create a `RewardSameCity` post processor that would adapt the score of the produced recommendation if the input node and the recommended item are living in the same city.

#### Summary

[](#summary)

To summarize, a typical recommendation engine will be a set of :

- one or more `Discovery Engines`
- zero or more `Fitlers` and `BlackLists`
- zero or more `PostProcessors`

Let's start it !

#### Usage by example

[](#usage-by-example)

We will use the small dataset available from MovieLens containing movies, users and ratings as well as genres.

The dataset is publicly available here : . The data set to download is in the **MovieLens Latest Datasets** section and is named `ml-latest-small.zip`.

Once downloaded and extracted the archive, you can run the following Cypher statements for importing the dataset, just adapt the file urls to match your actual path to the files :

```
CREATE CONSTRAINT ON (m:Movie) ASSERT m.id IS UNIQUE;
CREATE CONSTRAINT ON (g:Genre) ASSERT g.name IS UNIQUE;
CREATE CONSTRAINT ON (u:User) ASSERT u.id IS UNIQUE;

```

```
LOAD CSV WITH HEADERS FROM "file:///Users/ikwattro/dev/movielens/movies.csv" AS row
WITH row
MERGE (movie:Movie {id: toInt(row.movieId)})
ON CREATE SET movie.title = row.title
WITH movie, row
UNWIND split(row.genres, '|') as genre
MERGE (g:Genre {name: genre})
MERGE (movie)-[:HAS_GENRE]->(g)

```

```
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///Users/ikwattro/dev/movielens/ratings.csv" AS row
WITH row
MATCH (movie:Movie {id: toInt(row.movieId)})
MERGE (user:User {id: toInt(row.userId)})
MERGE (user)-[r:RATED]->(movie)
ON CREATE SET r.rating = toInt(row.rating), r.timestamp = toInt(row.timestamp)

```

For the purpose of the example, we will assume we are recommending movies for the User with ID 460.

### Installation

[](#installation)

Require the dependency with `composer` :

```
composer require graphaware/reco4php
```

### Usage

[](#usage)

#### Discovery

[](#discovery)

In order to recommend movies people should watch, you have decided that we should find potential recommendations in the following way :

- Find movies rated by people who rated the same movies than me, but that I didn't rated yet

As told before, the `reco4php` recommendation engine framework makes all the plumbing so you only have to concentrate on the business logic, that's why it provides base class that you should extend and just implement the methods of the upper interfaces, here is how you would create your first discovery engine :

```
