Build A Tool in the Google docs that read the sentiment of your document by using Google’s Natural Language API

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 post 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 post is complete, you will be able to select text in a document and mark its sentiment, using a menu choice, as shown below.

4347c01adce8a7bd.png

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

e87ab533eb3056d0.png

it is focused on calling the Natural Language API from Google Docs.

log in into your Google cloud console :

Choose an account

Enable the Natural Language API

In the Google Cloud console, select Navigation menu > APIs & Services > Library:

3b8addd9600e5ac5.png

Search for Natural Language API and click on the API to enable or to confirm that the API is enabled.

36ac0b824d45d28.png

Get an API Key

You’ll need to generate an API user key to pass in the request URL.

  1. To create an API key, seclect Navigation menu > APIs & Services > Credentials:
  2. In the left pane, click on Create credentials and select API key:
bc4940935c1bef7f.png
  1. 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.

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.

  1. Create a new Google Doc.
  2. From within your new document, select the menu item Tools > Script editor. If you are presented with a welcome screen, click Blank Project.
  3. 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.
/**
* @OnlyCurrentDoc
*
* The above comment directs Apps Script to limit the scope of file
* access for this add-on. It specifies that this add-on will only
* attempt to read or modify the files in which the add-on is used,
* and not all of the user's files. The authorization request message
* presented to users will reflect this limited scope.
*/

/**
* Creates a menu entry in the Google Docs UI when the document is
* opened.
*
*/
function onOpen() {
  var ui = DocumentApp.getUi();

  ui.createMenu('Natural Language Tools')
    .addItem('Mark Sentiment', 'markSentiment')
    .addToUi();
}
/**
* Gets the user-selected text and highlights it based on sentiment
* with green for positive sentiment, red for negative, and yellow
* for neutral.
*
*/
function markSentiment() {
  var POSITIVE_COLOR = '#00ff00';  //  Colors for sentiments
  var NEGATIVE_COLOR = '#ff0000';
  var NEUTRAL_COLOR = '#ffff00';
  var NEGATIVE_CUTOFF = -0.2;   //  Thresholds for sentiments
  var POSITIVE_CUTOFF = 0.2;

  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var string = getSelectedText();

    var sentiment = retrieveSentiment(string);

    //  Select the appropriate color
    var color = NEUTRAL_COLOR;
    if (sentiment <= NEGATIVE_CUTOFF) {
      color = NEGATIVE_COLOR;
    }
    if (sentiment &gt;= POSITIVE_CUTOFF) {
      color = POSITIVE_COLOR;
    }

    //  Highlight the text
    var elements = selection.getSelectedElements();
    for (var i = 0; i < elements.length; i++) {
      if (elements[i].isPartial()) {
        var element = elements[i].getElement().editAsText();
        var startIndex = elements[i].getStartOffset();
        var endIndex = elements[i].getEndOffsetInclusive();
        element.setBackgroundColor(startIndex, endIndex, color);

      } else {
        var element = elements[i].getElement().editAsText();
        foundText = elements[i].getElement().editAsText();
        foundText.setBackgroundColor(color);
      }
    }
  }
}
/**
 * Returns a string with the contents of the selected text.
 * If no text is selected, returns an empty string.
 */
function getSelectedText() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  var string = "";
  if (selection) {
    var elements = selection.getSelectedElements();

    for (var i = 0; i < elements.length; i++) {
      if (elements[i].isPartial()) {
        var element = elements[i].getElement().asText();
        var startIndex = elements[i].getStartOffset();
        var endIndex = elements[i].getEndOffsetInclusive() + 1;
        var text = element.getText().substring(startIndex, endIndex);
        string = string + text;

      } else {
        var element = elements[i].getElement();
        // Only translate elements that can be edited as text; skip
        // images and other non-text elements.
        if (element.editAsText) {
          string = string + element.asText().getText();
        }
      }
    }
  }
  return string;
}

/** Given a string, will call the Natural Language API and retrieve
  * the sentiment of the string.  The sentiment will be a real
  * number in the range -1 to 1, where -1 is highly negative
  * sentiment and 1 is highly positive.
*/
function retrieveSentiment (line) {
//  TODO:  Call the Natural Language API with the line given
//         and return the sentiment value.
  return 0.0;
}

More details about Apps Script are available at the App Script site.

  1. Select the menu item File > Save all. Name your new script Natural Language Toolsand click OK. (The script’s name is shown to end users in several places, including the authorization dialog.)
  2. 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-8 version into the document), but feel free to use any text you wish.
  3. Reload the document to see the new menu, Natural Language Tools, which you created, appear in the Google Docs toolbar.
  4. 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.
  5. Allow Natural Language Tools to view and manage documents that this application has been installed in.
  6. 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.

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 retrieveSentiment function.

Find all the details of the Natural Language API here.

  1. Return to the Tools > Script editor in Google Docs.
  2. In the retrieveSentiment function, remove the current lines and add a variable to contain your API key, which you saved in the “Get an API key” section.
  var apiKey = "your key here";

  1. Create a variable that will hold the URL of the Natural Language API with your API key appended to it.
var apiEndpoint =
'https://language.googleapis.com/v1/documents:analyzeSentiment?key='
+ apiKey;

  1. 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.
  var docDetails = {
    language: 'en-us',
    type: 'PLAIN_TEXT',
    content: line
  };

  1. Build the entire data payload from the document details by adding the encoding type.
  var nlData = {
    document: docDetails,
    encodingType: 'UTF8'
  };

  1. Create a structure containing the payload and the necessary header information.
  var nlOptions = {
    method : 'post',
    contentType: 'application/json',
    payload : JSON.stringify(nlData)
  };

  1. Make the call, saving the response.
  var response = UrlFetchApp.fetch(apiEndpoint, nlOptions);

  1. 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.
  var data = JSON.parse(response);

  var sentiment = 0.0;
  //  Ensure all pieces were in the returned value
  if (data && data.documentSentiment
          && data.documentSentiment.score){
     sentiment = data.documentSentiment.score;
  }

  return sentiment;

The complete code to retrieve the sentiment is below.

function retrieveSentiment (line) {
  var apiKey = "your key here";
  var apiEndpoint =
'https://language.googleapis.com/v1/documents:analyzeSentiment?key='
+ apiKey;

  //  Create a structure with the text, its language, its type,
  //  and its encoding
  var docDetails = {
    language: 'en-us',
    type: 'PLAIN_TEXT',
    content: line
  };

  var nlData = {
    document: docDetails,
    encodingType: 'UTF8'
  };

  //  Package all of the options and the data together for the call
  var nlOptions = {
    method : 'post',
    contentType: 'application/json',
    payload : JSON.stringify(nlData)
  };

  //  And make the call
  var response = UrlFetchApp.fetch(apiEndpoint, nlOptions);

  var data = JSON.parse(response);

  var sentiment = 0.0;
  //  Ensure all pieces were in the returned value
  if (data && data.documentSentiment
          && data.documentSentiment.score){
     sentiment = data.documentSentiment.score;
  }

  return sentiment;
}

  1. 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.

congratulations you made it

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s