Sentiment analysis inspects user input and identifies the prevailing subjective opinion to determine a user's attitude as positive, negative, or neutral. When making a detect intent request, you can enable sentiment analysis, and the response will contain sentiment analysis values.
The Natural Language API is used by Dialogflow to perform this analysis. For more information on the API and documentation on interpreting Dialogflow sentiment analysis results, see the following:
Supported languages
For a list of supported languages, see the sentiment column on the
Languages page. If you request sentiment
analysis for an unsupported language, your detect intent request does not fail,
but the QueryResult.diagnostic_info field contains error information.
Before you begin
This feature is only applicable when using the API for end-user interactions. If you are using an integration, you can skip this guide.
Before you begin, complete the following steps:
- Read Dialogflow basics.
- Perform setup steps.
Create an agent
- Go to the Dialogflow ES console.
- Sign in to the console if prompted. For more information, see the Dialogflow console overview.
- In the sidebar menu, expand Loading agents.
- Click Create new agent.
- Enter the agent name, default language, and default time zone.
- Enter an existing project. To let the Dialogflow console create a project, select Create a new Google project.
- Click Create.
Agent settings for sentiment analysis
You can trigger sentiment analysis per detect intent request, or you can configure your agent to always return sentiment analysis results.
To enable sentiment analysis for all queries:
- Go to the Dialogflow ES console.
- Select an agent.
- Select the settings settings icon next to the agent name.
- Select the Advanced tab.
- Turn on Enable sentiment analysis for the current query.
Use the Dialogflow simulator
You can interact with the agent and receive sentiment analysis results using the Dialogflow simulator:
- Enter "Thank you for helping me."
- View the SENTIMENT section at the bottom of the simulator. It shows a positive sentiment score.
- Enter "It didn't work at all." in the simulator.
- View the SENTIMENT section at the bottom of the simulator. It shows a negative sentiment score.
Detect intent
To detect intent, call the detectIntent method on the
Sessions type.
REST
Call thedetectIntent method and provide the sentimentAnalysisRequestConfig
field.
Before using any of the request data, make the following replacements:
- PROJECT_ID: your Google Cloud project ID
- SESSION_ID: a session ID
HTTP method and URL:
POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent/sessions/SESSION_ID:detectIntent
Request JSON body:
{
"queryParams": {
"sentimentAnalysisRequestConfig": {
"analyzeQueryTextSentiment": true
}
},
"queryInput": {
"text": {
"text": "please reserve an amazing meeting room for six people",
"languageCode": "en-US"
}
}
}
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{
"responseId": "747ee176-acc5-46be-8d9a-b7ef9c2b9199",
"queryResult": {
"queryText": "please reserve an amazing meeting room for six people",
"action": "room.reservation",
"parameters": {
"date": "",
"duration": "",
"guests": 6,
"location": "",
"time": ""
},
"fulfillmentText": "I can help with that. Where would you like to reserve a room?",
...
"sentimentAnalysisResult": {
"queryTextSentiment": {
"score": 0.8,
"magnitude": 0.8
}
}
}
}
Notice that the sentimentAnalysisResult field contains score and
magnitude values.
Java
To authenticate to Dialogflow CX, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow CX, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Dialogflow CX, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.