Skip to main content

Xamarin + Azure: Part 4 - Intelligent Apps

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.



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.
privateconststringBaseUrl = "https://westus.api.cognitive.microsoft.com/  "
privateconststringAccountKey = "xxx"

3. Add the functions to call the Cognitive Services and get the sentiment of a text.

public static asyncTask <string> GetSentiment(string text) { 
    using(var client = newHttpClient()) { 
        client.BaseAddress = newUri(BaseUrl); 
        // Request headers. 
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AccountKey); 
        client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json")); 
        // Request body. Insert your text data here in JSON format. 
        byte[] byteData = Encoding.UTF8.GetBytes("{\"documents\":[" "{\"id\":\"1\",\"text\":\"" + text + "\"},]}"); 
        // Detect sentiment: 
        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(); 
    

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.

public async Task <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); 


5 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.


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

Comments

Popular posts from this blog

Creating and Querying Microsoft Azure DocumentDB

DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.

This article is available available on theMicrosoft Technet Wiki. This article was highlighted in theTop Contributor awardson the 12th of October 2014. This article was highlighted in the TNWiki Article Spotlight. This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.
This wiki shall introduce you to this new service.

Setting up a Microsoft Azure DocumentDBGo to the new Microsoft Azure Portal. https://portal.azure.com/ 


 Click on New > DocumentDB


Enter A Database ID and hit Create!



Query Unstructured Data From SQL Server Using PolyBase

Scope The following article demonstrates how unstructured data and relational data can be queried, joined and processed in a single query using PolyBase, a new feature in SQL Server 2016. Pre-RequisitesIntroduction to Big Data Analytics Using Microsoft Azure Big Data Analytics Using Hive on Microsoft Azure Analyze Twitter Data With Hive in Azure HDInsight Running Hadoop on Linux using Azure HDInsight  Introduction Traditionally, Big Data is processed using Apache Hadoop which is totally fine. But what if the result of this needs to be linked to the traditional Relation Database? For example, assume that from the analysis of tons of application logs, marketing needs to contact some customs that faced problems in an application following a failure in the application.
This problem is solved with PolyBase. PolyBase allows you to use Transact-SQL (T-SQL) statements to access data stored in Hadoop or Azure Blob Storage and query it in an ad-hoc fashion. It also lets you query semi-structure…

Creating and Deploying Microsoft Azure WebJobs

Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.
This article focuses on the basics of WebJobs before demonstrating an example where it can be used.

This article is also available on the Mirosoft TechNet Wiki.
This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


Introduction
What is Microsoft Azure WebJobs?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.

What Scheduling Options is supported by Microsoft Azure WebJobs? Azure WebJobs can run Continuously, On Demand or on a Schedule.
In what language/scripts are WebJobs written?
Azure WebJobs can be created using the following scripts:  .cmd, .bat, .exe (using windows cmd).ps1 (using powershell).sh (using bash).php (using php).py (using python).js (using node)In this article, the use of c# command line app shall be demonstrated.
Cr…