Example

Example

# Description: Named Entity Recognition 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;

# possible named entities types 

%ners = (
	0 => "Unknown",
	1 => "Person",
	2 => "Organization",
	3 => "Location",
	4 => "Title",
	5 => "Position",
	6 => "Age",
	7 => "Date",
	8 => "Duration",
	9 => "Nationality",
	10 => "Event",
	11 => "Url",
	12 => "MiscellaneousLocation" );


# sample text
my $text = "Eyal Shaked was appointed General Manager of the Optical Networks Division in October 2005.";

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

# create connection to the Named Entity Recognizer API service
my $ua = LWP::UserAgent->new or die "Cannot create connection!";
# set the URL for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
my $api_url = "http://api.intellexer.com/recognizeNeText?apikey=YourAPIKey&loadNamedEntities=true&loadRelationsTree=true&loadSentences=true";
my $req = HTTP::Request->new(POST => $api_url);
$req->header('content-type' => 'application/octet-stream');
$req->content($text);
# 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 information about detected named entities
	print "Detected Named Entities:\n";
	my @entities = @{$json_results->{'entities'}};
	foreach $entity ( @entities )
	{
		print "\t", $ners{$entity->{'type'}}, ": ", $entity->{'text'}, "\n";
	}
	# print relations tree
	print "\nRelations tree:\n";
	printTree($json_results->{'relationsTree'}, 1);

}
else 
{
	print "HTTP POST error code: ", $resp->code, "\t", "HTTP POST error message: ", $resp->message, "\n";
}

# print relations 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'}};
	$tree_height++;
	foreach my $child ( @children )
	{
		printTree($child, $tree_height);
	}
}

Output

Detected Named Entities:
	Person: Eyal Shaked
	Organization: Optical Networks Division
	Position: General Manager
	Date: October 2005

Relations tree:
	root: Relations
		NE-person: Eyal Shaked
			VP: appointed (O-V)
				NE-pos: General Manager (POS)
					NE-org: Optical Networks Division (of)
				NE-date: October 2005 (DATE)