blog post

Entity Analysis using Google Cloud Natural Language API

Sachin Kumar
April 8, 2023
6 MIN READ

Entity Analysis using Google Cloud Natural Language API

In this tutorial, we will learn how to do Entity Analysis using Google Cloud Natural Language API.

In this tutorial, we will learn how to do Entity Analysis using Google Cloud Natural Language API. This tutorial is a continuation of our previous tutorial where we learned how to perform Sentiment Analysis using Google Cloud Natural Language API.

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

Requirements:

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

Setup:

Sentiment Analysis using Google Cloud Natural Language API
Sentiment Analysis using Google Cloud Natural Language API
  • Remember your project ID as shown in the screenshot below. Also, make sure to add your unique project name.
  • Bonus Tip: 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.
Sentiment Analysis using Google Cloud Natural Language API

Configuration:

  • 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.
Sentiment Analysis using Google Cloud Natural Language API
  • As a result, Search for Google Cloud Natural Language API and click on it.
Sentiment Analysis using Google Cloud Natural Language API
  • Also, click ENABLE to enable the Cloud Natural Language API. After few seconds, the API should be enabled.
Sentiment Analysis using Google Cloud Natural Language API

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.
Sentiment Analysis using Google Cloud Natural Language API
  • 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
Google Cloud Shell

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.

export API_KEY=
  • 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.

Entity Analysis using Google Cloud Natural Language API:

The Natural Language API method we'll use is analyzeEntities. With this method, the API can extract entities (like people, places, and events) from text. In this tutorial, you will learn how to extract entities (like people, places, and events) from the text that we pass into the 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 Entity Analysis on a block of text.
  • First of all, let us create a JSON request with the text that we would like to perform Entity 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.

AnalyzeEntities Request:

To try it out the API's entity analysis, we'll use the following sentence:Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series.

Sentiment Analysis using Google Cloud Natural Language API

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

Sentiment Analysis using Google Cloud Natural Language API

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


{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series."
  },
  "encodingType":"UTF8"
}
  • In the request, we tell the Natural Language API about the text we'll be sending. Supported type values are PLAIN_TEXT or 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 Entity 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 analyzeEntities endpoint.
  • Copy the curl command from here and run it in your cloud shell command line.

{
  "entities": [
     {
      "name": "Robert Galbraith",
      "type": "PERSON",
      "metadata": {
        "mid": "/m/042xh",
        "wikipedia_url": "https://en.wikipedia.org/wiki/J._K._Rowling"
      },
      "salience": 0.7980405,
      "mentions": [
        {
          "text": {
            "content": "Joanne Rowling",
            "beginOffset": 0
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "Rowling",
            "beginOffset": 53
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "novelist",
            "beginOffset": 96
          },
          "type": "COMMON"
        },
        {
          "text": {
            "content": "Robert Galbraith",
            "beginOffset": 65
          },
          "type": "PROPER"
        }
      ]
    }
  ]
}
  • Your response should look like this.

{
  "entities": [
     {
      "name": "Robert Galbraith",
      "type": "PERSON",
      "metadata": {
        "mid": "/m/042xh",
        "wikipedia_url": "https://en.wikipedia.org/wiki/J._K._Rowling"
      },
      "salience": 0.7980405,
      "mentions": [
        {
          "text": {
            "content": "Joanne Rowling",
            "beginOffset": 0
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "Rowling",
            "beginOffset": 53
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "novelist",
            "beginOffset": 96
          },
          "type": "COMMON"
        },
        {
          "text": {
            "content": "Robert Galbraith",
            "beginOffset": 65
          },
          "type": "PROPER"
        }
      ]
    }
  ]
}
  • From the response, you can clarify how we do Entity Analysis using Google Cloud Natural Language API.
  • Let's deep dive into the response and understand what is going on.
For each entity in the response, we get the following:
  • entity type
  • associated Wikipedia URL if there is one
  • salience
  • indices of where this entity appeared in the text
  • Salience is a number in the [0,1] range that refers to the centrality of the entity to the text as a whole.

Entity Analysis using Google Cloud Natural Language API can also recognize the same entity mentioned in different ways. Take a look at the mentions list in the response: the API is able to tell that "Joanne Rowling", "Rowling", "novelist" and "Robert Galbriath" all point to the same thing.

Conclusion:

That's a wrap on how to perform Entity Analysis using Google Cloud Natural Language API. Let me know your thoughts on this powerful API. Entity Sentiment, 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. Also, check out the latest tutorial on Getting Started with Flutter #machinelearning #techwithsach #entityanalysis #googlecloud #naturallanguageapi #tutorials

More Resources and Contents