Skip to main content

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.

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) {
 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!

Comments

Popular posts from this blog

Creating and Querying Microsoft Azure DocumentDB

DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.

This article is available available on theMicrosoft Technet Wiki. This article was highlighted in theTop Contributor awardson the 12th of October 2014. This article was highlighted in the TNWiki Article Spotlight. This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.
This wiki shall introduce you to this new service.

Setting up a Microsoft Azure DocumentDBGo to the new Microsoft Azure Portal. https://portal.azure.com/ 


 Click on New > DocumentDB


Enter A Database ID and hit Create!



Query Unstructured Data From SQL Server Using PolyBase

Scope The following article demonstrates how unstructured data and relational data can be queried, joined and processed in a single query using PolyBase, a new feature in SQL Server 2016. Pre-RequisitesIntroduction to Big Data Analytics Using Microsoft Azure Big Data Analytics Using Hive on Microsoft Azure Analyze Twitter Data With Hive in Azure HDInsight Running Hadoop on Linux using Azure HDInsight  Introduction Traditionally, Big Data is processed using Apache Hadoop which is totally fine. But what if the result of this needs to be linked to the traditional Relation Database? For example, assume that from the analysis of tons of application logs, marketing needs to contact some customs that faced problems in an application following a failure in the application.
This problem is solved with PolyBase. PolyBase allows you to use Transact-SQL (T-SQL) statements to access data stored in Hadoop or Azure Blob Storage and query it in an ad-hoc fashion. It also lets you query semi-structure…

Creating and Deploying Microsoft Azure WebJobs

Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.
This article focuses on the basics of WebJobs before demonstrating an example where it can be used.

This article is also available on the Mirosoft TechNet Wiki.
This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


Introduction
What is Microsoft Azure WebJobs?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.

What Scheduling Options is supported by Microsoft Azure WebJobs? Azure WebJobs can run Continuously, On Demand or on a Schedule.
In what language/scripts are WebJobs written?
Azure WebJobs can be created using the following scripts:  .cmd, .bat, .exe (using windows cmd).ps1 (using powershell).sh (using bash).php (using php).py (using python).js (using node)In this article, the use of c# command line app shall be demonstrated.
Cr…