Product

Getting Started with AYLIEN Text Analysis API and Java

Introduction

This is a continuation of our getting up and running with AYLIEN Text Analysis API blog series. In our first ‘getting started’ blog, we went through the process of signing up for the API, obtaining your Application ID, Application Key and making calls with Node.JS. For this blog, we are going to focus on working with the API using Java.

We’re going to walk you through how easy it is to perform some basic Text Analysis processes like, detecting what language a piece of text is written in, analyzing the sentiment of a piece of text and finally generating some hashtags for a URL that can be used for maximum exposure when sharing content on social media.

To give you an overview of what can be achieved, we will first look at the code in action. We will then go through the code, section by section, to investigate each of the API endpoints used.

 

Overview of the code in action

The complete code snippet is given at the end of this blog for you to copy and paste. To run it, open a text editor such as notepad or sublime text and copy and paste the snippet. Ensure you replace the YOUR_APP_ID and YOUR_APP_KEY placeholders in the code with the application id and application key which you received when you signed up for the APIs.

Save the file as TextAPISample.java and then open a windows command prompt.
Navigate to the folder where you saved the code snippet and compile the code by typing “javac TextAPISample.java”

Note: You will need to have the java development kit (jdk) installed to compile and run this example, you can download it here.

 

 

You can then run the code by typing “java TextAPISample”. Once you run it, you should receive the output as shown below.

 

image

 

In this case we have detected that the text is written in English and the sentiment or polarity of the text is positive. We have also analyzed and generated hashtags for a URL, that points to a BBC wildlife photography story.

The detail above shows the code running in its entirety, but to highlight each feature/end-point we will now go through the code snippet, section by section, to explain the workings of Language Detection, Sentiment Analysis, and finally Hashtag Suggestion.

Language Detection

Using the language detection endpoint you can analyze a piece of text or a URL. In the function we have used in this demo code, the parameter “textOrUrl” controls whether the call is made specifying the text directly or as a URL.

public static Language getLanguage(String text, String textOrUrl)
{
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("language", parameters);
    Language language = new Language();
    NodeList nodeList = doc.getElementsByTagName("lang");
    Node langNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("confidence");
    Node confNode = nodeList.item(0);
    language.setLang(langNode.getTextContent());
    language.setConfidence(Double.parseDouble(confNode.getTextContent()));
 
    return language;
}

In this case we have specified that it should analyze the following text “John is a very good football player!” and as you can see from the output below, it determined that the text is written English. Note: For all of the endpoints, the API returns the text which was analysed for reference and we have included it in the output in each case.

Result:

John is a very good football player!
Language: en (0.999998)

 

Sentiment Analysis

Similarly, the Sentiment Analysis endpoint can take a piece of text or a URL and analyze it.

public static Sentiment getSentiment(String text, String textOrUrl)
{
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("sentiment", parameters);
    Sentiment sentiment = new Sentiment();
    NodeList nodeList = doc.getElementsByTagName("polarity");
    Node polarityNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("polarity_confidence");
    Node confNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("text");
    Node textNode = nodeList.item(0);
    sentiment.setText(textNode.getTextContent());
    sentiment.setPolarity(polarityNode.getTextContent());
    sentiment.setPolarityConfidence(Double.parseDouble(confNode.getTextContent()));
 
    return sentiment;
}

In this case, we have also specified that it should analyze the text “John is a very good football player!”. The API has determined that the sentiment of the piece of text is positive.

Result:

John is a very good football player!
Sentiment: positive (0.999984)

 

Hashtag Suggestions

Finally, the Hashtag Suggestion endpoint, analyses a URL and generates a list of hashtag suggestions:

public static Hashtags getHashtags(String text, String textOrUrl)
{
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("hashtags", parameters);
     NodeList nodeList = doc.getElementsByTagName("hashtag");
    Hashtags hashtags = new Hashtags();
    List<String> hts = new ArrayList<String>();
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node currentNode = nodeList.item(i);
      hts.add(currentNode.getTextContent());
    }
 
    hashtags.setHashtags(hts.toArray(new String[hts.size()]));
 
    nodeList = doc.getElementsByTagName("text");
    Node textNode = nodeList.item(0);
    hashtags.setText(textNode.getTextContent());
 
    return hashtags;
}

 

For hashtag suggestions, we have used an article about wildlife photography published on the BBC news website http://www.bbc.com/news/science-environment-29701853. The hashtag endpoint first extracts the text from the URL (which is returned for reference by the call and the start of which I have shown below) and then analyses that text and generates hashtag suggestions.

Results:

Hashtags: #France #BBCWildlife #BBCNews #Infrared #ZoomLens #SerengetiNationalPark #UnitedStates #Elephant #BBC #MultipleExposure #GEO #UK #RockMusic #NaturalHistoryMuseumLondon #GeostationaryOrbit #Haze #FocalLength #BBC #UnitedKingdom #SouthAfrica #Serengeti #ChemicalFormula

The text analyzed for hashtag suggestions is shown here for reference…

Slumbering lions win top wildlife photo prize

A stark image of lions resting on a rock outcrop in the Serengeti has won the 2014 Wildlife Photographer of the Year (WPY) Award…

For more getting started guides and code snippets to help you get up and running with our API, visit our Getting Started page on our website. If you haven’t already done so you can get free access to our API on our sign up page.

 

The complete code snippet:


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
 
class Language {
  private String lang;
  private String text;
  private Double confidence;
 
  public String getLang() {
    return lang;
  }
  public void setLang(String lang) {
    this.lang = lang;
  }
 
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public Double getConfidence() {
    return confidence;
  }
  public void setConfidence(Double confidence) {
    this.confidence = confidence;
  }
}
 
class Hashtags {
  private String[] hashtags;
  private String text;
  public String[] getHashtags() {
    return hashtags;
  }
 
  public void setHashtags(String[] hashtags) {
    this.hashtags = hashtags;
  }
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
}
 
class Sentiment {
  private String polarity;
  private String text;
  private Double polarityConfidence;
 
  public String getPolarity() {
    return polarity;
  }
  public void setPolarity(String polarity) {
    this.polarity = polarity;
  }
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public Double getPolarityConfidence() {
    return polarityConfidence;
  }
  public void setPolarityConfidence(Double confidence) {
    this.polarityConfidence = confidence;
  }
}
 
class TextAPISample {
  private static final String APPLICATION_ID = "YOUR_APP_ID";
  private static final String APPLICATION_KEY ="YOUR_APP_KEY";
 
  public static void main(String[] args) {
    String text = "John is a very good football player!";
    String textOrUrl = "text";
    Language lang = getLanguage(text, textOrUrl);
    System.out.printf("n%sn",
        lang.getText());
    System.out.printf("Language: %s (%f)n",
        lang.getLang(), lang.getConfidence());
    Sentiment sent = getSentiment(text,textOrUrl);
    System.out.printf("n%sn",
        sent.getText());
    System.out.printf("Sentiment: %s (%f)n",
        sent.getPolarity(), sent.getPolarityConfidence());
    textOrUrl = "url";
    Hashtags hashtags = getHashtags("http://www.bbc.com/news/science-environment-29701853", textOrUrl);
    StringBuilder sb = new StringBuilder();
    for (String s: hashtags.getHashtags()) {
      sb.append(" " + s);
    }
    System.out.printf("nHashtags:%sn", sb.toString());
    System.out.printf("nThe text analyzed for hashtag suggestions is shown here for reference...n");
        System.out.printf("n%snn",
        hashtags.getText());
  }
 
  public static Language getLanguage(String text, String textOrUrl) {
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("language", parameters);
    Language language = new Language();
    NodeList nodeList = doc.getElementsByTagName("lang");
    Node langNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("confidence");
    Node confNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("text");
    Node textNode = nodeList.item(0);
    language.setText(textNode.getTextContent());
    language.setLang(langNode.getTextContent());
     language.setConfidence(Double.parseDouble(confNode.getTextContent()));
 
    return language;
  }
 
  public static Hashtags getHashtags(String text, String textOrUrl) {
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("hashtags", parameters);
     NodeList nodeList = doc.getElementsByTagName("hashtag");
    Hashtags hashtags = new Hashtags();
    List<String> hts = new ArrayList<String>();
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node currentNode = nodeList.item(i);
      hts.add(currentNode.getTextContent());
    }
 
    hashtags.setHashtags(hts.toArray(new String[hts.size()]));
 
    nodeList = doc.getElementsByTagName("text");
    Node textNode = nodeList.item(0);
    hashtags.setText(textNode.getTextContent());
 
    return hashtags;
  }
 
  public static Sentiment getSentiment(String text, String textOrUrl) {
    final Map<String, String> parameters;
    parameters = new HashMap<String, String>();
    parameters.put(textOrUrl, text);
    Document doc = callAPI("sentiment", parameters);
    Sentiment sentiment = new Sentiment();
    NodeList nodeList = doc.getElementsByTagName("polarity");
    Node polarityNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("polarity_confidence");
    Node confNode = nodeList.item(0);
    nodeList = doc.getElementsByTagName("text");
    Node textNode = nodeList.item(0);
    sentiment.setText(textNode.getTextContent());
    sentiment.setPolarity(polarityNode.getTextContent());
     sentiment.setPolarityConfidence(Double.parseDouble(confNode.getTextContent()));
 
    return sentiment;
  }
 
  public static Document callAPI(String endpoint, Map<String, String> parameters) {
    URL url;
    HttpURLConnection connection = null;
 
    try {
      String queryString = "";
      StringBuilder sb = new StringBuilder();
      for (Map.Entry<String, String> e: parameters.entrySet()) {
        if (sb.length() > 0) { sb.append('&'); }
        sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=')
          .append(URLEncoder.encode(e.getValue(), "UTF-8"));
      }
      queryString = sb.toString();
      url = new URL("https://api.aylien.com/api/v1/" + endpoint);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty(
          "Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty(
          "Content-Length", Integer.toString(queryString.getBytes().length));
      connection.setRequestProperty("Accept", "text/xml");
      connection.setRequestProperty(
          "X-AYLIEN-TextAPI-Application-ID", APPLICATION_ID);
      connection.setRequestProperty(
          "X-AYLIEN-TextAPI-Application-Key", APPLICATION_KEY);
      connection.setDoInput(true);
      connection.setDoOutput(true);
 
      DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
      dos.writeBytes(queryString);
      dos.flush();
      dos.close();
 
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      InputSource xis = new InputSource(connection.getInputStream());
 
      return builder.parse(xis);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
    }
  }
}





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