How to detect the language of a string in Python

Cloudmersive
1 min readAug 23, 2019

--

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet