# Description: Clusterizer 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;

# sample text
my $text = "Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs. ";

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

# create connection to the Clusterizer API service
my $ua = LWP::UserAgent->new or die "Cannot create connection!";
# set the URL with parameters for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
my $api_url = "http://api.intellexer.com/clusterizeText?apikey=YourAPIKey&conceptsRestriction=10&fullTextTrees=true";
my $req = HTTP::Request->new(POST => $api_url);
$req->header('content-type' => 'application/octet-stream');
# 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 concept tree
	print "Concept tree: \n";
	printTree($json_results->{'conceptTree'}, 0);
	print "HTTP POST error code: ", $resp->code, "\t", "HTTP POST error message: ", $resp->message, "\n";

# print concept tree
sub printTree()
	my $node = shift;
	my $tree_height = shift;
	for (my $i = 0; $i < $tree_height; $i++)
		print "\t";
	print $node->{"text"}, "\n";
	my @children = @{$node->{'children'}};
	foreach my $child ( @children )
		printTree($child, $tree_height);


Concept tree: 

		computer programming
	executable computer program
	computing problem
	original formulation