Search our Blogs
Showing results for 
Search instead for 
Do you mean 
 

Automatically identify the language of text using Python

Identifying language of text, files, and websites can difficult if you do not speak a multitude of languages. Fortunately for developers, doing this in Python is simple using Haven OnDemand’s Language Identification API. All you need to do is install the official Python client library, POST a block of raw text, publicly accessible URL, or local file to Haven OnDemand’s Language Identification API, and obtain the result. For a full list of supported languages, see here.

 

Code

Completed code

 

First, install the official Haven OnDemand Python client library:

 

 

pip install havenondemand

 

Next, open up the file you will write code in and require Haven OnDemand:

 

from havenondemand.hodclient import *
client = HODClient("APIKEY", "v1")

Replace “APIKEY” with your API key, which can be found here after signing up.

 

Next, you’ll call the Language Identification API by submitting either a block of raw text, publicly accessible URL, or local file:

 

# data = {'url': 'http://www.galactanet.com/oneoff/theegg_mod.html', 'additional_metadata': False} # uncomment if using publicy accessible URL
# data = {'file': 'path/to/file', 'additional_metadata': False} # uncomment if using local file
data = {'text': 'This sentence is english', 'additional_metadata': False} # uncomment if using raw text
response = client.post_request(data, HODApps.IDENTIFY_LANGUAGE, async=False)
print response

Note: the optional “additional_metadata” parameter in the request that lets you specify whether or not you’d like additional information on the language identified .

 

When you run the code, it will output the response of the API with the language of the text. It will look like this:

 

{u'unicode_scripts': [u'Basic Latin'], u'encoding': u'UTF8', u'language': u'english', u'language_iso639_2b': u'ENG'}

 

Comments
efroselli Level 4
| ‎12-09-2016 07:04

Clear and easy tutorial. Thanks!

Social Media
About the Author
Topics
† The opinions expressed above are the personal opinions of the authors, not of HPE. By using this site, you accept the Terms of Use and Rules of Participation