Natural Language Standalone Hello World



Natural Language Standalone Hello World


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

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

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>io.happycoding</groupId>
  <artifactId>google-cloud-natural-language-hello-world-standalone</artifactId>
  <version>1</version>

  <properties>
    <exec.mainClass>io.happycoding.naturallanguage.NaturalLanguageHelloWorld</exec.mainClass>
    <exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

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

</project>

NaturalLanguageHelloWorld.java

NaturalLanguageHelloWorld.java contains a main() method that sends a sentiment analysis request and outputs the result.

package io.happycoding.naturallanguage;

import com.google.cloud.language.v1.AnalyzeSentimentResponse;
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 = "Happy coding!";

    // Create a Document containing plain text content
    Document doc = Document.newBuilder()
      .setContent(text).setType(Type.PLAIN_TEXT).build();

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

    // Close the service
    languageService.close();

    // 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

Comments

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!