Perform sentiment analysis by using the gcloud tool
This page shows you how to get started with the Cloud Natural Language API using the Google Cloud SDK.
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
Make an entity analysis request
gcloud
Use thegcloud
command line tool to call the analyze-entities
command
and use the --content
flag to
specify the text to analyze.
gcloud ml language analyze-entities --content="Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'."
Command-line
Usecurl
to make a POST
request to the documents:analyzeEntities
method and provide the appropriate request body as shown in the following
example.
The example uses the gcloud auth application-default print-access-token
command to obtain an access token for the service account that you created
when you set up the project.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ "https://language.googleapis.com/v1/documents:analyzeEntities" \ --data "{ 'document':{ 'type':'PLAIN_TEXT', 'content':'Michelangelo Caravaggio, Italian painter, is known for \'The Calling of Saint Matthew\'.' }, 'encodingType':'UTF8' }"
You should see a response similar to the following:
{ "entities": [ { "name": "Michelangelo Caravaggio", "type": "PERSON", "metadata": { "wikipedia_url": "http://en.wikipedia.org/wiki/Caravaggio", "mid": "/m/020bg" }, "salience": 0.83047235, "mentions": [ { "text": { "content": "Michelangelo Caravaggio", "beginOffset": 0 }, "type": "PROPER" }, { "text": { "content": "painter", "beginOffset": 33 }, "type": "COMMON" } ] }, { "name": "Italian", "type": "LOCATION", "metadata": { "mid": "/m/03rjj", "wikipedia_url": "http://en.wikipedia.org/wiki/Italy" }, "salience": 0.13870546, "mentions": [ { "text": { "content": "Italian", "beginOffset": 25 }, "type": "PROPER" } ] }, { "name": "The Calling of Saint Matthew", "type": "EVENT", "metadata": { "mid": "/m/085_p7", "wikipedia_url": "http://en.wikipedia.org/wiki/The_Calling_of_St_Matthew_(Caravaggio)" }, "salience": 0.030822212, "mentions": [ { "text": { "content": "The Calling of Saint Matthew", "beginOffset": 69 }, "type": "PROPER" } ] } ], "language": "en" }
Clean up
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
Get started with the Natural Language API in your language of choice by using a Cloud Natural Language Client Library.
Consult the Natural Language Basics for conceptual information on forming Natural Language API requests and handling responses.
Try the Sentiment Analysis Tutorial and browse the Sample Applications.