Example
"""
Description: Clusterizer API usage example.
Copyright: (C) 2016 EffectiveSoft Ltd. All Rights Reserved.
Technical support: technical-support@effective-soft.com
"""
import json
import urllib
import urllib2
# sample text
sample_text = "Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs."
# set the URL with parameters for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
api_url = "http://api.intellexer.com/clusterizeText?apikey=YourAPIKey&conceptsRestriction=10&fullTextTrees=true"
# print concept tree
def print_tree(node, height):
for i in range(0, height):
print "\t",
print node.get('text')
children = node.get('children')
height += 1
for child in children:
print_tree(child, height)
# print response results
def print_response(response):
print "Concept tree:"
print_tree(response.get('conceptTree'), 1)
# create request to the Clusterizer API service
def request_api(url, text):
header = { 'Content-Type' : "application/octet-stream" }
req = urllib2.Request(url, text, 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, sample_text)
except urllib2.HTTPError as error:
print 'HTTP error - %s' % error.read()
Output
Concept tree:
programming
computer programming
executable computer program
process
computing problem
original formulation