Sentiment Analysis inspects the given text and identifies the prevailing
emotional opinion within the text, especially to determine a writer's attitude
as positive, negative, or neutral. Sentiment analysis is performed through the
analyzeSentiment
method. For information on which languages are supported by the Natural Language API,
see Language Support. For information on
how to interpret the score
and magnitude
sentiment values included in the
analysis, see Interpreting sentiment analysis values.
This section demonstrates a few ways to detect sentiment in a document. For each document, you must submit a separate request.
Analyzing Sentiment in a String
Here is an example of performing sentiment analysis on a text string sent directly to the Natural Language API:
Protocol
To analyze sentiment in a document, make a POST
request to the
documents:analyzeSentiment
REST 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 a service account set up for the
project using the Google Cloud Platform gcloud CLI.
For instructions on installing the gcloud CLI,
setting up a project with a service account
see the Quickstart.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ 'encodingType': 'UTF8', 'document': { 'type': 'PLAIN_TEXT', 'content': 'Enjoy your vacation!' } }" "https://language.googleapis.com/v2/documents:analyzeSentiment"
If you don't specify document.language_code
, then the language will be automatically
detected. For information on which languages are supported by the Natural Language API,
see Language Support. See
the Document
reference documentation for more information on configuring the request body.
If the request is successful, the server returns a 200 OK
HTTP status code and
the response in JSON format:
{ "documentSentiment": { "magnitude": 0.8, "score": 0.8 }, "language": "en", "sentences": [ { "text": { "content": "Enjoy your vacation!", "beginOffset": 0 }, "sentiment": { "magnitude": 0.8, "score": 0.8 } } ] }
documentSentiment.score
indicates positive sentiment with a value greater than zero, and negative
sentiment with a value less than zero.
gcloud
Refer to the analyze-sentiment
command for complete details.
To perform sentiment analysis, use the gcloud CLI and
use the --content
flag to identify the content to analyze:
gcloud ml language analyze-sentiment --content="Enjoy your vacation!"
If the request is successful, the server returns a response in JSON format:
{ "documentSentiment": { "magnitude": 0.8, "score": 0.8 }, "language": "en", "sentences": [ { "text": { "content": "Enjoy your vacation!", "beginOffset": 0 }, "sentiment": { "magnitude": 0.8, "score": 0.8 } } ] }
documentSentiment.score
indicates positive sentiment with a value greater than zero, and negative
sentiment with a value less than zero.
Go
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Go API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Java API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Python API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Natural Language reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Natural Language reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Natural Language reference documentation for Ruby.
Analyzing Sentiment from Cloud Storage
For your convenience, the Natural Language API can perform sentiment analysis directly on a file located in Cloud Storage, without the need to send the contents of the file in the body of your request.
Here is an example of performing sentiment analysis on a file located in Cloud Storage.
Protocol
To analyze sentiment from a document stored in Cloud Storage,
make a POST
request to the
documents:analyzeSentiment
REST method and provide
the appropriate request body with the path to the document
as shown in the following example.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ 'document':{ 'type':'PLAIN_TEXT', 'gcsContentUri':'gs://<bucket-name>/<object-name>' } }" "https://language.googleapis.com/v2/documents:analyzeSentiment"
If you don't specify document.language_code
, then the language will be automatically
detected. For information on which languages are supported by the Natural Language API,
see Language Support. See
the Document
reference documentation for more information on configuring the request body.
If the request is successful, the server returns a 200 OK
HTTP status code and
the response in JSON format:
{ "documentSentiment": { "magnitude": 0.8, "score": 0.8 }, "language_code": "en", "sentences": [ { "text": { "content": "Enjoy your vacation!", "beginOffset": 0 }, "sentiment": { "magnitude": 0.8, "score": 0.8 } } ] }
documentSentiment.score
indicates positive sentiment with a value greater than zero, and negative
sentiment with a value less than zero.
gcloud
Refer to the analyze-sentiment
command for complete details.
To perform sentiment analysis on a file in Cloud Storage, use the gcloud
command line tool and use the --content-file
flag to identify the file
path that contains the content to analyze:
gcloud ml language analyze-sentiment --content-file=gs://YOUR_BUCKET_NAME/YOUR_FILE_NAME
If the request is successful, the server returns a response in JSON format:
{ "documentSentiment": { "magnitude": 0.8, "score": 0.8 }, "language": "en", "sentences": [ { "text": { "content": "Enjoy your vacation!", "beginOffset": 0 }, "sentiment": { "magnitude": 0.8, "score": 0.8 } } ] }
documentSentiment.score
indicates positive sentiment with a value greater than zero, and negative
sentiment with a value less than zero.
Go
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Go API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Java API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Node.js API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Natural Language, see Natural Language client libraries. For more information, see the Natural Language Python API reference documentation.
To authenticate to Natural Language, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C#:
Please follow the
C# setup instructions
on the client libraries page
and then visit the
Natural Language reference documentation for .NET.
PHP:
Please follow the
PHP setup instructions
on the client libraries page
and then visit the
Natural Language reference documentation for PHP.
Ruby:
Please follow the
Ruby setup instructions
on the client libraries page
and then visit the
Natural Language reference documentation for Ruby.
Additional languages