Sentiment analysis in Dynamics CRM using Azure Text Analytics

Last year I created a proof-of-concept solution that showed how to integrate Dynamics CRM with HP Haven OnDemand (then called HP IDOL OnDemand) to perform sentiment analysis and index records to support "find similar" queries. While I was working through the AzureCon challenge a few weeks ago, I thought it would be an interesting exercise to update my sentiment analysis code to work with the Text Analytics offering from the Microsoft Azure Marketplace.

The approach

As with my Haven OnDemand solution, the approach I'm using with Azure relies on a custom workflow activity that does the following:

  1. Parse a supplied text input and strip any HTML tags using a helper function.
  2. Create a JSON sentiment analysis request and post it to Azure Text Analytics with an HttpWebRequest.
  3. Deserialize the JSON respsonse returned by Azure Text Analytics to a custom class object using a DataContractJsonSerializer.
  4. Return the sentiment score to the calling process.

There are two main differences with the my Azure Text Analytics solution:

  1. The Azure service only returns a sentiment score, so this custom workflow activity doesn't return a positive/negative string value.
  2. Instead of embedding an access key in the custom workflow activity code, I've made it a parameter, which means you can take the solution straight from GitHub and start using it in your CRM organization as soon as you sign up for the Text Analytics service.

The solution in action

Here's a sample dialog I've created to demonstrate the use of the custom workflow activity.
Sample dialog definition

Sample dialog definition - input prompt

Sample dialog definition - custom workflow activity call

Sample dialog definition - output display

Here's how the process works for the sample text "I hate you."

Negative text

Negative score

Wrapping it up on a more positive note, here's the same dialog with "I love you" instead.

Positive text

Positive score

As you can see, the score for "I hate you" is about .06, and the score for "I love you" is about .91, which makes sense as scores closer to one are more positive, and scores closer to zero are more negative.

The code

You can download all the custom code and a CRM solution extract from my Crm-Sample-Code repository on GitHub. Let me know what you think in the comments!

comments powered by Disqus