Example
<?php
# Description: Named Entity Recognition API usage example.
# Copyright: (C) 2016 EffectiveSoft Ltd. All Rights Reserved.
# Technical support: technical-support@effective-soft.com
header('Content-Type: text/plain');
# possible named entities types
$ners = array(
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"
);
# ----------- FUNCTIONS -----------
# function for printing relations tree
function printTree($node, $height)
{
for ($i = 0; $i < $height; $i++)
{
echo "\t";
}
echo $node['text'] . "\n";
$children = $node['children'];
$height++;
foreach ($children as $child)
{
printTree($child, $height);
}
}
# ----------- END FUNCTIONS -----------*/
# sample text
$text = "Eyal Shaked was appointed General Manager of the Optical Networks Division in October 2005.";
# ----------- cURL -----------
# set the URL for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
$link = "http://api.intellexer.com/recognizeNeText?apikey=YourAPIKey&loadNamedEntities=true&loadRelationsTree=true&loadSentences=true";
$header = array('Content-type: application/octet-stream');
# curl connection initialization
$ch = curl_init();
#set cURL options
curl_setopt_array($ch, array(
CURLOPT_URL => $link,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $text,
CURLOPT_FOLLOWLOCATION => true
));
# perform the request
$results = curl_exec($ch);
# error checking
if (curl_errno($ch))
{
echo 'CURL error: ' . curl_error($ch);
}
else
{
# parse JSON results
$json_results = json_decode($results, true);
}
# print "Detected Named Entities"
echo 'Detected Named Entities: ';
$entities = &$json_results['entities'];
foreach ($entities as $entity) {
echo "\n\t" . $ners[$entity['type']] . ": " . $entity['text'];
}
# print relations tree
echo "\n\n" . 'Relations tree:' . "\n";
printTree($json_results['relationsTree'], 1);
curl_close($ch);
# ----------- END cURL -----------
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)