Sentiment analysis inspects user input and identifies the prevailing subjective opinion, especially to determine a user's attitude as positive, negative, or neutral. When making a detect intent request, you can specify that sentiment analysis be performed, and the response will contain sentiment analysis values.
The Natural Language API is used by Dialogflow to perform this analysis. For more information on that API and documentation on interpreting Dialogflow sentiment analysis results:
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.
You should do the following before reading this guide:
- Read Dialogflow basics.
- Perform setup steps.
Create an agent
If you have not already created an agent, create one now:
- Go to the Dialogflow ES console.
- If requested, sign in to the Dialogflow Console. See Dialogflow console overview for more information.
- Click Create Agent in the left sidebar menu. (If you already have other agents, click the agent name, scroll to the bottom and click Create new agent.)
- Enter your agent's name, default language, and default time zone.
- If you have already created a project, enter that project. If you want to allow the Dialogflow Console to create the project, select Create a new Google project.
- Click the Create button.
Import the example file to your agent
The steps in this guide make assumptions about your agent, so you need to import an agent prepared for this guide. When importing, these steps use the restore option, which overwrites all agent settings, intents, and entities.
To import the file, follow these steps:
-
Download the
room-booking-agent.zip
file. - Go to the Dialogflow ES console.
- Select your agent.
- Click the settings settings button next to the agent name.
- Select the Export and Import tab.
- Select Restore From Zip and follow instructions to restore the zip file that you downloaded.
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.
- Click the settings settings button next to the agent name.
- Select the Advanced tab.
- Toggle Enable sentiment analysis for the current query on.
Use the Dialogflow simulator
You can interact with the agent and receive sentiment analysis results via the Dialogflow simulator:
Type "Thank you for helping me."
See the SENTIMENT section at the bottom of the simulator. It should show a positive sentiment score.
Next, type "It didn't work at all." in the simulator.
See the SENTIMENT section at the bottom of the simulator. It should show 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, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.