Two Context Services behavior models provide a weighted list of either a user's likely next destination or cuisine preferences based on established behavior patterns. The more context that's available to the model, the more accurate the lists.
This article tells you how to do the following:
- Access the behavior model
- Get your users' likely next destination
- Get a list of your users' cuisine preferences
Prerequisites
Before you begin:
- Register your application with Intel® Cloud Services Platform.
- Get authorization and an access token for your app.
- Make sure your app has permission to store a user's context.
For more information on these requirements, see Request Access to Users' Context with Scopes in the Context Services Developer's Guide.
Note: In the following example code, BaseURL is https://api.intel.com. Authorization is "Bearer" plus the access token granted to the application; this information is required in the header for all Cloud Services calls.
Access the Behavioral Model
Your app must get permission from users to access their behavior model data. You can make this request at the same time that you request permissions to upload and download context, or you can make the request separately.
The following authorization request opens Intel Identity Services on the user's device, and requests access to the user's location and point of interest context, needed for this tutorial. The scopes are:
- prediction:location
- prediction:poi-weights
function AuthRequest() {
id = BuildRequestid();
var AuthRequestURL = BaseURL + '/identityui/v2/auth?client_id=' + APIKey + '&redirect_uri=urn:intel:identity:oauth:oob:async' + '&scope=user:details+context:location:checkin+context:post:location:checkin+prediction:location+prediction:poi-weights&state=' + id + '&response_type=code&auto_register=false';
var ref = window.open(AuthRequestURL);
ref.focus(); // set focus to window
document.getElementById(DocToModify).value = "Auth Request Complete";
}
Get Your Users' Likely Next Destination
For demonstration purposes, this section uses some sample stored context. To get a response to your behavior model calls, you need to have a collection of route history context.
- Request a list of likely next destinations for your user.
The call URL is https://api.intel.com/context/v1/users/{user ID}/models/behavior/places and includes the latitude and longitude of a starting location. It also includes a timestamp, which further refines the list that's generated. For example, if a user's starting destination is home, and it's 7:30 AM on a weekday, then the next destination might be the user's workplace, whereas leaving from home at 4:00 PM on a Saturday would generate a different next destination, using past context for that time and day of the week.
In addition, the call below uses "me" in place of the user ID, indicating that Context Services should use the app's user ID credentials.
function GetPlaces() { jQuery.support.cors = true; var URL = BaseURL + '/context/v1/users/me/models/behavior/places' + '?lat=45.54&lon=-122.9623&timestamp=2012-11-10T09:08:07Z'; var Authorization = 'Bearer ' + AccessToken; $.ajax({ url: URL, type: "GET", dataType: "json", beforeSend: function (request) { request.setRequestHeader("authorization", Authorization); request.setRequestHeader("Content-Type", "application/json"); }, success: function (data, textStatus, jqxhr) { alert(jqxhr.responseText); }, error: function (jqxhr, textStatus, errorThrown) { alert("An error occurred: " + jqxhr.status + " " + jqxhr.statusText + " " + textStatus + " " + errorThrown); alert(jqxhr.responseText); }, }); }
The following JSON response is based on sample route history. By the latitude and longitude, you can find out that the most likely next destination is the Millikin Way MAX station in Beaverton, Oregon. This spot has the highest weight, at 18.925. The next likely destination has a weight of 8.822, and is a spot along the Tualatin Valley Highway in Aloha, Oregon.
[{"poi-type":null,"distance-factor":0.11646441305907436,"weight":18.92546712209959,"centroid":{"lon":-122.80693500000004,"lat":45.49078800000002},"place-id":"f5e84095-9215-4ab0-b85e-b174d3c58f0f"},{"poi-type":"restaurant","distance-factor":0.203590875424561,"weight":8.822271268397646,"centroid":{"lon":-122.88428500000006,"lat":45.495632},"place-id":"a52ba589-7b38-4744-9e73-e548811dccdf"}]
Get a List of Your User's Cuisine Preferences
This section also uses demonstration sample data. The cuisine preferences that you get for your users will be based on the route history context your app has collected for that user.
- Request a weighted list of cuisine preferences for your user.
The call URL is https://api.intel.com/context/v1/users/{user ID}/models/behavior/poi-weights and includes the latitude and longitude of a point of interest in the user's vicinity, and a timestamp. The timestamp again helps to refine the list that's provided. For example, cuisine preferences for a sushi restaurant will have a lower weight in the morning than in the evening if a user tends to prefer sushi for dinner. In addition, "me" is used in place of the user ID, indicating that Context Services should use the app's user ID credentials.
function GetPOIWeights() { jQuery.support.cors = true; var URL = BaseURL + '/context/v1/users/me/models/behavior/poi-weights' + "?lat=45.54&lon=-122.9623&timestamp=2012-11-10T09:08:07Z"; var Authorization = 'Bearer ' + AccessToken; $.ajax({ url: URL, type: "GET", dataType: "json", beforeSend: function (request) { request.setRequestHeader("authorization", Authorization); request.setRequestHeader("Content-Type", "application/json"); }, success: function (data, textStatus, jqxhr) { alert(jqxhr.responseText); }, error: function (jqxhr, textStatus, errorThrown) { alert("An error occurred: " + jqxhr.status + " " + jqxhr.statusText + " " + textStatus + " " + errorThrown); alert(jqxhr.responseText); }, }); }
The following is the JSON response to the /poi-weights call. This response indicates that Japanese cuisine has the highest preference with sushi half as likely. This response also indicates that the preferred cuisine has a price level of 2 out of 5, 5 being the most expensive. The price metric is roughly as follows:
1: -$15
2: $15-30
3: $30-50
4: $50-75
5: $75
[{"weight":18.92546712209959,"attributes": {"cuisine":"Japanese","price":2}}, {"weight":8.822271268397646,"attributes": {"cuisine":"Sushi","price":2}}]
More Information
Context Services REST API Reference
Context Services REST Developer's Guide