Smarter ideas worth writing about.

Introduction to the Microsoft Cognitive Services Suite

When navigating the sea of tech buzzwords and trends, one topic for the past couple years has consistently held the interest of developers and consumers alike. “Cognitive Computing”, while being old in concept and theory, is just now coming to the forefront of use in a wide array of products and applications. The complexity and cost of aggregating learning data, producing complicated deep learning algorithms, and training machine learning models poses a problem for smaller companies who want to integrate AI and machine learning into their applications.

Fortunately, Microsoft Cognitive Services gives us a way to access cognitive functionality from an easy to use SDK and a powerful API. Though the SDK is still in preview, we can start to use and understand how to interface with these powerful API’s and how we can introduce value-adding business logic into our applications. This blog is aimed at helping developers get familiar with working with the Microsoft Cognitive Services Suite and to demonstrate just how easy it is to build a valuable application using social media data and Cognitive Services.

The Project

In this demo, we will be using ASP.NET Core 2.0, Twitter’s API, and Microsoft Cognitive Services to build a simple MVC application that will search Twitter for tweets that an account is mentioned in, analyze those tweets for sentiment and key phrases, and return and curate the results of the Cognitive Services API so that we can derive some helpful output. This type of functionality can be very useful if you have a large set of text to analyze for patterns. I invite you to clone this project from my GitHub to have as a reference while you read.


Twitter’s API

For the sake of brevity, I have already configured my application to access Twitter’s API and implemented some code with the help of the CoreTweet library to search Twitter for tweets mentioning a user-entered Twitter handle. For more information on registering your application and using the Twitter API, please view the Twitter API Documentation.

Microsoft Cognitive Service’s

For us to access Microsoft Cognitive Services, we will first need an Azure subscription. If you don’t already have one, you can get one for free by visiting the Microsoft Azure page. Once we have an Azure Subscription, we will need to setup a Cognitive Services resource in Azure. Microsoft Cognitive Services offers an array of different API endpoints that provide different cognitive functionality ranging from image and facial recognition to intelligent recommendations and semantic search. You can read more about the features offered by Microsoft Cognitive Services here.

For our application, we are going to make use of the Text Analytics API endpoint. This endpoint offers sentiment analyses and key phrase extraction features. To set up our Cognitive Services resource we will want to login to our Azure Dashboard and click the “+ New” button in the top left-hand corner. In the search box, we can search for “Cognitive Services”. Select the Cognitive services option in the dropdown search menu and this will bring us to the Create blade.

Once in the Create blade, we will need to give our service a name and select the subscription it is associated with. We then want to select “Text Analytics API” from the dropdown list and select a Resource group. We won’t cover Resource groups in this post, so we will just create a new Resource Group called “Cognitive Resources” and for resource group location I am going to select “East US 2”. Now we can click the create button and Azure will generate access keys for us to use the Text Analytics API. In the resource overview for the Cognitive Service resource, under the “Keys” tab we can find our access key that we will use to access the Cognitive Services API. Microsoft provides a page for us to test our API subscription key. On this page, we can select our API region, enter our API key, and input some test data. This is useful for viewing what the API accepts as input and what it provides for output.


The Microsoft Cognitive Services SDK will give us an easy way for us to interface with our API endpoint. We will need to add the Cognitive Services SDK to our project. We can add the SDK in Visual Studio by going to Tools => NuGet Package Manager => Package Manager Console and entering:

Install-Package Microsoft.Azure.CognitiveServices.Language -Version 1.0.0-preview

You can also do this through the NuGet Package Manager. Just make sure the “Include Prerelease” checkbox is selected.

For our demo, we will pull in the following namespaces.

  • Microsoft.Azure.CognitiveServices.Language.TextAnalytics
  • Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models

These contain the “TextAnalyticsAPI” class and associated model classes we need to interact with our Cognitive Services endpoint. This library is still in preview, but it will allow us a clean and efficient way for us to interface with the Text Analytics API and will work just fine for this demo. However, if we wanted to just use HTTP requests to interface with the API, we certainly can.


For this demo, we are going to use the following two features of the Text Analytics API.

  • Sentiment Analysis
    This functionality will read the piece of text we want to analyze and evaluate whether the text expresses a positive or negative sentiment by returning a sentiment score. The sentiment score is a value ranging from 0 to 1 with 0 representing a very negative sentiment and 1 representing a very positive sentiment. In our code we will implement some logic to ascertain the significance of the sentiment.

  • Key Phrase Extraction
    This functionality will return a list of strings that the API has identified to be the key points or subjects of the piece of text we are trying to analyze. This will let us identify the subject of the tweet we are analyzing.

These two functionalities together are very powerful because we can derive negative or positive sentiment and isolate the subject of that attitude. When evaluating large sets of data, this can be useful for recognizing patterns and common subjects that bring about positive or negative reactions.

The Code

For our demo, I have added a service class to call the Text analytics API. I called this class “TextAnalyticsService”.

public class TextAnalysisService : ITextAnalysis
        private ITextAnalyticsAPI _textAnalyticsAPI;
        private IList<MultiLanguageInput> _multiLanguageInput;
        private IList<AnalysisResult> _analysisResults;
        private const string _language = "en";

        public TextAnalysisService(TextAnalyticsAPI textAnalyticsAPI, IList<MultiLanguageInput> multiLanguageInput, IList<AnalysisResult> analysisResults)
            _textAnalyticsAPI = textAnalyticsAPI;
            _multiLanguageInput = multiLanguageInput;
            _analysisResults = analysisResults;

I am using dependency injection to inject all the dependencies our Text Analytics service needs. The “textAnalyticsAPI” instance is a singleton that contains the following properties:

  • AzureRegion – This is an enum that is included within our Cognitive Services model namespace. The current SDK only supports values for certain regions. For our project, I have this property set to “Eastus2”

  • SubscriptionKey – This API key is the one we got from our “Keys” tab in Azure. I have this set in our “appsettings.json” file, but it is important to keep this key secure and never check it into source control.

We have a constant called “_language” that is set to a value of “en” for English. In a real-life application, we could make use of the language recognition API that Cognitive Services offers and map each tweet to its proper language because Twitter is used by users of many different languages. For the sake of this demo, we will just assume all of the tweets we are analyzing are in English.

In our “TextAnalyticsService”, we have exposed a method called “AnalyzeTweets”. This method iterates through the list of tweets and formats them for processing by the API.

public IEnumerable<AnalysisResult> AnalyzeTweets(IEnumerable<string> tweets)
            for(int i = 0;i < tweets.Count(); i++)
                _multiLanguageInput.Add(new MultiLanguageInput
                    Language = _language,
                    Id = i.ToString(),
                    Text = tweets.ToList()[i]

            MultiLanguageBatchInput batchInput = new MultiLanguageBatchInput(_multiLanguageInput);

            var analysisResults = GetAnalysisResultsAsync(batchInput).Result;

            return analysisResults;

The API calls to the sentiment and key phrases endpoints take a “MultiLanguageBatchInput” object as an argument. There exist models for a Language input who’s default language is English and an input model for just a single text input, but currently the method’s used to call the API’s only accept a “MultiLanguageBatchInput” because the SDK is still in preview. To construct this “MultiLanguageBatchInput”, we must pass in a list of “MultiLanguageInput”. This object has 3 properties

  • Language – the language of the text we are analyzing

  • Id – a unique string id for the text in the list. For our demo I am just setting this from a counter. This Id field will be useful when we want to associate an API result with an input.

  • Text – the text that the API will analyze.

Once our tweets are ready for the API – we will invoke the “KeyPhrasesAsync” method and the “SentimentAsync” method of the “TextAnalyticsAPI” class. I have separated these off into a private async method called “GetAnalysisResultsAsync”. This method will call our Cognitive Services endpoint, analyze the results for information, and pass them back in a format that our view can use.

private async Task<IEnumerable<AnalysisResult>> GetAnalysisResultsAsync(MultiLanguageBatchInput batchInput)
            KeyPhraseBatchResult keyPhraseBatchResult = await _textAnalyticsAPI.KeyPhrasesAsync(batchInput);
            SentimentBatchResult sentimentBatchResult = await _textAnalyticsAPI.SentimentAsync(batchInput);

            foreach(var keyPhrase in keyPhraseBatchResult.Documents)
                string tweet = batchInput.Documents
                    .Where(d => d.Id == keyPhrase.Id)
                    .Select(t => t.Text)

                var sentiment = sentimentBatchResult.Documents
                    .Where(s => s.Id == keyPhrase.Id)

                if (IsSignificant(sentiment.Score) && !keyPhrase.KeyPhrases.Count().Equals(0))
                    AnalysisResult analysisResult = new AnalysisResult
                        KeyPhrases =keyPhrase.KeyPhrases,
                        Attitude = DeriveAttitude(sentiment.Score),
                        Tweet = tweet


            return _analysisResults;

When we invoke our API methods, we will load the results into a “KeyPhraseBatchResult” and “SentimentBatchResult” object. We can then iterate through the results, get the sentiment and the text associated to the key phrases using Linq, and analyze our sentiment score.

two things we want to know about our sentiment are:

  1. The significance of the score – It’s important to note that the machine learning models that drive Microsoft Cognitive Services are still being trained and are imperfect, so to make our output as accurate and effective as possible we are going to parse our output to only contain sentiment scores that the API has found to be blatantly positive or negative. I implement this by only adding a result to my output if this sentiment score is greater than .85 or less than .20, however for your projects, you can use whatever thresholds you find appropriate.

  2. private bool IsSignificant(double? score)
    if(score > .85 || score < .20)
               return true;
               return false;

  3. The attitude the score denotes – We want our user to see if a sentiment is positive or negative. Remember a sentiment score is a value from 0 to 1 with 0 being the most negative and 1 being the most positive. For this, I have added an enum called Attitude with a “Positive” member and a “Negative” member and a method called “DeriveAttitude” inside my “TextAnalysisService” class.

  4. public enum Attitude
    private Attitude DeriveAttitude(double? sentimentScore)
        if (sentimentScore > .85)
            return Attitude.Positive;
            return Attitude.Negative;

Once we have decided that our sentiment is significant and we have derived the attitude that our sentiment score denoted, we will create an “AnalysisResult” object that contains our key phrases, our attitude and our original tweet so we can display these on the output screen. My “AnalysisResult” object looks like this.

public class AnalysisResult
    public IEnumerable<string> KeyPhrases { get; set; }
    public Attitude Attitude { get; set; }
    public string Tweet { get; set; }

We can now pass our list of analysis results back to our controller when we are setting it as the analysis results in the view model for the output page.

For our output page I am using a view model class called “AnalysisViewModel”. For this demo we only care to display the Username that was entered by the user and our list of analysis results.

public class AnalysisViewModel
    public string Username { get; set; }
    public IEnumerable<AnalysisResult> AnalysisResults { get; set; }

The Output

With the help of UI framework Bootstrap, here is a few examples of some of our output.




These are just a few examples of some output from this project, but what we can see here is some simple code and brief setup that yields a payload of high-potential information. While these single outputs may not offer much, we could envision that storing these outputs, aggregating them to a report, and running analytics on those reports can offer a lot of insight for targeted customer engagement. We could see that maybe Razer should do more giveaways, or HP might need to rethink the way they manage their mailing list, or that avocado toast is a good candidate for a marketing campaign for Amazon and Whole Foods.

A brief disclaimer about this output screen. If you decide to setup the application and run it yourself (which I sincerely hope you will), be wary of the fact that we are dealing with live, uncensored data. While the application only returns text, it is possible (and quite likely) that the application will return some profanity.

Future Potential

This demo is just a small sample of all the capabilities that Microsoft Cognitive Services has to offer. To expand on this project, we could take the same functionality and have it monitor our Twitter mentions on a daily or weekly basis and aggregate the results into useful reports. For example, we could implement a front-end library like Chart.js to make a more robust output screen.


All the project code is available on GitHub. We have just seen how easy it is for us to integrate Microsoft Cognitive Services into our applications. There are many other powerful features to explore with Microsoft Cognitive Services. I hope that this blog has inspired and motivated you to dive head first into AI and machine learning integration with Microsoft Cognitive Services.


About The Author

App Dev Consultant

Connor is an Application Development Consultant in Cardinal's Raleigh office. While most of his experience is in the Microsoft stack, he is also familiar with JavaScript frameworks and front-end development. Currently, Connor is exploring VR and AR development for mobile devices with Unity game-engine.