{"id":37953,"date":"2018-10-04T01:00:22","date_gmt":"2018-10-04T05:00:22","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=37953"},"modified":"2019-03-25T14:02:30","modified_gmt":"2019-03-25T22:02:30","slug":"upgrade-your-app-with-sentiment-analysis","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/upgrade-your-app-with-sentiment-analysis\/","title":{"rendered":"Upgrade Your App with Sentiment Analysis"},"content":{"rendered":"<p>\t\t\t\tUnderstanding the sentiment of an email or text message can be difficult. Someone may send a message, intending for it to be encouraging, but it may be accidentally interpreted as mean or condescending.<\/p>\n<p>Luckily, Microsoft&#8217;s <a href=\"https:\/\/azure.microsoft.com\/services\/cognitive-services\/text-analytics\/?WT.mc_id=none-XamarinBlog-bramin\">Text Analytics Service<\/a> can help avoid any confusion.<\/p>\n<h2>Text Analytics Service<\/h2>\n<p>Microsoft&#8217;s <a href=\"https:\/\/azure.microsoft.com\/services\/cognitive-services\/text-analytics\/?WT.mc_id=none-XamarinBlog-bramin\">Cognitive Services<\/a> team have created the <a href=\"https:\/\/westus.dev.cognitive.microsoft.com\/docs\/services\/TextAnalytics.V2.0\/operations\/56f30ceeeda5650db055a3c9\/?WT.mc_id=none-XamarinBlog-bramin\">Sentiment Analysis API<\/a> that uses machine learning to determine the text sentiment. And the best part is, you don&#8217;t need to be machine learning experts to use it.<\/p>\n<p>You just submit the text in a POST Request, specifying the text&#8217;s language and a GUID Id.<\/p>\n<pre class=\"EnlighterJSRAW\">{\r\n  \"documents\": [\r\n    {\r\n      \"language\": \"en\",\r\n      \"id\": \"251c99d7-1f89-426a-a3ad-c6fa1b34f020\",\r\n      \"text\": \"I hope you find time to actually get your reports done today.\"\r\n    }\r\n  ]\r\n}<\/pre>\n<p>Then the API returns back its sentiment score:<\/p>\n<pre class=\"EnlighterJSRAW\">{\r\n\"sentiment\": {\r\n  \"documents\": [\r\n    {\r\n      \"id\": \"251c99d7-1f89-426a-a3ad-c6fa1b34f020\",\r\n      \"score\": 0.776355504989624\r\n    }\r\n  ]\r\n}<\/pre>\n<p>The sentiment score ranges between 0 and 1.<\/p>\n<p>Scores close to 0 indicate negative sentiment, while scores close to 1 indicate positive sentiment.<\/p>\n<p>Now let&#8217;s add it to a Xamarin app.<\/p>\n<h2>Sentiment Analysis + Xamarin<\/h2>\n<p>Let&#8217;s upgrade a Xamarin app to include text sentiment analysis.<\/p>\n<p>The completed Xamarin.Forms app can be found here: <a href=\"https:\/\/github.com\/brminnick\/SentimentAnalysis\">https:\/\/github.com\/brminnick\/SentimentAnalysis<\/a>.<\/p>\n<h3>1. Generate Text Analytics API Key<\/h3>\n<p>First, you&#8217;ll need to generate a Text Analytics API Key using the Azure Portal<\/p>\n<ol>\n<li>Navigate to the <a href=\"https:\/\/portal.azure.com\/?WT.mc_id=none-XamarinBlog-bramin\">Azure Portal<\/a>\n<ul>\n<li>If you are new to Azure, use <a href=\"https:\/\/azure.microsoft.com\/free\/ai\/?WT.mc_id=none-XamarinBlog-bramin\">this sign-up link<\/a> to receive a free $200 credit<\/li>\n<\/ul>\n<\/li>\n<li>On the Azure Portal, select <strong>+Create a Resource<\/strong><\/li>\n<li>In the <strong>New<\/strong>\u00a0window, select <strong>AI + Machine Learning<\/strong><\/li>\n<li>Use <strong>Featured<\/strong>\u00a0frame to select <strong>Text Analytics<\/strong><\/li>\n<li>Once the <strong>Create<\/strong>\u00a0window opens, make the following selections:\n<ul>\n<li><strong>Name<\/strong>: [Create a unique resource name]<\/li>\n<li><strong>Subscription<\/strong>: [Select your Azure subscription]<\/li>\n<li><strong>Location<\/strong>: [Select the location closest to you]<\/li>\n<li><strong>Pricing Tier<\/strong>: F0 (5K Transactions per 30 days)\n<ul>\n<li>This is a free tier<\/li>\n<\/ul>\n<\/li>\n<li><strong>Resource Group<\/strong>: [Create a unique resource group name]<\/li>\n<\/ul>\n<\/li>\n<li>Use the <strong>Create<\/strong>\u00a0window to select <strong>Create<\/strong><\/li>\n<li>On the Azure Portal, select the bell-shaped notification icon<\/li>\n<li>Stand by while the <strong>Notifications<\/strong>\u00a0window says <strong>Deployment in progress&#8230;<\/strong><\/li>\n<li>Once the deployment has finished, on the <strong>Notifications<\/strong>\u00a0window, select <strong>Go to resource<\/strong><\/li>\n<li>On the Resource page, select <strong>Keys<\/strong>\u00a0and locate <strong>KEY 1<\/strong>\n<ul>\n<li>We will use this API Key in our app<\/li>\n<\/ul>\n<\/li>\n<li>In the Resource page, select <strong>Overview<\/strong>\u00a0and locate the <strong>Endpoint<\/strong>\n<ul>\n<li>We will use this Url in our app<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>2. Install NuGet Packages<\/h3>\n<p>Next, let&#8217;s add the following NuGet Packages to our Xamarin app. Make sure to install these in each C# Project, including the .NET Standard project, the iOS-specific project, and Android-specific project:<\/p>\n<p>&#8211; <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Azure.CognitiveServices.Language.TextAnalytics\/2.1.0-preview\">Microsoft.Azure.CognitiveServices.Language.TextAnalytics v2.1.0-preview<\/a>\n&#8211; <a href=\"https:\/\/www.nuget.org\/packages\/Newtonsoft.Json\/11.0.2\">Newtonsoft.Json v11.0.2<\/a><\/p>\n<p>The TextAnalytics\u00a0SDK is a library that contains all of the boiler-plate code necessary for accessing the TextAnalytics API, including the C# Models and the HttpClient logic. You aren&#8217;t required to use the SDK, but it certainly saves you from writing many lines of code.<\/p>\n<h3>3. Implement the Sentiment Service<\/h3>\n<p>Now let&#8217;s implement the Sentiment Service in our Xamarin app.<\/p>\n<p>This code leverages the TextAnalytics SDK&#8217;s helper classes which call the Sentiment API. This allows us to implement a Sentiment Service with just a few lines of code.<\/p>\n<p>Make sure to add your API KEY and your Base Url from your Azure Text Analytics resource.<\/p>\n<div class=\"csharp\" style=\"font-family: Menlo,\">\n<p><span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System.Collections.Generic<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System.Linq<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System.Net.Http<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System.Threading<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">System.Threading.Tasks<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">Microsoft.Azure.CognitiveServices.Language.TextAnalytics<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">using<\/span> <span style=\"color: #008080\">Microsoft.Rest<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #0600ff;font-weight: bold\">namespace<\/span> SentimentAnalysis\n<span style=\"color: #008000\">{<\/span>\n<span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 public<\/span> <span style=\"color: #0600ff;font-weight: bold\">static<\/span> <span style=\"color: #6666cc;font-weight: bold\">class<\/span> SentimentService\n<span style=\"color: #008000\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>{<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/Example Base Url: https:\/\/westus.api.cognitive.microsoft.com<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>const<\/span> <span style=\"color: #6666cc;font-weight: bold\">string<\/span> _sentimentAPIBaseUrl <span style=\"color: #008000\">=<\/span> <span style=\"color: #666666\">&#8220;Add Your API&#8217;s Base Url Here&#8221;<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>const<\/span> <span style=\"color: #6666cc;font-weight: bold\">string<\/span> _textSentimentAPIKey <span style=\"color: #008000\">=<\/span> <span style=\"color: #666666\">&#8220;Add Your API Key Here&#8221;<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/HttpClient wrapper for accessing the TextAnalytics API<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>readonly<\/span> <span style=\"color: #0600ff;font-weight: bold\">static<\/span> TextAnalyticsClient _textAnalyticsApiClient <span style=\"color: #008000\">=<\/span> <a style=\"color: #000060\"><span style=\"color: #008000\">new<\/span><\/a> TextAnalyticsClient<span style=\"color: #008000\">(<\/span><a style=\"color: #000060\"><span style=\"color: #008000\">new\u00a0<\/span><\/a>ApiKeyServiceClientCredentials<span style=\"color: #008000\">(<\/span>_textSentimentAPIKey<span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">)<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><\/span>Endpoint <span style=\"color: #008000\">=<\/span> _sentimentAPIBaseUrl\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/Input the text to be analyzed and return its sentiment score. The sentiment score will range from 0 to 1, where 0 is negative sentiment and 1 is positive sentiment.<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>public<\/span> <span style=\"color: #0600ff;font-weight: bold\">static<\/span> async Task<span style=\"color: #008000\">&lt;<\/span><span style=\"color: #6666cc;font-weight: bold\">double<\/span><span style=\"color: #008000\">?&gt;<\/span> GetSentiment<span style=\"color: #008000\">(<\/span><span style=\"color: #6666cc;font-weight: bold\">string<\/span> text<span style=\"color: #008000\">)<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/Create the request object to send to the TextAnalytics API<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/The request can contain multiple text inputs which you can use to batch multiple requests into one API call<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>var request <span style=\"color: #008000\">=<\/span> <a style=\"color: #000060\"><span style=\"color: #008000\">new<\/span><\/a> MultiLanguageBatchInput<span style=\"color: #008000\">(<\/span><a style=\"color: #000060\"><span style=\"color: #008000\">new<\/span><\/a> List<span style=\"color: #008000\">&lt;<\/span>MultiLanguageInput<span style=\"color: #008000\">&gt;<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><\/span>{<\/span> <a style=\"color: #000060\"><span style=\"color: #008000\">new<\/span><\/a> MultiLanguageInput<span style=\"color: #008000\">(<\/span>id<span style=\"color: #008000\">:<\/span> <span style=\"color: #666666\">&#8220;1&#8221;<\/span>, text<span style=\"color: #008000\">:<\/span> text<span style=\"color: #008000\">)<\/span> <span style=\"color: #008000\">}<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span><span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>\/\/Get the sentiment results from the TextAnalytics API <\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>var sentimentResult <span style=\"color: #008000\">=<\/span> await _textAnalyticsApiClient<span style=\"color: #008000\">.<\/span><span style=\"color: #0000ff\">SentimentAsync<\/span><span style=\"color: #008000\">(<\/span>request<span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/Parse the sentiment score <\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>return<\/span> sentimentResult<span style=\"color: #008000\">?.<\/span><span style=\"color: #0000ff\">Documents<\/span><span style=\"color: #008000\">?.<\/span><span style=\"color: #0000ff\">FirstOrDefault<\/span><span style=\"color: #008000\">(<\/span><span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">?.<\/span><span style=\"color: #0000ff\">Score<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span>\/\/Helper class to add our API Key to the HttpRequestMessage Header<\/span>\n<span style=\"color: #6666cc;font-weight: bold\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>class<\/span> ApiKeyServiceClientCredentials <span style=\"color: #008000\">:<\/span> ServiceClientCredentials\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>readonly<\/span> <span style=\"color: #6666cc;font-weight: bold\">string<\/span> _subscriptionKey<span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>public<\/span> ApiKeyServiceClientCredentials<span style=\"color: #008000\">(<\/span><span style=\"color: #6666cc;font-weight: bold\">string<\/span> subscriptionKey<span style=\"color: #008000\">)<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>_subscriptionKey <span style=\"color: #008000\">=<\/span> subscriptionKey<span style=\"color: #008000\">;<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span><\/p>\n<p><span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>public<\/span> <span style=\"color: #0600ff;font-weight: bold\">override<\/span> Task ProcessHttpRequestAsync<span style=\"color: #008000\">(<\/span>HttpRequestMessage request, CancellationToken cancellationToken<span style=\"color: #008000\">)<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>{<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>if<\/span> <span style=\"color: #008000\">(<\/span>request <a style=\"color: #000060\"><span style=\"color: #008000\">is<\/span><\/a> <span style=\"color: #0600ff;font-weight: bold\">null<\/span><span style=\"color: #008000\">)<\/span>\n<span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>throw<\/span> <a style=\"color: #000060\"><span style=\"color: #008000\">new<\/span><\/a> ArgumentNullException<span style=\"color: #008000\">(<\/span>nameof<span style=\"color: #008000\">(<\/span>request<span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>request<span style=\"color: #008000\">.<\/span><span style=\"color: #0000ff\">Headers<\/span><span style=\"color: #008000\">.<\/span><span style=\"color: #0000ff\">Add<\/span><span style=\"color: #008000\">(<\/span><span style=\"color: #666666\">&#8220;Ocp-Apim-Subscription-Key&#8221;<\/span>, _subscriptionKey<span style=\"color: #008000\">)<\/span><span style=\"color: #008000\">;<\/span><\/p>\n<p><span style=\"color: #0600ff;font-weight: bold\"><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span><span style=\"color: #008080;font-style: italic\">\u00a0 \u00a0 <\/span>return<\/span> Task<span style=\"color: #008000\">.<\/span><span style=\"color: #0000ff\">CompletedTask<\/span><span style=\"color: #008000\">;<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span>\n<span style=\"color: #008000\"><span style=\"color: #008080;font-style: italic\"><span style=\"color: #0600ff;font-weight: bold\">\u00a0 \u00a0 <\/span><\/span>}<\/span>\n<span style=\"color: #008000\">}<\/span><\/p>\n<\/div>\n<p>Now we can connect a simple UI to our Sentiment Service and instantly know whether a message is happy or sad.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/user-images.githubusercontent.com\/13558917\/45986950-97533080-c023-11e8-82db-668fbcf9fce5.gif\" width=\"1000\" height=\"700\" \/><\/p>\n<h2>Learn More<\/h2>\n<p>Visit the Microsoft Docs to learn more about Cognitive Services:<\/p>\n<p>&#8211; <a href=\"https:\/\/azure.microsoft.com\/services\/cognitive-services\/?WT.mc_id=none-XamarinBlog-bramin\">Cognitive Services<\/a>\n&#8211; <a href=\"https:\/\/azure.microsoft.com\/services\/cognitive-services\/text-analytics\/?WT.mc_id=none-XamarinBlog-bramin\">Text Analytics<\/a>\n&#8211; <a href=\"https:\/\/westus.dev.cognitive.microsoft.com\/docs\/services\/TextAnalytics.V2.0\/operations\/56f30ceeeda5650db055a3c9\/?WT.mc_id=none-XamarinBlog-bramin\">Sentiment Analysis API<\/a><\/p>\n<h3>About The Author<\/h3>\n<p>Brandon Minnick is a Developer Advocate at Microsoft. As a Developer Advocate, Brandon loves helping developers build cloud-connected mobile apps! Brandon loves talking about mobile and invites you to start up a conversation: <a href=\"https:\/\/twitter.com\/intent\/user?user_id=3418408341\">@TheCodeTraveler<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure&#8217;s Text Analytics Service makes it easy to add sentiment analysis to our cross-platform apps. Let&#8217;s see how with this to implement it in a Xamarin app!<\/p>\n","protected":false},"author":568,"featured_media":40949,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[856,2,556,291],"tags":[867,4],"class_list":["post-37953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud","category-developers","category-integrations","category-xamarin-platform","tag-azure","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Azure&#8217;s Text Analytics Service makes it easy to add sentiment analysis to our cross-platform apps. Let&#8217;s see how with this to implement it in a Xamarin app!<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/37953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/568"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=37953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/37953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/40949"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=37953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=37953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=37953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}