The Natural Language API is a pretrained machine learning model that can analyze syntax, extract entities, and evaluate the sentiment of text. It can called from Google Docs to perform all of these functions.

This codelab will walk you through calling the Natural Language API to recognize the sentiment of selected text in a Google Doc and highlight it based on that sentiment.

What are we going to be building?

Once this codelab is complete, you will be able to select text in a document and mark its sentiment, using a menu choice, as shown below.

Text will be highlighted in red for negative sentiment, green for positive sentiment, and yellow for neutral sentiment.

What you'll learn

How to call the Natural Language API from Google Docs

How to add menus to Google Docs

How to recognize and work with selected text in Google Docs

What you'll need

A Google account

Access to a Google Cloud Platform Project

This codelab is focused on calling the Natural Language API from Google Docs. Non-relevant concepts and code blocks are glossed over and are provided for you to simply copy and paste.

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one.

Next, you'll need to enable billing in the Developers Console in order to use Google Cloud resources like the Natural Language API. There should be no charge for using Google Cloud Platform during this codelab since the first 5,000 calls of the Natural Language API each month are free.

From within your new document, select the menu item Tools > Script editor. If you are presented with a welcome screen, click Blank Project.

Delete any code in the script editor and paste in the code below. This code will create a menu item, extract the text from the current selected text, and highlight the text based on its sentiment. It does not call the Natural Language API yet.

Reload the document to see the new menu, "Natural Language Tools", which you created, appear in the Google Docs toolbar

Select text and then the Mark Sentiment option from the Natural Language Tools menu. The first time you select this option, you will be prompted to authorize the script to run. Click Authorize, and then log in with your credentials.

Allow Natural Language Tools to view and manage documents that this application has been installed in.

Once the script is authorized, the selected text will be highlighted in yellow, since the stub for sentiment analysis always returns 0.0, which is neutral.

Once your program can extract text from the selection and highlight it, it's time to call the Natural Language API. All of this will be done in the body of the retrieveSentiment function.

Build a structure from the line passed into the function that holds the text of the line, along with its type and language. More information about supported languages can be found in the product documentation.

Save your script, reload the document, and try out the full program. You may need to re-authorize with your credentials to enable the new functionality. Select different sections of your document to see how the sentiment may differ over its parts.

You've created a Google Doc and called the Natural Language API to analyze the sentiment of selected portions of the document.

If you are done with this example, return to the Credentials section of the Google Cloud Console and delete the API key to ensure it is not used elsewhere.