tutorials / google-cloud / natural-language

Natural Language

tutorial javascript google cloud natural-language

Cloud Natural Language is a set of tools designed for parsing and understanding unstructured text, aka natural language. For example:

The Cloud Natural Language APIs use machine learning models that have already been trained, so you can skip straight to the fun stuff. It’s also possible to train your own models (you can learn more about that here), but this guide is going to stick with the pre-trained models.

Enable Cloud Natural Language API

Before we can use the Cloud Translation API, we have to enable it. Go here:

https://console.developers.google.com/apis/library/language.googleapis.com

Make sure your project is selected, and click the Enable button.

Credentials

The Cloud Natural Language API requires your project’s credentials to work. When you deploy to App Engine this will work ~magically~ automatically, but when running or deploying locally you have to set your credentials manually. Follow the steps here to set up your local credentials.

Important: Before proceeding, make sure you have your GOOGLE_APPLICATION_CREDENTIALS environment variable set. Nothing will work without this.

Maven Dependency

As mentioned above, the Cloud Natural Language API allows us to write code that analyzes text. It’s available as a web service, or as a library that can be called from many languages. We’re going to use it as a Java library.

To add the library to our classpath, we can use this maven dependency:

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-language</artifactId>
  <version>1.55.0</version>
</dependency>

You can read the documentation for the Java library here, but it’s probably easiest to work through an example.

Hello World

(You can view the full source of this example here.)

import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;
import java.io.IOException;

public class NaturalLanguageHelloWorld {

  public static void main(String[] args) throws IOException {

    String text = "I love coding!";
    Document doc = Document.newBuilder()
        .setContent(text).setType(Type.PLAIN_TEXT).build();

    LanguageServiceClient languageService = LanguageServiceClient.create();
    Sentiment sentiment = languageService.analyzeSentiment(doc).getDocumentSentiment();
    languageService.close();

    System.out.println("Score: " + sentiment.getScore());
  }
}

This code creates a new Document that contains "I love coding!" and passes it into a LanguageServiceClient, which returns the analysis in a Sentiment instance.

We can then get the score of the sentiment, which is a value between -1 and 1, representing how negative or positive the text is.

Try running this code with a few different values for the text variable. Can you generate a negative score?

Web App

The above example performs sentiment analysis in a standard Java application. This is useful if you want to build a desktop application or analyze some text on your computer. But you can also use the Natural Language API in server code, which comes in handy if you want to build a web app.

(You can download the full code for this example here.)

We can start with some HTML:

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Sentiment Analysis</title>
  </head>
  <body>
    <h1>Sentiment Analysis</h1>
    <p>Type a message and click submit to analyze its sentiment:</p>

    <form method="POST" action="/sentiment">
      <textarea name="message"></textarea>
      <br/>
      <button>Submit</button>
    </form>

  </body>
</html>

This HTML file renders a <form> element that contains a single text area.

sentiment analysis form

When the user clicks the Submit button, the form sends a POST request to the /sentiment URL. This request is then handled by a servlet:

SentimentAnalysisServlet

package io.happycoding.servlets;

import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/sentiment")
public class SentimentAnalysisServlet extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String message = request.getParameter("message");

    Document doc = Document.newBuilder()
        .setContent(message).setType(Document.Type.PLAIN_TEXT).build();
    LanguageServiceClient languageService = LanguageServiceClient.create();
    Sentiment sentiment = languageService.analyzeSentiment(doc).getDocumentSentiment();
    float score = sentiment.getScore();
    languageService.close();

    // Output the sentiment score as HTML.
    // A real project would probably store the score in Datastore.
    response.setContentType("text/html;");
    response.getOutputStream().println("<h1>Sentiment Analysis</h1>");
    response.getOutputStream().println("<p>You entered: " + message + "</p>");
    response.getOutputStream().println("<p>Sentiment analysis score: " + score + "</p>");
    response.getOutputStream().println("<p><a href=\"/\">Back</a></p>");
  }
}

This servlet gets the sentiment analysis score of the text entered by the user. In a real project, we’d probably store the score in Datastore or do some additional processing, but this example outputs the score directly as HTML.

sentiment analysis result

Learn More