# Description: Language Recognizer 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 = "Ces droits et tous les autres, ne sont que des manisfestations concrète du droit général a l'existence et à l'acquisition de sa fin. Ces déterminations doivent provenir du fait.";
# returned data from Intellexer API
my $results = "";

# create connection to the Language 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/recognizeLanguage?apikey=YourAPIKey";
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 information
	my @languages = @{$json_results->{'languages'}};
	foreach my $lang ( @languages )
		print "Language ", $lang->{"language"}, "\n";
		print "Encoding ", $lang->{"encoding"}, "\n";
		print "Weight ", $lang->{"weight"}, "\n\n";
	print "HTTP POST error code: ", $resp->code, "\t", "HTTP POST error message: ", $resp->message, "\n";


Language French
Encoding utf-8
Weight 9.11012893258428