Getting Started with the AYLIEN SDK for Java
This is the sixth in our series of blogs on getting started with AYLIEN’s various SDKs.
If you’re new to AYLIEN and you don’t have an account yet, you can go directly to the Getting Started page on the website which will take you through the signup process. We have a free plan to get started with that allows you to make up to 1,000 calls per day for free.
Downloading and Installing the Java SDK
All of our SDK repositories are hosted on Github. For the Java SDK, the Text Analysis API is published to Maven Central, so simply add the dependency to the POM:
<dependency> <groupId>com.aylien.textapi> <artifactId>client> <version>0.1.0> </dependency>
Once you’ve installed the SDK you’re ready to start coding. For the remainder of this blog we’ll walk you through making calls and show the output you should receive in each case. Taking simple examples we’ll showcase some of the API’s endpoints like Language detection, Sentiment Analysis and hashtag suggestion.
Configuring the SDK with your AYLIEN credentials
Once you’ve received your AYLIEN APP_ID and APP_KEY from the signup process and have downloaded the SDK you can begin making calls with the following imports and configuration code.
import com.aylien.textapi.TextAPIClient; import com.aylien.textapi.parameters.*; import com.aylien.textapi.responses.*; TextAPIClient client = new TextAPIClient( "YourApplicationId", "YourApplicationKey");
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.
First off, let’s take a look at the language detection endpoint. As a simple example we’re going to detect the language of the following sentence: ‘What language is this sentence written in?’
To do this, you can call the endpoint using the following piece of code.
String text = "What language is this sentence written in?"; LanguageParams languageParams = new LanguageParams(text,null); Language language = client.language(languageParams); System.out.printf("nText : %s",language.getText()); System.out.printf("nlanguage :%s",language.getLanguage()); System.out.printf("nConfidence %f",language.getConfidence());
You should receive an output very similar to the one shown below. This 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, which means you can be pretty sure it is correct.
Language Detection Results
Text : "What language is this sentence written in?"
Language : en
Next, we’ll look at analyzing the sentence “John is a very good football player” to determine it’s sentiment i.e. whether it’s positive, neutral or negative. The Sentiment Analysis 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!"; SentimentParams sentimentParams = new SentimentParams(text,null,null); Sentiment sentiment = client.sentiment(sentimentParams); System.out.printf("nText : %s",sentiment.getText()); System.out.printf("nSentiment Polarity : %s",sentiment.getPolarity()); System.out.printf("nPolarity Confidence: %f",sentiment.getPolarityConfidence()); System.out.printf("nSubjectivity: %s",sentiment.getSubjectivity()); System.out.printf("nSubjectivity Confidence: %f",sentiment.getSubjectivityConfidence());
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.999999
Subjectivity : objective Subjectivity Confidence: 0.989682
Finally, we’ll look at analyzing a BBC article to extract hashtag suggestions for it with the following code.
HashTagsParams hashtagsParams = new HashTagsParams(null,url,null); HashTags hashtags = client.hashtags(hashtagsParams); System.out.print("Hashtags : n"); System.out.print(hashtags + "n");
You should receive the output shown below.
Hashtag Suggestion Results
Hashtags : #Planet #JohannesKepler #Kepler #Birmingham #Earth #Astronomy #TheAstrophysicalJournal #Warwick #Venus #Orbit #Mercury #SolarSystem #Resonance #TerrestrialPlanet #Lightyear #Imagine
If Java’s not your preferred language then check out our other SDKs for node.js, Go, PHP, Python, Ruby and .Net (C#). For more information regarding the APIs go to the documentation section of our website.