How to detect the language of a string in Ruby

Today’s tutorial will cover automating language identification. Let’s begin.

First, add the following to you Gemfile to add the API that we will be using:

gem 'cloudmersive-nlp-api-client', '~> 1.2.9'

Second, call language_detection_post:

# load the gem
require 'cloudmersive-nlp-api-client'
# setup authorization
CloudmersiveNlpApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
api_instance = CloudmersiveNlpApiClient::LanguageDetectionApi.newtext_to_detect = "text_to_detect_example" # String | Text to detect language ofbegin
#Detect language of text
result = api_instance.language_detection_post(text_to_detect)
p result
rescue CloudmersiveNlpApiClient::ApiError => e
puts "Exception when calling LanguageDetectionApi->language_detection_post: #{e}"

Third… that’s it! This function supports a large number of languages, including French, Chinese, Spanish, German, Portuguese, and Dutch. The NLP API covers a variety of other functions including spell checking, noun identification, and POS tagging.

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