Get an API Key
Set up your Google Doc
Using the Natural Language API from Google Docs
The Natural Language API is a pretrained machine learning model that can analyze syntax, extract entities, and evaluate the sentiment of text. It can be called from Google Docs to perform all of these functions.
This lab 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.
Once this lab 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.
This lab is focused on calling the Natural Language API from Google Docs.
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
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud Console
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account.
If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.
Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.
Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Cloud Console opens in this tab.
Task 1. Enable the Natural Language API
Before you start, make sure that the Natural Language API is enabled.
In the Google Cloud console, select Navigation menu > APIs & Services > Library:
Search for Cloud Natural Language API and click on the API to enable or to confirm that the API is enabled.
Task 2. Get an API Key
You need to generate an API user key to pass in the request URL.
To create an API key, seclect Navigation menu > APIs & Services > Credentials:
Click on Create credentials at the top and select API key:
Copy the API key to a text file or a Google Doc to use in a later step. Click Close. Once you have the API key you are ready to move into Google Docs.
Click Check my progress to verify the objective.
Task 3. Set up your Google Doc
Before you can call the Natural Language API, you'll make an Apps Script program to create the menu, link it to a function to mark the text, and extract the text from the user selection.
- Create a new Google Doc.
- From within your new document, select the menu item Extensions > Apps Script.
- 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.
- On the menu bar, click on the save project button . (The script's name is shown to end users in several places, including the authorization dialog.)
- Return to your document. Add text to your document. You can use the sample that comes from Alice in Wonderland on Project Gutenberg (copy and paste the
Plain Text UTF-8version into the document), but feel free to use any text you wish.
- 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.
Click Check my progress to verify the objective.
Task 4. Call the Natural Language API
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
- Return to the Extensions > Apps Script in Google Docs.
- In the
retrieveSentimentfunction, remove the current lines and add a variable to contain your API key, which you saved in the "Get an API key" section.
- Create a variable that will hold the URL of the Natural Language API with your API key appended to it.
- Build a structure from the line passed into the function that holds the text of the line, along with its type and language. Currently the only supported language is English.
- Build the entire data payload from the document details by adding the encoding type.
- Create a structure containing the payload and the necessary header information.
- Make the call, saving the response.
- The response will be returned in JSON format, so parse it and extract the score field, if it exists. Return either that field or 0.0.
The complete code to retrieve the sentiment is below.
- 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.
Finish your Quest
This self-paced lab is part of the Workspace Integrations Quest. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Quests.
Take your next lab
Continue your Quest with Build a Complete Database Web App with App Maker, or check out these suggestions:
Next steps / Learn more
Continue your Google Cloud learning with these suggestions:
- Take more labs. Learn more about the Natural Language API by taking labs, like Entity and Sentiment analysis with the Natural Language API. Or take something completely different like Rent-a-VM to Process Earthquake Data.
- Start a Quest. A Qwiklabs Quest is a series of related labs that form a learning path. Completing a Quest earns you a digital badge, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. See available Qwiklabs Quests.
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated September 30, 2022
Lab Last Tested September 30, 2022
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.