Skip to content

Instantly share code, notes, and snippets.

View predictioniogists's full-sized avatar

PredictionIO predictioniogists

View GitHub Profile
<?php
// Assuming we already have an ItemRec engine named 'similarapps'
// Get app ID
$app = getApp();
try {
// Retrieve similar apps for the current app
$command = $client->getCommand(
'itemsim_get_top_n',
<?php
// Assuming we already have an ItemRec engine named 'appsrec'
// Get user ID and the current view's category
$uid = getUid();
$category = getCategory();
try {
// Retrieve apps recommendation for the current user and category
<?php
// Assuming we already have an ItemSim engine named 'similarapps'
// Get a list of previous viewed app IDs for the current session
$previouslyViewedApps = getViewedAppsInThisSession();
$similarApps = array();
try {
// Retrieve similar apps for each previously viewed app
import io.prediction.ItemSimGetTopNRequestBuilder;
// You need to specify the name of the engine from which you want to retrieve the prediction results.
// Add the following code to display similar sightseeing spots to the user after arriving a destination:
String[] destinationIds;
try {
//retreive top 5 similar sightseeing spots from the item similarity engine
String[] itypes = {"sightseeing"}
// When a new destination is added:
try {
// sightseeing spot type may be "museums", "landmarks", etc
String[] itypes = {"destinations", "sightseeing", "sightseeing spot type 1", "sightseeing spot type 2"};
client.createItem("destination ID or name", itypes);
} catch (Exception e) {
handleException(e);
}
// When the logged-in user goes to a destination:
import io.prediction.ItemRecGetTopNRequestBuilder;
// You need to specify the name of the engine from which you want to retrieve the prediction results.
// Add the following code to display recommended drivers to the logged-in user:
String[] driverIds;
try {
// Retrieve top 5 recommended drivers from the item recommendation engine.
// Assuming client.identify("user id") is already called when the user logged in.
import io.prediction.Client;
import io.prediction.UserActionItemRequestBuilder;
import io.prediction.CreateItemRequestBuilder;
// In the beginning of your application, you need to instantiate the PredictionIO client object.
// You need this client object to import data into PredictionIO and retrieve prediction results.
// Add this line to the place where you do initialization:
Client client = new Client("your app key");
// Then add the following lines to your codes which handle the corresponding application logic:
<ivy-module ...>
...
<dependencies>
<dependency org="io.prediction" name="client" rev="0.6.0" />
...
</dependencies>
...
</ivy-module>
<project ...>
...
<dependencies>
<dependency>
<groupId>io.prediction</groupId>
<artifactId>client</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>
...
# You need to specify the name of the engine from which you want to retrieve the prediction results.
# When a user views a meal, you can suggest similar meals to the user by adding the following:
begin
# retreive top 5 similar courses from the item similarity engine
iids = client.get_itemsim_top_n("the engine name", "the viewed meal ID", 5) # list of recommended meal IDs
rescue ItemSimNotFoundError => e
iids = get_default_iids_list(5) # default behavior when there is no prediction results, e.g. recommends newest meals
log_error(e) # log the error for debugging or analysis later
end