Scope
The article shown below follows my presentation and demo at Xamarin Dev Days in Mauritius. The article demonstrates how to get started with Azure mobile apps and the benefits of the powers of Cloud in terms of scalability, Offline Sync, and Data Analytics.
This is the fourth part of the series, where we’ll use intelligent features of the app, using Microsoft Cognitive Services.
Introduction
The objective of this article is to build on the sample app discussed in part 3 and use Microsoft Cognitive Services to calculate the sentiment score of the feedback to determine whether it is positive or a negative feedback.
Microsoft Cognitive Services lets you build apps with powerful algorithms, using just a few lines of code. They work across the devices and platforms such as iOS, Android and Windows, keep improving, and are easy to set up.
To reach this goal, the Text Analytics API of Microsoft Cognitive Services shall be used.
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.
https://www.microsoft.com/cognitive-services/en-us/text-analytics-api To start using Text
Analytics API click “Get started for free” to get a license key.
Implementation
The objective is to automatically calculate the sentiment score when the user is submitting the feedback.
The easiest way to implement this is to modify the Cloud back-end code to add logic that will call the Text Analytics API and update the value of the field Sentiment Score as the user will submit a feedback in the Post Feedback action of the FeedbackController.
Below are the steps to proceed with this change.
- Open the back-end project and add a new class called Analytics.
- Add variables to keep the Base URL and the Account Key.
- privateconststringBaseUrl = "https://westus.api.cognitive.microsoft.com/";
- privateconststringAccountKey = "xxx";
- Add the functions to call the Cognitive Services and get the sentiment of a text.
- publicstaticasyncTask < string > GetSentiment(string text) {
- using(var client = newHttpClient()) {
- client.BaseAddress = newUri(BaseUrl);
-
- client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AccountKey);
- client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));
-
- byte[] byteData = Encoding.UTF8.GetBytes("{\"documents\":[" + "{\"id\":\"1\",\"text\":\"" + text + "\"},]}");
-
- varuri = "text/analytics/v2.0/sentiment";
- var response = awaitCallEndpoint(client, uri, byteData);
- dynamic d = JObject.Parse(response);
- returnd.documents[0].score;
- }
- }
- staticasyncTask < string > CallEndpoint(HttpClient client, stringuri, byte[] byteData) {
- using(var content = newByteArrayContent(byteData)) {
- content.Headers.ContentType = newMediaTypeHeaderValue("application/json");
- var response = awaitclient.PostAsync(uri, content);
- returnawaitresponse.Content.ReadAsStringAsync();
- }
- }
- In the Feedback Controller, amend the method Post Feedback, as shown below. This will take the Feedback text, call Cognitive Services, calculate the Sentiment Score and save it in the database.
- publicasyncTask < IHttpActionResult > PostFeedback(Feedback item) {
- string score = awaitLogic.Analytics.GetSentiment(item.FeedbackText);
- item.SentimentScore = double.Parse(score);
- Feedback current = awaitInsertAsync(item);
- returnCreatedAtRoute("Tables", new {
- id = current.Id
- }, current);
- }
- In the client Application, open the page feedbackpage.xaml and add the code, mentioned below to display the sentiment score.
- <LabelText="{Binding SentimentScore}" />
Demo
Open the Application and add new feedback.
From now on, the Sentiment Score will be saved in the database. This is an easy way to understand the feedback from an attendee.
Conclusion
In this article, we demonstrated how to add Text Analytics in the sample Application, using Microsoft Cognitive Services.
The takeaway here is to see how fast and easy it is to connect your Application to a Cloud back-end, benefit from a managed SQL Server, and connect to Machine Learning APIs.
References
- https://www.microsoft.com/cognitive-services/en-us/text-analytics/documentation
- https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-apps-text-analytics
- https://www.microsoft.com/cognitive-services