Natural Language Standalone Hello World

Natural Language Standalone Hello World

September 27, 2020

example java google-cloud natural-language

This is a command line program that uses Google Cloud Natural Language to perform sentiment analysis on some text.

View the code for this project here.

Download the code as a .zip from DownGit here.

This example prints out the sentiment score for the string "Happy coding!".


pom.xml is a Maven POM file that defines the project.

<project xmlns=""




</project> contains a main() method that sends a sentiment analysis request and outputs the result.

package io.happycoding.naturallanguage;


public class NaturalLanguageHelloWorld {

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

    String text = "Happy coding!";

    // Create a Document containing plain text content
    Document doc = Document.newBuilder()

    // Get the document's sentiment score
    LanguageServiceClient languageService = LanguageServiceClient.create();
    AnalyzeSentimentResponse response = languageService.analyzeSentiment(doc);
    Sentiment sentiment = response.getDocumentSentiment();

    // Close the service

    // Print the sentiment score
    float sentimentScore = sentiment.getScore();
    System.out.println("Score: " + sentimentScore);

Run this project by executing this command:

mvn package exec:java

Learn more in these tutorials:

Natural Language Examples


Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!