Example

Example

"""
Description: Sentiment Analyzer API usage example.
Copyright: (C) 2016 EffectiveSoft Ltd. All Rights Reserved.
Technical support: technical-support@effective-soft.com
"""

import json
import urllib
import urllib2

# list of reviews in JSON format
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.\"
		}
			]"""

# 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)
api_url = "http://api.intellexer.com/analyzeSentiments?apikey=YourAPIKey&ontology=hotels&loadSentences=true"

# print categorized opinions
def print_tree(node, height):
	for i in range(0, height):
		print "\t",
	print node.get("t"),
	if node.get('w') != 0:
		print "\t", node.get('w')
	else:
		print "\t"
	children = node.get('children')
	height += 1
	for child in children:
		print_tree(child, height)

# print response results
def print_response(response):
	print "Sentences with sentiment objects and phrases:";
	sentences = response.get('sentences')
	for sent in sentences:
		print "Sentence Weight = ", sent.get('w'), "\t", sent.get('text').encode('utf-8')
	#print categorized opinions
	print "\nCategorized Opinions with sentiment polarity (positive/negative)"
	print_tree(response.get('opinions'), 0)

# create request to the Sentiment Analyzer API service
def request_api(url, data):
	header = { 'Content-Type' : "application/json" }
	req = urllib2.Request(url, data, header)
	conn = urllib2.urlopen(req)
	try:
		json_response = json.loads(conn.read())
	finally:
		conn.close()
	print_response(json_response)

# perform the request
try:
	request_api(api_url, reviews)
except urllib2.HTTPError as error:
	print 'HTTP error - %s' % error.read()

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)
None 	
	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