Example

Example

# Description: Sentiment Analyzer API usage example.
# Copyright: (C) 2015 EffectiveSoft Ltd. All Rights Reserved.
# Technical support: technical-support@effective-soft.com
# This example requires LWP and JSON libraries.


use JSON;
use LWP::UserAgent;
require HTTP::Request;


# list of reviews in JSON format
my $reviews = '[
		{
			"id": "snt1",
			"text": "Very good comfort and very kind service in this modern hotel, very close to Chennai Airport."
		},
		{
			"id": "snt2",
			"text": "The service and restaurants are excellent, especially like the Samudra restaurant providing lots of tempting alternatives."
		},
		{
			"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."
		},
		{
			"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."
		}]';

# returned data from Intellexer API
my $results = "";

# create connection to the Sentiment Analyzer API service
my $ua = LWP::UserAgent->new or die "Cannot create connection!";
# 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)
my $api_url = "http://api.intellexer.com/analyzeSentiments?apikey=YourAPIKey&ontology=hotels&loadSentences=true";
my $req = HTTP::Request->new(POST => $api_url);
$req->header('content-type' => 'application/json');
$req->content($reviews);
# perform the request
my $resp = $ua->request($req);
# error checking
if ($resp->is_success) 
{
	$results = $resp->decoded_content;
	# parse JSON results
	my $json_results = decode_json($results);
	# print categorized opinions
	print "Categorized Opinions with sentiment polarity (positive/negative)\n";
	printTree($json_results->{"opinions"}, 0);
	
}
else 
{
	print "HTTP POST error code: ", $resp->code, "\t", "HTTP POST error message: ", $resp->message, "\n";
}

# print categorized opinions
sub printTree()
{
	my $node = shift;
	my $tree_height = shift;
	for (my $i = 0; $i < $tree_height; $i++)
	{
		print "\t";
	}
	print $node->{"t"};
	if ($node->{"w"} != 0)
	{
		print "\t", $node->{"w"}, "\n";
	}
	else
	{
		print "\n";
	}
	my @children = @{$node->{'children'}};
	$tree_height++;
	foreach my $child ( @children )
	{
		printTree($child, $tree_height);
	}
}

Output

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