Getting Started With Xamarin And Azure - Intelligent Apps - Part Four

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.
Text Analytics API
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.
  1. Open the back-end project and add a new class called Analytics.
  2. Add variables to keep the Base URL and the Account Key.
    1. privateconststringBaseUrl = "https://westus.api.cognitive.microsoft.com/";  
    2. privateconststringAccountKey = "xxx";  
  3. Add the functions to call the Cognitive Services and get the sentiment of a text.
    1. publicstaticasyncTask < string > GetSentiment(string text) {  
    2.     using(var client = newHttpClient()) {  
    3.         client.BaseAddress = newUri(BaseUrl);  
    4.         // Request headers.  
    5.         client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AccountKey);  
    6.         client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));  
    7.         // Request body. Insert your text data here in JSON format.  
    8.         byte[] byteData = Encoding.UTF8.GetBytes("{\"documents\":[" + "{\"id\":\"1\",\"text\":\"" + text + "\"},]}");  
    9.         // Detect sentiment:  
    10.         varuri = "text/analytics/v2.0/sentiment";  
    11.         var response = awaitCallEndpoint(client, uri, byteData);  
    12.         dynamic d = JObject.Parse(response);  
    13.         returnd.documents[0].score;  
    14.     }  
    15. }  
    16. staticasyncTask < string > CallEndpoint(HttpClient client, stringuri, byte[] byteData) {  
    17.     using(var content = newByteArrayContent(byteData)) {  
    18.         content.Headers.ContentType = newMediaTypeHeaderValue("application/json");  
    19.         var response = awaitclient.PostAsync(uri, content);  
    20.         returnawaitresponse.Content.ReadAsStringAsync();  
    21.     }  
    22. }  
  4. 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.
    1. publicasyncTask < IHttpActionResult > PostFeedback(Feedback item) {  
    2.     string score = awaitLogic.Analytics.GetSentiment(item.FeedbackText);  
    3.     item.SentimentScore = double.Parse(score);  
    4.     Feedback current = awaitInsertAsync(item);  
    5.     returnCreatedAtRoute("Tables"new {  
    6.         id = current.Id  
    7.     }, current);  
    8. }  
  5. In the client Application, open the page feedbackpage.xaml and add the code, mentioned below to display the sentiment score.
    1. <LabelText="{Binding SentimentScore}" />  

Demo

Open the Application and add new feedback.

Text Analytics API

From now on, the Sentiment Score will be saved in the database. This is an easy way to understand the feedback from an attendee.

Text Analytics API

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

Up Next
    Ebook Download
    View all
    Learn
    View all