Example
<?php
// Description: Sentiment Analyzer API usage example.
// Copyright: (C) 2016 EffectiveSoft Ltd. All Rights Reserved.
// Technical support: technical-support@effective-soft.com
header('Content-Type: text/plain');
# list of reviews in JSON format
$reviews = array(
array(
"id" => "snt1",
"text" => "Very good comfort and very kind service in this modern hotel, very close to Chennai Airport."
),
array(
"id" => "snt2",
"text" => "The service and restaurants are excellent, especially like the Samudra restaurant providing lots of tempting alternatives."
),
array(
"id" => "snt3",
"text" => "Often travel to Chennai on business and this was the first time I tried the Trident, Chennai. I'll be staying here on all future trips. Excelllent service and the food was terrific."
),
array(
"id" => "snt4",
"text" => "The ambiance of the restaurants is very poor, Cinnamon is more laid back, Samudra is more exclusive. Food is excellent and of course with Trident totally safe. Staff are very warm and welcoming. The rooms are not brand new."
)
);
$json_reviews = json_encode($reviews);
# ----------- FUNCTIONS -----------
# function for printing sentences
function printSentences($response) {
foreach($response['sentences'] as $resp_sent) {
echo "\n" . 'Sentence Weight = ' . $resp_sent['w'] . "\t" . $resp_sent['text'];
}
}
# function for printing categorized opinions
function printTree($node, $height)
{
for ($i = 0; $i < $height; $i++)
{
echo "\t";
}
echo $node['t'];
if ($node['w'] != 0)
{
echo "\t" . $node['w'] . "\n";
}
else
{
echo "\n";
}
$children = $node['children'];
$height++;
foreach ($children as $child)
{
printTree($child, $height);
}
}
# ----------- END FUNCTIONS -----------
# ----------- cURL -----------
# set the URL for POST request, specify url, parameters for information processing, ontology for opinion categorization and API key for authorization purposes (change YourAPIKey to the Intellexer API key)
$link = "http://api.intellexer.com/analyzeSentiments?apikey=YourAPIKey&ontology=hotels&loadSentences=true";
$header = array('Content-type: application/json');
# curl connection initialization
$ch = curl_init();
#set cURL options
curl_setopt_array($ch, array(
CURLOPT_URL => $link,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $json_reviews,
CURLOPT_FOLLOWLOCATION => true
));
# perform the request
$results = curl_exec($ch);
# error checking
if (curl_errno($ch))
{
echo 'CURL error: ' . curl_error($ch);
}
else
{
# parse JSON results
$json_results = json_decode($results, true);
# print "Sentences with sentiment objects and phrases"
echo 'Sentences with sentiment objects and phrases: ' . "\n";
printSentences($json_results);
# print "Categorized Opinions with sentiment polarity (positive/negative)"
echo "\n\n" . 'Categorized Opinions with sentiment polarity (positive/negative)' . "\n";
printTree($json_results['opinions'], 0);
}
curl_close($ch);
# ----------- END cURL -----------
Output
Sentences with sentiment objects and phrases:
Sentence Weight = 2.21307627275 <pos w="4.475">Very</pos> <pos w="4.475">good</pos> <obj>comfort</obj> and <pos w="2.475">very</pos> <pos w="2.475">kind</pos> <obj>service</obj> in this <pos w="1.65">modern</pos> <obj>hotel</obj>, <pos w="2.25">very</pos> <pos w="2.25">close</pos> <pos w="2.25">to</pos> <obj>Chennai</obj> <obj>Airport</obj>.
Sentence Weight = 1.26491106407 <obj>The</obj> <obj>service</obj> and <obj>restaurants</obj> are <pos w="4.95">excellent</pos>, especially like the Samudra restaurant providing lots of <pos w="1.65">tempting</pos> <obj>alternatives</obj>.
Sentence Weight = 0 Often travel to Chennai on business and this was the first time I tried the Trident, Chennai.
Sentence Weight = 1 I 'll be <pos w="1.4">staying</pos> here on all future trips.
Sentence Weight = 1 Excelllent service and <obj>the</obj> <obj>food</obj> was <pos w="4.95">terrific</pos>.
Sentence Weight = -0.256073759866 <obj>The</obj> <obj>ambiance</obj> <obj>of</obj> <obj>the</obj> <obj>restaurants</obj> is <neg w="-2.475">very</neg> <neg w="-2.475">poor</neg>, Cinnamon is more laid back, <obj>Samudra</obj> is <pos w="2.0625">more</pos> <pos w="2.0625">exclusive</pos>.
Sentence Weight = 1.3416407865 <obj>Food</obj> is <pos w="4.95">excellent</pos> and of <obj>course</obj> <obj>with</obj> <obj>Trident</obj> <pos w="2.475">totally</pos> <pos w="2.475">safe</pos>.
Sentence Weight = 1.38675049056 <obj>Staff</obj> are <pos w="2.475">very</pos> <pos w="2.475">warm</pos> and <pos w="1.65">welcoming</pos>.
Sentence Weight = -2 <obj>The</obj> <obj>rooms</obj> are <neg w="-0.4125">not</neg> <neg w="-0.4125">brand</neg> <neg w="-0.4125">new</neg>.
Categorized Opinions with sentiment polarity (positive/negative)
Dining/Food & Drink
food
excellent 4.95
terrific 4.95
Hotel atmosphere
ambiance
very poor -2.475
comfort
very good 4.475
Hotel Facilities
hotel
modern 1.65
restaraunt
excellent 4.95
Hotel/Room service
service
excellent 4.95
very kind 2.475
Staff
hotel staff
very warm 2.475
welcoming 1.65
Rooms/Room type
room
not brand new -0.4125
Other
alternative
tempting 1.65
chennai airport
close 2.25
course
totally safe 2.475
samudra
more exclusive 2.0625