Product

Getting Started with the AYLIEN SDK for Ruby

This is the fourth in our series of blogs on getting started with our various SDKs. Depending on what your language preference is, we have SDKs available for Node.js, Python, Ruby, PHP, GO, Java and .Net (C#). Last week’s blog focused using our Go SDK. This week we’ll focus getting up and running with Ruby.

If you are new to our API and you don’t have an account, you can go directly to the Getting Started page on our website, which will take you through the signup process. You can sign up to a free plan to get started which, allows you to make up to 1,000 calls per day to the API for free.

Downloading and Installing the Ruby SDK

All of our SDK repositories are hosted on Github, you can get the Ruby repository here. The simplest way to install the repository is by using “gem”.

$ gem install aylien_text_api

Configuring the SDK with your AYLIEN credentials

Once you have received your AYLIEN APP_ID and APP_KEY from the signup process and have downloaded the SDK you can start making calls by passing your configuration parameters as a block to AylienTextApi.configure.

require 'aylien_text_api'

AylienTextApi.configure do |config|
  config.app_id        =    "YOUR_APP_ID"
  config.app_key       =    "YOUR_APP_KEY"
end
client = AylienTextApi::Client.new

Alternatively, you can pass them as parameters to AylienTextApi::Client class.

require 'aylien_text_api'

client = AylienTextApi::Client.new(app_id: "YOUR APP ID", app_key: "YOUR APP KEY")

When calling the various API endpoints you can specify a piece of text directly for analysis or you can pass a url linking to the text or article you wish to analyze.

Language Detection

First let’s take a look at the language detection endoint. Specifically we will detect the language of the sentence “’What language is this sentence written in?’

You can call the endpoint using the following piece of code.

text = "What language is this sentence written in?"
language = client.language text: text
puts "Text: #{language[:text]}"
puts "Language: #{language[:lang]}"
puts "Confidence: #{language[:confidence]}"

You should receive an output very similar to the one shown below which shows that the language detected was English and the confidence that it was detected correctly (a number between 0 and 1) is very close to 1 indicating that you can be pretty sure it is correct.

Language Detection Results

Text: What language is this sentence written in?
Language: en
Confidence: 0.9999962069593649

Sentiment Analysis

Next we will look at analyzing the sentence “John is a very good football player” to determine it’s sentiment i.e. positive , neutral or negative. The endpoint will also determine if the text is subjective or objective. You can call the endpoint with the following piece of code

text = "John is a very good football player!"
sentiment = client.sentiment text: text
puts "Text            :  #{sentiment[:text]}"
puts "Sentiment Polarity  :  #{sentiment[:polarity]}"
puts "Polarity Confidence  :  #{sentiment[:polarity_confidence]}"
puts "Subjectivity  :  #{sentiment[:subjectivity]}"
puts "Subjectivity Confidence  :  #{sentiment[:subjectivity_confidence]}"

You should receive an output similar to the one shown below which indicates that the sentence is objective and is positive, both with a high degree of confidence.

Sentiment Analysis Results

Text            :  John is a very good football player!
Sentiment Polarity  :  positive
Polarity Confidence  :  0.9999988272764874
Subjectivity  :  objective
Subjectivity Confidence  :  0.9896821594138254

Article Classification

Next we will take a look at the classification endpoint. The Classification endpoint automatically assigns an article or piece of text to one or more categories making it easier to manage and sort. The classification is based on IPTC International Subject News Codes and can identify up to 500 categories. The code below analyses a BBC news article about mega storms on the planet Uranus.

url = "http://www.bbc.com/earth/story/20150121-mega-storms-sweep-uranus"
classify = client.classify url: url
classify[:categories].each do |cat|
	puts "Label : #{cat[:label]}"
	puts "Code : #{cat[:code]}"
	puts "Confidence : #{cat[:confidence]}"
end

When you run this code you should receive an output similar to that shown below which assigns the article an IPTC label of “natural science – astronomy” with an IPTC code of 13004007.

Article Classification Results

Label : natural science - astronomy
Code : 13004007
Confidence : 1.0

Hashtag Analysis

Next we will look at analyzing the same BBC article to extract hashtag suggestions for sharing the article on social media with the following code.

url = "http://www.bbc.com/earth/story/20150121-mega-storms-sweep-uranus"
hashtags = client.hashtags url: url
hashtags[:hashtags].each do |str|
	puts str
end

You should receive the output shown below.

Hashtag Suggestion Results

If Ruby isn’t your preferred language then check out our SDKs for node.js, Go, PHP, Python, Java and .Net (C#). For more information regarding the APIs go to the documentation section of our website.

We will be publishing ‘getting started’ blogs for the remaining languages over the coming weeks so keep an eye out for them. If you haven’t already done so you can get free access to our API on our sign up page.





Text Analysis API - Sign up




Author


Avatar

Mike Waldron

Head of Marketing & Sales @ AYLIEN A legal convert with a masters degree from Smurfit Business School, Mike runs our Sales and Marketing at AYLIEN. Mike gathered his Sales and Marketing experience with technology companies in Sydney and Dublin before getting the startup itch and joining the team at AYLIEN. Twitter: @MikeWallly