Sentiment Analysis in Dynamics CRM using Azure Text Analytics
-
Upload
lucas-alexander -
Category
Technology
-
view
810 -
download
0
Transcript of Sentiment Analysis in Dynamics CRM using Azure Text Analytics
Sentiment Analysis in Dynamics CRM using Azure Text AnalyticsLucas Alexander | April 11, 2016
About Me
Dynamics CRM technical architect @ Tribridge Working with Dynamics CRM since 2005 Lives in Auburn, Alabama, USA Blog – alexanderdevelopment.net Twitter – @lucas_is GitHub – github.com/lucasalexanderLucas Alexander
Agenda
What is Azure Machine Learning? Azure ML Text Analytics API Interoperability methods CRM applications for sentiment analysis Solution approach Code deep dive Demonstration Further reading
Additional Resources: FAQ -
https://azure.microsoft.com/en-us/documentation/articles/machine-learning-faq/
Pricing - https://azure.microsoft.com/en-us/pricing/details/machine-learning/
What is Azure Machine Learning?
Azure Machine Learning is a fully managed service that you can use to create, test, operate and manage predictive analytic solutions in the cloud. With only a browser, you can sign-in, upload data and immediately start machine learning experiments. Drag-and-drop predictive modeling, a large pallet of modules, and a library of starting templates makes common machine learning tasks simple and quick.
- https://azure.microsoft.com/en-us/services/machine-learning/
Azure ML Text Analytics API
The Text Analytics API is a suite of text analytics services built with Azure Machine Learning. No training data is needed to use this API, just bring your text data. This API uses advanced natural language processing techniques under the hood.
- https://datamarket.azure.com/dataset/amla/text-analytics
Additional resources• Documentation –
https://azure.microsoft.com/en-us/documentation/articles/machine-learning-apps-text-analytics/
• Interactive demo – https://text-analytics-demo.azurewebsites.net/
Note – currently English is the only language supported for sentiment analysis and key phrase extraction.
Go to https://datamarket.azure.com/dataset/amla/text-analytics
Click the sign up button for the free tier (or pay if you want)
Follow the prompts Copy the primary access key
from the management portal
Azure ML Text Analytics API – sign up
Stands for Representational State Transfer
Lighter weight alternative to SOAP
Think web services based on URLs
Can exchange data in a variety of formats (XML, JSON, etc.)
Interoperability methods – REST
Representational state transfer (REST) is an abstraction of the architecture of the World Wide Web . . .
REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.
- Wikipedia
JavaScript Object Notation
Lightweight data-interchange format
Typically more human-readable than XML
Interoperability methods – JSON
{"Inputs":[
{"Id":"1","Text":"hello world"},{"Id":"2","Text":"hello foo world"},{"Id":"3","Text":"hello my world"},
]}
Route emails to queues based on sentiment Dynamically load agent scripts Reporting on interactions
CRM applications for sentiment analysis
Solution approach
Format text as JSON object and
post to Text Analytics API
Process text, generate
sentiment score (0 to 1)
Supply text for sentiment
analysis
Return responseDisplay/process result accordingly
Return score to calling process
Parse JSON response, extract sentiment score
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.
Custom workflow activity
Workflow activity – Request/response
Request{
"Inputs":[ {"Id":"1","Text":"hello world"},{"Id":"2","Text":"hello foo world"},{"Id":"3","Text":"hello my world"}
]}
Response{
"odata.metadata":"https://api.datamarket.azure.com/data.ashx/amla/text-analytics/v1/$metadata", "SentimentBatch":[
{"Score":0.9549767,"Id":"1"},{"Score":0.7767222,"Id":"2"},{"Score":0.8988889,"Id":"3"}
], "Errors":[]
}
Workflow activity – Inputs and outputs
/// <summary>/// text to analyze/// </summary>[RequiredArgument][Input("Text Input")]public InArgument<String> TextInput { get; set; }
/// <summary>/// azure text analytics access key /// </summary>[RequiredArgument][Input("API Key")]public InArgument<String> ApiKey { get; set; }
/// <summary>/// sentiment score - numbers closer to 1 are more positive, numbers closer to 0 are more negative/// </summary>[Output("Score")]public OutArgument<Decimal> Score { get; set; }
Workflow activity – build web request
//strip any html characters from the text to analyzeinputText = HtmlTools.StripHTML(inputText);tracingService.Trace(string.Format("stripped text: {0}", inputText));
//escape any double quote characters (") so they don't cause problems when posting the json laterstring escapedText = inputText.Replace("\"", "\\\"");tracingService.Trace(string.Format("escaped text: {0}", escapedText));
//create the webrequest objectHttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_webAddress);
//set request content type so it is treated as a regular form postreq.ContentType = "application/x-www-form-urlencoded";
//set method to postreq.Method = "POST";
//build the requestStringBuilder postData = new StringBuilder();
//note the Id value set to 1 - we can hardcode this because we're only sending a batch of onepostData.Append("{\"Inputs\":[{\"Id\":\"1\",\"Text\":\""+escapedText+"\"}]}");
Workflow activity – post web request
//set the authentication using the azure service access keystring authInfo = "AccountKey:" + ApiKey.Get(executionContext);authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));req.Headers["Authorization"] = "Basic " + authInfo;
//create a streambyte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData.ToString());req.ContentLength = bytes.Length;System.IO.Stream os = req.GetRequestStream();os.Write(bytes, 0, bytes.Length);os.Close();
//post the request and get the responseSystem.Net.WebResponse resp = req.GetResponse();
Workflow activity – handle web response
//deserialize the response to a SentimentBatchResult objectSentimentBatchResult sentimentResult = new SentimentBatchResult();System.Runtime.Serialization.Json.DataContractJsonSerializer deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(sentimentResult.GetType());sentimentResult = deserializer.ReadObject(resp.GetResponseStream()) as SentimentBatchResult;
Deserialization of response to a C# object is done using the approach outlined in my "Posting/processing JSON in a Dynamics CRM 2011 custom workflow activity" blog post
Workflow activity – deserialization
/// <summary>/// class to represent sentiment result batch response/// </summary>[DataContract]public class SentimentBatchResult{
[DataMember(Name = "SentimentBatch")]public List<SentimentBatchResultItem> SentimentBatch { get; set; }
[DataMember(Name = "Errors")]public List<ErrorRecord> Errors { get; set; }
}
SentimentBatchResult – C# class to represent the overall response object
Workflow activity – deserialization
/// <summary>/// class to represent individual sentiment result item/// </summary>[DataContract]public class SentimentBatchResultItem{
[DataMember(Name = "Score")]public Decimal Score { get; set; }
[DataMember(Name = "Id")]public String Id { get; set; }
}
SentimentBatchResultItem – C# class to represent a single sentiment score object
Workflow activity – deserialization
/// <summary>/// class to represent individual error item/// </summary>[DataContract]public class ErrorRecord{
[DataMember(Name = "Id")]public String Id { get; set; }
[DataMember(Name = "Message")]public String Message { get; set; }
}
ErrorRecord – C# class to represent a single error object
CRM dialog – definition
CRM dialog – prompt
CRM dialog – parameters
CRM dialog – response
CRM demo – negative text
CRM demo – positive text
Blog post – http://alexanderdevelopment.net/post/2015/10/12/sentiment-analysis-in-dynamics-crm-using-azure-text-analytics/
Code – https://github.com/lucasalexander/Crm-Sample-Code/tree/master/CrmAzureTextAnalysis
Further reading