Creating Machine Learning Applications using Azure Cognitive Services

Azure contains a vast array of services that can be used for machine learning, text analysis, and more. In this article, Supriya Pande provides a brief explanation of machine learning and then walks you through creating a sentiment analysis application.

You may have come across some of the popular buzzwords going around in the field of computer science such as machine learning and artificial intelligence (AI). As the world of computing is progressing, humans are now prone to delegate many of their tasks to machines. Machine learning and AI are making computers perform tasks that human intelligence can do. In this article, I am going to give you a brief introduction to machine learning and then we will take a deep dive into Azure Cognitive Services. I will also introduce you to the list of Cognitive Services APIs that can be used to solve business problems. To understand how these APIs can be used to build client .NET applications, we will create an intelligent application that will analyze the user text input and provide the Sentiment Analysis using Text Analytics API. Without further delay let’s move on and start our journey into the world of Cognitive Services.

What is Machine Learning (ML)?

As per Arthur Samuel, one of the pioneers of ML “Machine Learning is the field of study that gives computers the ability to learn without being explicitly programmed.” You want computers and applications to learn this without explicitly programming all the possibilities. Machine learning is used in many applications these days, in fact, you are using Machine learning on a daily basis through a smartwatch or smart devices. Let me introduce you to the main components of machine learning.

  • Training Data: Machine learning is used to analyze the data. This data contains the patterns which determine the goodness and badness of an entity. The training data must always contain the correct answer that you want to predict. For instance, if you think from a text analytics perspective, you will have to know which word pattern falls in a positive connotation and which ones fall under negative pattern. The machine learning algorithm finds the patterns in training data, further analyzes it, and maps these attributes to the correct answers or output.
  • Learning Algorithm: Learning algorithms find the patterns in the training data that maps the user inputs to the correct answers. This process is called ‘training.’ After it has found the models, it can then create a training model.
  • Training Model: The model can be considered as an output of the training process. During the training process, the algorithm analyzes the inputs and training data. Further, it generates a function that maps input to desired outputs. These will be fed to the training model which can recognize the correct patterns. After the model is trained, it is further exposed to the client applications which can send the data to the model and then get the desired response /recognized pattern as the output. The entire training process remains as a black box to the user, and just the trained model is exposed to the outside applications.

The diagram below shows the components and process involved in machine learning

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\AML44.png

Figure: Machine learning Overview Diagram

What are Azure Cognitive Services?

You probably realize that machine learning is very useful. It can be used it to make applications more intelligent because it helps us to detect patterns in the input provided. You will have to create the patterns and create an algorithm to detect these patterns to end up with the machine learning model. Developing the model through the training process is a very tedious and tough job. Azure Cognitive Services can provide big-time savings by determining the patterns and tedious algorithm and training the data. Azure Cognitive Services are REST APIs that expose a machine learning model to the outside world. Your application can just consume the Cognitive Service API methods to get the desired output.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\AMI1.jpg

Figure: Azure Cognitive Services

The Benefits of Azure Cognitive Services

  • Ready-made machine learning models that your applications can consume using REST APIs
  • No need to gather the training data, create a learning algorithm, or train the model. Everything is taken care of by Microsoft.
  • These APIs are hosted in the Azure Cloud which makes them easily accessible and scalable. Because Cognitive Services come under your Azure subscription, they can easily be consumed on pay per use basis.
  • Many available APIs to solve the business problems.

Now let’s move our focus to the various APIs provided by Azure Cognitive Services. You can imagine the Cognitive Services as multiple APIs that are programmed to do specific tasks. Cognitive Services are mainly divided into 5 different categories, and they are further divided into function-specific regular APIs. In addition to regular the APIs, they also have Custom Services. Custom services use your data instead of Microsoft data patterns. So, you can design your own model based on your based on what specifications you need.

List of Cognitive Services

  • Vision (Service for Analyzing Videos & Images)
    • Computer Vision- Mainly Analyzes image and recognizes the scene and activity in an image.
    • Face – Detect faces and emotion in an image.
    • Content Moderator – To check for offensive content in an image or video. Used to allow or block some specific content in an image or video.
    • Video Indexer – Analyze video content and extract the text spoken in video or detect other objects in a video.
    • Custom Vision – Used for customized image recognition where a programmer can tune the data as needed.
  • Speech (Services for Speech Synthesis & Voice Recognition)
    • Speech to Text – Recognizes speech and converts it into text.
    • Text to Speech – Converts text into speech
    • Speaker Recognition API – It identifies speaker based on their speech.
    • Speech Translation – It recognizes the speech and translates into a different language.
    • Custom Speech – Used for building customized speech recognition model.
  • Language (Services to Process Natural Language)
    • Bing Spell Check – It can detect and correct spelling mistakes
    • Content Moderator – To check for offensive content in the text. This is one of the features from Custom Moderator specified in Vision services.
    • Translator Text – Automatically detects the language and translates it into the customizable language.
    • Text Analytics – Extracts and recognizes sentiments from User Input.
    • Language Understanding Service – You can this of this as a Custom API for Language Category. This consists of your data, and you train the model by feeding it the meaning of the data. This will help our application to understand the meaning of what a speaker is saying.
  • Knowledge (Services for making Intelligent Recommendations)
    • QnA Maker – Process text into question and answers. It can be used by bots to respond back with knowledgeable answers.
    • Custom Decision – Uses your data and learns the way of making the correct decision with every usage.
  • Search (Services helping to search the stuff)
    • Bing Autosuggest – Suggest search terms and help autocomplete the search query.
    • Bing News Search – Search specifically in the news section.
    • Bing Web Search – Search in the web section for the provided input values.
    • Bing Entity Search – Identify the entity types such as people or places mentioned in the input and return the results.
    • Bing Image Search – Searches for images that match the phrase in the input.
    • Bing Video Search – Searches for videos that match the phrase in the input.
    • Bing Custom Search API – You can customize the search area where you want to search and get the results for your input.

Besides all these services, there are many more services that will be added in the future. They are present in Cognitive Services Labs which are available here. These are not included currently in the production services. These services might still be in construction and may be added to the regular services in the near future,

By now, I assume that you understand the basics of Cognitive Services. The next thing that I want to focus your attention on is how to leverage these Cognitive Services APIs in client applications. For this, I will show you how to a C# application and use the Text Analytics API to analyze the sentiments of the input text. Before building this client application, here is an overview of the Text Analytics API.

Text Analytics API

The Text Analytics API is one of the services present in the Language Category of Cognitive Services. It provides the advanced natural language processing for the text input. The main functions included in Text Analytics APIs are sentiment analysis, key phrase extraction, language detection, and entity linking. Let’s just briefly go over these functions:

  • Sentiment Analysis: This feature is mainly used to analyze the positive or negative information from the text. Once the user sends the input text, the sentiment analysis returns a numeric value between 0 and 1 depending on the information/text provided. For instance, if the input is “I am very happy” the sentiment Analysis output will be very close to 1. However, if the input is “I don’t like going to the circus,” will return value close to 0 which indicates the negative sentiments.
  • Key-Phrase Extraction: This feature extracts the keywords form the input which in turn may help in finding results for sentiment analysis.
  • Language Detection: This detects the language of the input text. There are 120 languages supported.
  • Entity Linking (Preview): This feature is available currently as a preview version which will soon be released as a regular feature. Entity linking maps the words from your text to additional information on the web. For example, in an input “We had an awesome view of Golden Gate when we visited San Francisco” San Francisco is an entity which is linked to the Golden Gate entity. This improves the overall user experience and correctness of the results. You will understand it better when we go through an example.

To see this in action, run a demo of Text Analytics APIs through the Microsoft Azure Portal. When you are on the webpage, you will notice that there are few samples available for the demo purposes with the English or Spanish Language. For demo purposes, enter this text “We had an awesome view of Golden Gate when we visited San Francisco.”

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\AML21.png

Once you click Analyze, the analysis results will be displayed on the right-side section of the webpage. As you can see, that for this input text, the APIs detected that it’s the English Language, Through the key phrases, it has come to the conclusion that the input has a positive sentiment index of 0.95. Another thing to notice is that entities such as Golden Gate and San Francisco are linked appropriately through the Linked Entities feature.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\AML22.png

By now, you probably see how efficient Cognitive Services are. Now it’s time to try running Text Analytics APIs through a .NET client application.

Building the C# Client Application Using Text Analytics API (Sentiment Analysis)

The first step to begin working on Text Analytics API is setting up the Cognitive Services in Azure, and for this, you would need a subscription to the Azure Portal.

After you have logged in, locate Cognitive Services by going to the All Services tab shown in the screenshot below. After you are on the All services page, scroll down to the AI + Machine Learning Section and then select Cognitive Services.

Once you have selected Cognitive services, a page will open where you can add a new Text Analytics Service. Click on the Add button and then, to be sure that you are subscribing to the correct service, search for the keywords Text Analytics.

Now select the Text Analytics service that is shown in the previous screenshot. After selection, you will see all the details about the service. It shows that this service supports Sentiment analysis, Key phrase extraction, and Language detection which confirms that this is the exact service that we are looking for. Just click Create to get the ball rolling.

To create the service, provide the necessary details in the form. For the Pricing tier, you can click on View full pricing details, and you will be provided with all the relevant options you can make according to the requirements and usage of your project. I have selected the free F0 tier that serves my purpose for a demo application. I selected West US for the Region.

Note: There seems to be an issue happening on Text Analytics services on South Central and East US. If the Text Analytics service in your region does not work, try recreating the service in the West US region.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\A5TextAnalyticsCreateNew.jpg

Once you click Create, Azure will do all the necessary deployments and set up a service for you. Go back to the Cognitive services page where you can search for your cognitive service that was just created.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\A5TextAnalyticsCreated.jpg

Creating the Project

To demonstrate sentiment analysis from the Text Analytics API, create a simple console application. This application will feed in the text and send it to the service to retrieve the sentiment index.

Create a Console App (.NET Framework) in C# and name it DemoCognitiveTextApp.

Adding NuGet Packages for Text Analytics API

After you have successfully created the project, go to the Solution Explorer tab and right click on References/ Select Manage NuGet Packages. Add the Text API .NET SDK packages which will install all the necessary DLLs to talk to Azure Cognitive Services.

You will need to Browse for the correct package and look for the keyword Microsoft.Projectoxford.Text. Then just install the correct version of the package suitable for your .NET version.

After the installation, you will notice that a reference is added to your project.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\A5PackageInstalled.jpg

Now that the Text API package reference is added to the project, you will need to get the API keys that are needed in the project. Go to Solution Explorer and open the app.config file to create a new key. You will need to grab the keys for your Text Analytics API from Azure portal.

C:\Users\spande\AppData\Local\Microsoft\Windows\INetCache\Content.Word\A5TexKeys.jpg

The app.config should look like this

Also, to read these keys, you will have to add System.Configuration as a reference to your project. Go to Solution Explorer, right-click References => Add New Reference. Click on Frameworks and select System.Configuration.

Create a Sentiment Document

To demonstrate Sentiment Analysis using Text API, add a new class file TestAnalysis.cs to the project. This class has the RunSentimentAnalysis( string language, string id, string input) method which takes three parameters. You might be wondering what the three parameters are used for. The answer to this is for creating the requested document. The important thing that you need to keep in mind here is that all the Text API requests take some form of document Array. In the case of Sentiment Analysis, the document is made up of three properties: Language, Text, and ID.

  • Language: This tells what the language of the text is. This is default set to “en” English.
  • Text: The actual text that needs to be analyzed, in our example use provides it through the Console Window.
  • ID: This is a unique random value. The application will use a GUID for this.

The document object is in JSON format with these three properties.

Creating a SentimentDocument object will use this code:

This document will need to be added to the sentiment request that will be passed on to the Text Analytics API. For adding the document, you will use the code below:

Create the Sentiment Client:

For talking successfully with text APIs, you need to create a client using the API keys that you have placed in the app.config file. The below code below will create a sentiment client which will be used to make all relevant API calls.

Sending Over the Sentiment Request and Getting the Response Back:

The code uses a SentimentClient object to make a call to the Text API, uses the GetSentimentAsync(sentimentRequest) method to make an API call. after the text is processed, the response will be returned in the response document. This document will have the Sentiment Analysis Score. According to Microsoft documentation, “Text Analytics API returns a numeric score between 0 and 1 along with the sentiment string from the text. Scores close to 1 indicate positive sentiment and scores close to 0 indicate negative sentiment. Sentiment score is generated using classification techniques. The input features of the classifier include n-grams, features generated from part-of-speech tags, and word embeddings.” Cognitive services are under constant improvements. You might see more new features being added to these services regularly.

Here is the complete code for the TextAnalysis.cs file

Here is Program.cs file

The Program.cs file asks for the user’s input, and it has a call to a method in the TextAnalysis.cs class. Here is the code for Program.cs file:

Results After Running the Application

Positive Text: As you can see the text with positive meaning has a value close to 1 or 100%.

Negative Text: For the negative text the value is farther lesser than 1.

Similar to Sentiment Analysis, you can write code for Key Phrase Analysis and Language Detection. By now you probably understand that you can easily set up your environment for the Cognitive Services APIs and use them effectively in your client applications.

Usage in the Real World

Now that you have a feel for Text Analytics in action, there are many real-world scenarios where Text Analytics are very effective, and it really helps to solve real-world problems. It can reduce manual work to a large extent and can do wonders in analyzing the data quickly. Because of the heavy adoption of social media, it is easy to get to know directly from the customers what they feel about your product. Text Analytics would be very helpful in this scenario. Text Analytics can be run on the product reviews from any social media sites such as Twitter, Facebook or E-Commerce sites. This will help you to understand people’s opinions about the product. You can write an application that can scan through social media feeds, look for keywords, detect the language, and perform sentiment analysis. Text analytics can also help analyze surveys to gain feedback about your product.

Conclusion

After reading this article, you should understand some of the core concepts involved in machine learning and how you can use Azure Cognitive Services to leverage its prebuilt services and customizable features to build intelligent and analytical client applications.

References

https://azure.microsoft.com/en-us/services/cognitive-services/

https://labs.cognitive.microsoft.com/

https://azure.microsoft.com/en-us/services/cognitive-services/text-analytics/