Example

Example

// Description: Named Entity Recognizer API usage example.
// Copyright: (C) 2015 EffectiveSoft Ltd. All Rights Reserved.
// Technical support: technical-support@effective-soft.com
// This example requires the libcurl and jsoncpp libraries.

#include <iostream>
#include "curl/curl.h"
#include "json/json.h"

using namespace std;

// possible named entities types 
const char* entities_types[] = {"Unknown", "Person", "Organization", "Location", "Title", "Position",
							 "Age", "Date", "Duration", "Nationality", "Event", "Url", "MiscellaneousLocation"};

// libcurl write callback function
static int writer(char *data, size_t size, size_t nmemb,
				  std::string *writerData)
{
	if (writerData == NULL)
		return 0;
	writerData->append(data, size*nmemb);
	return size * nmemb;
}

// print relations tree
void printTree(Json::Value node, int height)
{
	for(unsigned int i = 0; i < height; ++i) std::cout << "\t";
	std::cout << node["text"].asString() << "\n";
	node = node["children"];
	height++;
	for (unsigned int j = 0; j < node.size(); ++j)
	{
		printTree(node[j], height);
	}
}


int main(int argc, char* argv[])
{
	CURL *curl = NULL;
	CURLcode res;

	// sample text
	char pszText[] = "Eyal Shaked was appointed General Manager of the Optical Networks Division in October 2005.";
	// returned data from Intellexer API
	std::string results;
	// libcurl connection initialization
	curl = curl_easy_init();
	if (curl == NULL)
	{
		std::cerr << "Failed to create CURL connection\n";
		return EXIT_FAILURE;
	}
	struct curl_slist *headerlist = NULL;
	if(curl) 
	{
		// set the URL for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
		curl_easy_setopt(curl, CURLOPT_URL, "http://api.intellexer.com/recognizeNeText?apikey=YourAPIKey&loadNamedEntities=true&loadRelationsTree=true&loadSentences=true");
		// set option to the callback function
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
		// set option for writing received data
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
		// specify the POST data
		headerlist = curl_slist_append(headerlist, "Content-Type: application/octet-stream");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, pszText);
		// perform the request
		res = curl_easy_perform(curl);
		// error checking
		if(res != CURLE_OK)
			std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << "\n";
		else
		{
			// parse JSON results
			Json::Value root;
			Json::Reader reader;
			bool isSuccess = reader.parse(results,	root, false);
			if (isSuccess)
			{
				// print information about detected named entities
				std::cout <<"Detected Named Entities:\n";
				const Json::Value entities = root["entities"];
				for(unsigned int i = 0; i < entities.size(); ++i) 
				{ 
					std::cout <<"\t"<< entities_types[entities[i]["type"].asInt()] << ": " << entities[i]["text"].asString() << "\n";
				}
				const Json::Value tree = root["relationsTree"];
				// print relations tree
				std::cout << "\nRelations tree:\n";
				printTree(tree, 1);		
			}
			else
			{
				std::cerr << "JSON parsing error\n";
				return EXIT_FAILURE;
			}
		}
		//cleanup
		curl_easy_cleanup(curl);
	}
	//global cleanup
	curl_global_cleanup();
	return 0;
}

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)