DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Perform Sentiment Analysis and Classification on Text (In Java)
  • Keep Your Application Secrets Secret
  • Aggregating REST APIs Calls Using Apache Camel
  • Step-by-Step Guide to Use Anypoint MQ: Part 1

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Testing SingleStore's MCP Server
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How to Create Java NLP Apps Using Google NLP API

How to Create Java NLP Apps Using Google NLP API

Learn about setting up the development environment for the Google Cloud Natural Language API using Eclipse IDE and Java.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Mar. 04, 18 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

Natural language processing (NLP) is an AI-based technology used to create apps related to speech recognition, natural language understanding, and natural language generation. Some of the applications related to NLP are content classification, sentiment analysis, and syntactic analysis.

In this post, you will learn about how to get set up with a development environment for creating NLP-based apps using Google Cloud NLP APIs.

Set Up Eclipse-Based Development Environment for Google NLP API

The steps below will help you get set up with Eclipse IDE and a Java-based development environment for developing apps using Google Cloud Natural Language API.

  • Create Google project: Create a project by logging into Google Cloud console. I created a project NLP-Sample. Select the project that you just created.
  • Go to Cloud NLP API page: From the Google Cloud console, go to the Google Natural Language API page by searching using the keyword such as "google cloud natural language API" as shown in the diagram below. Alternatively, go to the Google Cloud Natural Language AP page and select the project you created in the previous step.
  • Enable Cloud NLP API: Enable the Google Cloud NLP API by clicking ENABLE. The following screenshot shows the ENABLE button.
  • Create service account key: Create a service account key with which you will access the Google NLP APIs from your Java programs. From the Google NLP dashboard, click Credentials, as shown in the left navigation. Then, click Create credentials and select Service account key. Enter the details in the form as shown in the diagram below and submit.This will download the service account key in a JSON file. The service account key file looks like the following:
    {
        "type": "service_account",
        "project_id": "nlp-sample-194807",
        "private_key_id": "6fcfbf135fd46c60256453621919d60ecea5564b",
        "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...XXkhk=\n-----END PRIVATE KEY-----\n",
        "client_email": "demo-57@nlp-sample-345807.iam.gserviceaccount.com",
        "client_id": "118019199276547406057",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://accounts.google.com/o/oauth2/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/demo-57%40nlp-sample-346807.iam.gserviceaccount.com"
    }
  • Create a Maven Project: Create a Maven project in Eclipse.
  • Include Google Cloud NLP API dependency in POM: Put the following in your pom.xml file:
    <dependency>
     <groupId>com.google.cloud</groupId>
     <artifactId>google-cloud-language</artifactId>
     <version>1.14.0</version>
    </dependency>
  • Configure Google Application Credentials: Configure GOOGLE_APPLICATION_CREDENTIALS as an environment variable whose value is the file path for the service account key, which gets downloaded as a result of creating a credential as service-account-key.
  • Test a sample Java app related to content classification: Place the following code in App.java:
    package com.vflux.nlpdemo;
    
    import com.google.cloud.language.v1.ClassificationCategory; 
    import com.google.cloud.language.v1.ClassifyTextRequest;
    import com.google.cloud.language.v1.ClassifyTextResponse;
    import com.google.cloud.language.v1.Document;
    import com.google.cloud.language.v1.Document.Type;
    import com.google.cloud.language.v1.LanguageServiceClient;
    
    public class App 
    {
      public static void main(String... args) throws Exception {
          String text = "The two sides also signed agreements worth around USD 50 million that includes setting up of a USD 30 million super specialty hospital after Palestinian President Mahmoud Abbas received Prime Minister Modi in an official ceremony at the presidential compound, also known as Muqata&#39;a, in Ramallah - the Palestinian seat of government. On his part, President Abbas acknowledged that the Indian leadership has always stood by peace in Palestine. Abbas said Palestine is always ready to engage in negotiations to achieve its goal of an independent state. He asked India to facilitate the peace process with Israel. We rely on India&#39;s role as an international voice of great standing and weigh through its historical role in the Non-Aligned Movement and in all international forum and its increasingly growing power on the strategic and economic levels, in a way that is conducive to just and desired peace in our region, said President Abbas. The two sides signed agreements worth USD 50 million. The agreement includes setting up of a super speciality hospital worth USD 30 million in Beit Sahur and construction of a centre for empowering women worth USD 5 million. Three agreements in the education sector worth USD 5 million and for procurement of equipment and machinery for the National Printing Press in Ramallah were also signed.";
          try (LanguageServiceClient language = LanguageServiceClient.create()) {
                // set content to the text string
                Document doc = Document.newBuilder()
                    .setContent(text)
                    .setType(Type.PLAIN_TEXT)
                    .build();
                ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
                    .setDocument(doc)
                    .build();
                // detect categories in the given text
                ClassifyTextResponse response = language.classifyText(request);
    
                for (ClassificationCategory category : response.getCategoriesList()) {
                  System.out.printf("Category name : %s, Confidence : %.3f\n",
                      category.getName(), category.getConfidence());
                }
              }
        }
    }
  • Run the above program and you would get the following output:
    Category name : /News, Confidence : 0.550
    Category name : /Law &amp; Government/Government, Confidence : 0.500

Summary

In this post, you learned how to get set up with the Google Cloud Natural Language API with Java.

Did you find this article useful? Do you have any questions or suggestions about this article in relation to setting up the development environment for the Google Cloud Natural Language API using Eclipse IDE and Java? Leave a comment and ask your questions and I shall do my best to address your queries.

NLP API Google (verb) app Java (programming language)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Perform Sentiment Analysis and Classification on Text (In Java)
  • Keep Your Application Secrets Secret
  • Aggregating REST APIs Calls Using Apache Camel
  • Step-by-Step Guide to Use Anypoint MQ: Part 1

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!