Google Cloud Natural Language API in Android APP

When you want to do something more with your text once you’ve transcribed it.You might want to analyze it.That’s where the Natural Language API comes into play.The Natural Language API lets you extract entities, sentiment, and syntax from your text.Real world example is customer feedback platform.They enable all of their users to do is gather feedback from their app’s user as they’re going through their application.So Customer their make sense of all this open-ended feedback.It’s that open-ended text feedback that’s much more difficult for them to make sense of.That’s where the Natural Language API comes into play.Entity and Syntax annotation to pull out the key subjects and terms from the feedback, and then if necessary, route those to the right person in real time to respond to the feedback.

In this tutorial, I’ll introduce you to the Cloud Natural Language platform and show you how to use it to analyze text.


  • Google Cloud Platform account(You can use 12 months free trial)


1.Acquiring an API Key

To Use Google Cloud Natural Language API services in the app, you need an API key. You can get one by creating a new project in the Google Cloud Platform console.

Once the project has been created, go to API Manager > Dashboard and press the Enable API button.

Enable Natural Language API

To get API key, go to the Credentials tab, press the Create Credentials button, and select API key.

Google Cloud Vision API Key

2.Creating a New Android Project

Google provides client libraries in a number of programming languages to simplify the process of building and sending requests and receiving and parsing responses.

Add the following compile dependencies to the app build.gradle:

Add INTERNET permission in the AndroidManifest.xml file.


To interact with the API using the Google API Client library, you must create a CloudNaturalLanguage object using the CloudNaturalLanguage.Builder class. Its constructor also expects an HTTP transport and a JSON factory.

Furthermore, by assigning a CloudNaturalLanguageRequestInitializer instance to it, you can force it to include your API key in all its

All the text you want to analyze using the API must be placed inside a Document object. The Document object must also contain configuration information, such as the language of the text and whether it is formatted as plain text or HTML. Add the following code:

Next, you must create a Features object specifying the features you are interested in analyzing. The following code shows you how to create a Features object that says you want to extract entities and run sentiment analysis only.

Use the Document and Features objects to compose an AnnotateTextRequest object, which can be passed to the annotateText() method to generate an AnnotateTextResponse object.


Entity Analysis

So I’ve got the sentence and I sent it to the entity extraction endpoint of the Natural Language API And it return all of these as entities in my text. So we can see that each entity, we get the name of the entity, in this case, Google.The type of the entity is organization.Then we get back some metadata.MID ID that maps to Google’s Knowledge Graph.If you want to get more information about the entity, you can call Google’s Knowledge Graph API, passing it this ID.We also get the Wikipedia URL for this particular entity.

You can extract a list of entities from the AnnotateTextResponse object by calling its getEntities() method.

Analyze Entity


Sentiment Analysis

Analyze the sentiment of your text.If we have this restaurant review,

The food at that restaurant has stale,I will not be going back.

If I worked at this restaurant, I ’d and potentially follow up with this customer to see why they didn’t like it.But it’s likely that I would have lots and lots of reviews, and I probably wouldn’t want to read each one manually.I might want to flag the most positive and most negative once and then respond just to those. So we get two number back from the Natural Language API to help us do this.The first thing we get back is score, which will tell us on a scale from -1 to 1 how positive or negative is this text? In this example, we get negative 0.8, which is almost fully negative.Then we get magnitude, which tells us regardless of being positive or negative, how strong is the sentiment in this text?And this is a range from 0 to infinity, and it’s normalize based on the length of the text.So we get a pretty small number here,0.8 because this is just a small piece of text.

You can extract the overall sentiment of the transcript by calling the getDocumentSentiment() method. To get the actual score of the sentiment, however, you must also call the getScore() method, which returns a float.

analyze sentiment


Download this project from GitHub

Related Post

Google Cloud Vision API in Android APP

Google Cloud Speech API in Android APP

Leave a Reply

Your email address will not be published. Required fields are marked *