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.