Getting Started with Aylien’s Text Analysis SDK for Node.js

Getting Started with Aylien’s Text Analysis SDK for Node.js

We recently developed and released SDKs for Node.js, Python, Ruby and PHP with Java, Go and C# coming out next week. This is the first blog in a series on using AYLIEN’s various Software Development Kits (SDKs) and for todays blog we are going to focus on using the Node.js SDK.

If you are new to AYLIEN and do not yet have an account you can take a look at our blog on getting started with the API or alternatively 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 you started which allows you to make up to 1,000 calls per day to the API for free.

Downloading and Installing the Node.js SDK

All of our SDK repositories are hosted on Github. The simplest way to install the repository is with the node package manager “npm”, by typing the following from the command line

$ npm install aylien_textapi

Once you have installed the SDK you are ready to start coding!

For this guide in particular we are going to walk you through some of the basic functionality of the API incorporating the “Basic Functions” sample app from the Sandbox. We’ll illustrate making a call to three of the endpoints individually and interpret the output that you should receive in each case.

Accessing the SDK with your AYLIEN credentials

Once you have received your AYLIEN credentials from the signup process and have downloaded the SDK you can begin making calls by adding the following code to your node.js application.

var AYLIENTextAPI = require('aylien_textapi');
var textapi = new AYLIENTextAPI({
  application_key: "YOUR_APP_KEY"

When calling the various endpoints you can specify whether your input is a piece of text 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 endpoint. The Language Detection endpoint is pretty straight forward, it is used to detect the language of a sentence. In this case we are analyzing the following sentence: “What language is this sentence written in?“

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

textapi.language('What language is this sentence written in?', function(err, resp) {
  	console.log("nLanguage Detection Results:n");
  	if (err !== null) {
    console.log("Error: " + err);
  } else {
    console.log("Language	:",resp.lang);
    console.log("Confidence	:",resp.confidence);

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

Language Detection Results:

Language	: en
Confidence	: 0.9999984486883192

Sentiment Analysis

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 endpoint will also determine if the text is subjective or objective. You can call the endpoint with the following piece of code.

textapi.sentiment('John is a very good football player!', function(err, resp) {
  console.log("nSentiment Analysis Results:n");
  if (err !== null) {
    console.log("Error: " + err);
  } else {
    console.log("Subjectivity	:",resp.subjectivity);
    console.log("Subjectivity Confidence	:",resp.subjectivity_confidence);
    console.log("Polarity	:",resp.polarity);
    console.log("Polarity Confidence	:",resp.polarity_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:

Subjectivity	: objective
Subjectivity Confidence	: 0.9896821594138254
Polarity	: positive
Polarity Confidence	: 0.9999988272764874

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 analyzes a BBC news article about the Philae Lander which found organic molecules on the surface of a comet.

textapi.classify('', function(err, resp) {
    console.log("nArticle Classification Results:n");
  	if (err !== null) {
    console.log("Error: " + err);
  } else {
    for (var i=0; i < resp.categories.length; i++){
        console.log("Label	:",resp.categories[i].label);
    	console.log("Code	:",resp.categories[i].code);
		console.log("Confidence	:",resp.categories[i].confidence);

When you run this code you should receive an output similar to that shown below which assigns the article an IPTC label of “science and technology – space program” with an IPTC code of 13008000.

Article Classification Results:

Label	: science and technology - space programme
Code	: 13008000
Confidence	: 0.9999999983009931

If Node.js is not your preferred language then check out our SDKs for Python, Ruby, PHP on our website. We will be publishing ‘getting started’ blogs for each of these languages over the coming weeks so keep an eye out for those too. If you haven’t already done so you can get free access to our API on our sign up page.


Let's Talk