How to detect the language of a string in Python

Language detection has never been so easy. Let’s take a quick look at how to apply one of our APIs to simplify this important function. First, use Python’s pip installer to add the NLP (natural language processing) client:

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.NLP.git

Then call language_detection_post:

from __future__ import print_function
import time
import cloudmersive_nlp_api_client
from cloudmersive_nlp_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_nlp_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'
# create an instance of the API class
api_instance = cloudmersive_nlp_api_client.LanguageDetectionApi(cloudmersive_nlp_api_client.ApiClient(configuration))
text_to_detect = 'text_to_detect_example' # str | Text to detect language of
try:
# Detect language of text
api_response = api_instance.language_detection_post(text_to_detect)
pprint(api_response)
except ApiException as e:
print("Exception when calling LanguageDetectionApi->language_detection_post: %s\n" % e)

For example, we might input the phrase “Können Sie das übersetzen?” Our response:

{
"Successful": true,
"DetectedLanguage_ThreeLetterCode": "DEU",
"DetectedLanguage_FullName": "German"
}

Done.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store