Example
# -*- coding: utf-8 -*-
"""
Description: Language Recognizer API usage example.
Copyright: (C) 2016 EffectiveSoft Ltd. All Rights Reserved.
Technical support: technical-support@effective-soft.com
"""
import json
import urllib
import urllib2
# sample text
sample_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.";
# set the URL for POST request and specify API key for authorization purposes (change YourAPIKey to the Intellexer API key)
api_url = "http://api.intellexer.com/recognizeLanguage?apikey=YourAPIKey"
# print response results
def print_response(response):
languages = response.get('languages')
for lang in languages:
print "Language ", lang['language']
print "Encoding ", lang['encoding']
print "Weight ", lang['weight']
# create request to the Language Recognizer API service
def request_api(url, text):
header = { 'Content-Type' : "application/octet-stream" }
req = urllib2.Request(url, text, header)
conn = urllib2.urlopen(req)
try:
json_response = json.loads(conn.read())
finally:
conn.close()
print_response(json_response)
# perform the request
try:
request_api(api_url, sample_text)
except urllib2.HTTPError as error:
print 'HTTP error - %s' % error.read()
Output
Language French
Encoding utf-8
Weight 9.11012893258