Sentiment Analysis with Google Cloud’s Natural Language API and Node
By: Ross Sicherman
Tags: Google Cloud, Node, Sentiment Analysis, API, Natural Language

First, we need to create a project on the Google Cloud Platform Console (https://console.cloud.google.com/)

Next, create a new project by clicking the drop down in the navbar (to the left of the search bar), then click NEW PROJECT in the modal.

Name your project and click the CREATE button on the bottom left.

Once your project is created, it should be available for selection in the same modal from the previous step. Select the project name and then click the OPEN button on the bottom right of the modal.

The project name should now be displayed in the navbar dropdown (to the left of the search bar). Next, navigate to API’s and Services — Library.

Search for Cloud Natural Language API and click on the result

Click Enable

When the API is enabled, click the hamburger button on the top left to bring up the lefthand navigation pane. Then navigate to APIs & Services — Credentials

We’re going to need a service account credential for this API. In the center Credentials box, click Create credentials — Service account key

The service account should be configured like below. For Role, you should choose Project — Owner. Click the Create button on the bottom left when you’re done with the form. Note: This will trigger a file download. The file includes sensitive information that we’ll need to access the API later. Save it somewhere you’ll remember (project directory?) and make sure it is secure!

Finally, we’re ready to code! Create a new project directory and move the downloaded service account key file to the directory (if it’s not there already).

Next, install the google cloud language API npm package to your project

npm install @google-cloud/language

Create a new file (index.js) and type or copy the following code -

The client connection (line 3) will allow us to run Google’s Cloud Natural Language Sentiment Analysis on our input text.

Next, let’s create variables for the text we want to analyze. I’ve defined two const variables with positive and negative text (positiveText and negativeText, respectfully) -

The text needs to be sent to the Cloud Natural Language API in a specific format. See the positiveDocument and negativeDocument consts below -

Here, we’re formatting our positiveDocument and negativeDocument objects to tell the Natural Language API that it will receive plain text and the content will be positiveText or negativeText

Lastly, we call to the API’s sentiment analyzer (analyzeSentiment function) and log the results to the console -

Note analyzeSentiment requires an object with a document property as an argument.

Now we’re almost ready to run the script. One last minor step —

The @google-cloud/language package needs to know where the service account key file is. To do this, we need to set a specific environment variable (GOOGLE_APPLICATION_CREDENTIALS) to the location of the file. Recall earlier we saved the file in the root of our project directory. Thus — the command should look like this -

GOOGLE_APPLICATION_CREDENTIALS=./secret.json node index.js

(The above command assumes your terminal is currently in the project’s root directory. I also renamed my credentials file to secret.json to simplify the command as the default filename that google generates can be long)

After execution is complete, the console should display result for each document -

Each result includes each sentence’s sentiment score and magnitude (sentences array) and full text sentiment score and magnitude (documentSentiment object).

So what exactly are score and magnitude?

Score corresponds to the overall emotional leaning of the text. It ranges between -1.0 (negative) and 1.0 (positive).

corresponds to the overall emotional leaning of the text. It ranges between (negative) and (positive). Magnitude indicates the overall strength of emotion (both positive and negative) within the given text. Longer text blocks may have greater magnitudes.

(For more about natural language sentiment analysis features, including score and magnitude, see the Natural Language API documentation from Google)

In our example, the positiveText document’s results lean heavily towards positive sentiment, with an overall score of 0.8 for the full text (documentSentiment). Conversely, negativeText document’s results lean towards a negative sentiment with an overall score of -0.3 for the full text.