Microsoft Bot Framework Basics: Building Intelligent Bots – Adding Sentiment Analytics (Part 4)
Scope
The following articles demonstrates the use of the Text Analytics API from Microsoft Cognitive Services in a bot using the Microsoft Bot Framework. This article will build on the previous articles about Bot Framework where the basics of building a bot, the integration of Language Understanding API and Bing News API was demonstrated.
The objective of this article is to demonstrate how the bot can use sentiment analytics to determine whether a news item is positive or negative and reply the user accordingly.
The objective of this article is to demonstrate how the bot can use sentiment analytics to determine whether a news item is positive or negative and reply the user accordingly.
Text Analytics API
Text Analytics API is a suite of text analytics Services built with Azure Machine Learning. It currently offers APIs for sentiment analysis, key phrase extraction, and topic detection for English text, as well as language detection for 120 languages.
To start using Text Analytics API Jump click “Get started for free” to get a license key.
Integrating Sentiment Analytics with Bot Framework
At this stage, the bot is functional and retrieving news. Our objective now, is to make the user able to search for positive or negative news.
The first step is to subscribe to the Text Analytics API in Cognitive Services here . Note that you will get up to 5000 transactions free per month. Next, we’ll need to retrieve the Good/Bad news entity from the LUIS model to determine whether the user requested a good or bad news. We’ll then take the news results from the Bing API, calculate the sentiment score of each of them and filter it based on the Entity detected from the LUIS model.
The next step, is to create the method getSentimentScore that will take a text as parameter and return us the sentiment score. It is this method that will be used to filter the results returned by the Bing API.
private async Task < double > getSentimentScore(string documentText) {
string queryUri = “https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment “;
HttpClient client = new HttpClient() {
DefaultRequestHeaders = {
{
“Ocp-Apim-Subscription-Key”,
“XXX”
},
{
“Accept”,
“application/json”
}
}
};
var textInput = new BatchInput {
documents = new List < DocumentInput > {
new DocumentInput {
id = 1,
text = documentText,
}
}
};
var jsonInput = JsonConvert.SerializeObject(textInput);
HttpResponseMessage postMessage;
BatchResult response;
try {
postMessage = await client.PostAsync(queryUri, new StringContent(jsonInput, Encoding.UTF8, “application/json”));
response = JsonConvert.DeserializeObject < BatchResult > (await postMessage.Content.ReadAsStringAsync());
} catch (Exception e) {
return 0.0;
}
return response ? .documents[0] ? .score ? ? 0.0;
}
Next, remember that in our LUIS model we have 2 entities, GoodNews and BadNews? This is where we’ll use them!
In the search method, we’ll add the following line of codes to retrieve the Positive/Negative entities from the sentences if they are available. This will be used to detect whether we’ll filter by a good or a bad news.
var findPositive = result.TryFindEntity(“GoodNews”, out sentimentEntity);
var findNegative = result.TryFindEntity(“NegativeNews”, out sentimentEntity);
The next step is add the following code in the for loop of the search method. The only difference is that we’ll check the sentiment of each news item and decide whether it should be returned to the user of now.
var sentimentScore = await getSentimentScore(article.description);
if (findPositive && sentimentScore < 0.6) {
continue;
} else if (findNegative && sentimentScore > 0.4) {
continue;
}
Below is a screenshot of the bot when the code for Text Analytics has been added. The bot now understands the sentiment of the news!