Sentiment Analysis using Google Cloud Natural Language API

This tutorial walks you through Sentiment Analysis using Google Cloud Natural Language API using an analyzesentiment request, which performs sentiment analysis on text. Sentiment analysis attempts to determine the overall attitude (positive or negative) and is represented by numerical score and magnitude values. Keep an eye on my blog for more tutorials dealing with Google Cloud Machine Learning APIs. This tutorial is a step by step approach to setup, configure, enable and use Google Cloud Machine Learning API to perform Sentiment Analysis.

Sentiment Analysis using Google Cloud Natural Language API:

The Cloud Natural Language API lets you extract entities from text, perform sentiment and syntactic analysis, and classify text into categories. So, we will explore all of these in this quick tutorial.


  • A Google Cloud Platform Project
  • Browsers such as Chrome or Firefox


  • Remember your project ID as shown in the screenshot below. Also, make sure to add your unique project name.
  • If this is your first time on Google Cloud Platform, new users are eligible for a 300$ credit.
  • Also, you will have to enable billing for your account.


  • Since account creation and Project setup are complete, let us proceed to configure our project to enable the Google Cloud Natural Language API.
  • Go to Dashboard menu and click on APIs & Services.
  • In addition to that click on Enable APIS AND SERVICES.
  • As a result, Search for Google Cloud Natural Language API and click on it.
  • Also, click ENABLE to enable the Cloud Natural Language API. After few seconds, the API should be enabled.

Activate Cloud Shell:

  • Google Cloud Shell is a shell environment for managing resources hosted on Google Cloud Platform. It is a command line environment running in the cloud.
  • Also, we’ll use Cloud Shell to create our request to the Natural Language API.
  • First of all, click on the top right icon on Activate Shell Icon on the top right corner of the header bar as shown below.
  • As a result, A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt. Wait until the user@project:~$ prompt appears

Create an API Key:

  • We need to generate an API Key because we will be using curl to send a request to the Natural Language API.
  • To create an API key, navigate to the Credentials section of APIs & services in your Cloud console:
  • Click Create credentials dropdown and choose API Key.
  • Hence, you will see a pop-up window with your generated API Key. Copy the API key and save it safely within your system.
  • Let’s go back to the Google Cloud Shell command line and enter the following command.
  • Replace <YOUR_API_KEY> with the API Key that we copied in the previous step. Executing the above line in the terminal makes sure that the API_KEY has added to the environment variables and is not required to be called for each request.

Sentiment Analysis using Google Cloud Natural Language API:

  • Finally, we have come to the part where all the machine learning and magic happens.
  • The Natural language API lets you perform Sentiment Analysis on a block of text.
  • First of all, let us create a JSON request with the text that we would like to perform Sentiment Analysis.
  • In your Cloud Shell environment, create the file request.json with the code below.
  • You can either create the file using one of your preferred command line editors (nano, vim, emacs) or use the built-in Orion editor in Cloud Shell as shown below.

As a result, this will launch the editor in Cloud Shell. Click on File->New->File and enter file name as request.json

Copy the contents from below and paste it into your request.json file.

"content":"Inception is one of the best movies of all time. I think everyone should watch it."
"encodingType": "UTF8"
  • In the request, we tell the Natural Language API about the text we’ll be sending. Supported type values are PLAIN_TEXTor HTML. In content, we pass the text to send to the Natural Language API for analysis.
  • The Natural Language API also supports sending files stored in Cloud Storage for text processing. If we wanted to send a file from Cloud Storage, we would replace content with gcsContentUri and give it a value of our text file’s uri in Cloud Storage.
  • encodingType tells the API which type of text encoding to use when processing our text.

Call the Natural Language API to perform Sentiment Analysis:

  • You can now pass the request along with the API Key environment variable that we saved earlier to the Natural Language API through a curl command as shown below.
  • We will send the request to the API’s analyseSetiment endpoint.
  • Copy the curl command from here and run it in your cloud shell command line.
curl "${API_KEY}" -s -X POST -H "Content-Type: application/json" --data-binary @request.json
  • Your response should look like this:
  • From the response, you can clarify how we do Sentiment Analysis using Google Cloud Natural Language API.
  • Let’s deep dive into the response and understand what is going on. You will see two parts. One is documentSentiment which is for the whole block of text or document. The other is we have sentiment broken down by each sentence.
  • The Sentiment Score varies for each individual sentence.
  • The magnitude shows the intensity or magnitude value of that sentence.
  • a magnitude is a number ranging from 0 to infinity. It represents the weight of sentiment expressed in the statement, regardless of being positive or negative. Longer blocks of text with heavily weighted statements have higher magnitude values.
  • The Sentiment score is a number and ranges from -1 to +1.
  • -1 is really bad to +1 being very good. Anything close to 0 is a neutral score.
  • From the above response, you can see that the overall score for the document was 0.5 with a magnitude of 1.1. Whereas the sentiment score for the first sentence is 0.9 (which means really good). Also, the second sentence is 0.1 respectively.

Time to Play:

  • Let us perform another request with a different text showcasing some negative statements to see how Sentiment Analysis using Google Cloud Natural Language API performs.
  • Go back to request.json file and edit the contents to add some negative feedbacks.
"content":"The food was really bad at this restaurant. However, really enjoyed the drinks."
"encodingType": "UTF8"
  • From the response, you can see that the first sentence has a -0.9 sentiment score.
  • The second statement 0.8 sentiment score.
  • But the overall sentiment score for the entire feedback was neutral with a score of 0.

Important Use-Cases:

  • The power of this API is really limitless with its endless possibilities.
  • Once such use-case is integrating it with my Google Assistant app to fetch users feedback.
  • The feedback is then processed using Sentiment Analysis via a webhook.


That’s a wrap on how to perform Sentiment Analysis using Google Cloud Natural Language API.

Let me know your thoughts on this powerful API.

Entity, Syntax and Multilingual Natural Language Processing will be covered in the upcoming tutorials.

Therefore, see you all in the next tutorial. Keep an eye on my blog section for more interesting tutorials.