Use AWS Comprehend to Power your Bot Framework Text Analysis

by Michael Szul on

About a month ago, I published a blog post about using IBM Watson for text analysis in chatbots thanks to a package I published with the Bot Builder Community. This time around, let's talk about the newly released AWS Comprehend middleware.

This is yet another middleware component for text analysis built upon the idea of swapping in and out "engines" while maintaining much of the same middleware and access logic.

A few weeks ago, we made the official release, but I didn't have time to blog about it. Let's take a look at it. You'll notice how similar it is to the Text Analytics integration, as well as the Watson NLU integration.

Start by installing the package:

npm install @botbuildercommunity/middleware-aws-comprehend --save
      

Once you have that installed, you can include the AWS Comprehend middleware in your Bot Framework code just like you do with any other middleware:

import { SentimentAnalysis } from '@botbuildercommunity/middleware-aws-comprehend';
      
      adapter.use(new SentimentAnalysis());
      

You'll notice in this example, that we aren't passing anything into the constructor--not even credentials. AWS assumes two credentials. There are a couple of different ways to store and access AWS credentials, but this package assumes they're in your environment variables. In fact, the AWS SDK will check your environment variables on it's own. It looks for the following:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

See the AWS documentation for more about credentials and set up.

In the above code, we're importing the SentimentAnalysis middleware. Once you have the middleware set up, you can check the sentiment of a particular message off of the turn state with something like the following:

context.turnState.get("sentimentScore");
      

Now, the sentiment analysis for AWS is a little different than the other text analysis middleware. For AWS, you get two values:

context.turnState.get("sentimentType");
      context.turnState.get("sentimentScore");
      

With this, the type designates whether it is positive, negative, or neutral, while the score is for that particular type. With the Cognitive Services Text Analytics API, you only get a score ranging from 0 - 1 where the closer you get to one, the more positive the sentiment.

Here's a quick code snippet from the example that's inside the repository for the package:

adapter.use(new SentimentAnalysis());
      
      server.post("/api/messages", (req, res) => {
          adapter.processActivity(req, res, async (context) => {
              if (context.activity.type === "message") {
                  await context.sendActivity(`You said "${context.activity.text} with a ${context.turnState.get("sentimentType")} sentiment of ${context.turnState.get("sentimentScore")}"`);
              }
          });
      });
      

All of this assumes default options for the AWS Comprehend integration. You can also pass in options to the constructor that represent the options you would normally pass into AWS:

import { SentimentAnalysis } from '@botbuildercommunity/middleware-aws-comprehend';
      
      adapter.use(new SentimentAnalysis({ apiVersion: '2017-11-27', region: 'us-east-1', lang: 'en' }));
      

The middleware assumes options used in the example above, which is why you don't need to specify them; however, if you need to use different options, you'll have to pass them in.

For lang, this isn't an official AWS Comprehend option; however, behind the scenes AWS Comprehend methods accept a LanguageCode. This package allows you to pass that language code in with the rest of the options in the constructor.