Quantrium Guides

Microsoft Azure’s Sentiment Analysis Service

How to use Azure ML’s Sentiment Analysis Service using Python

Kailash S
Quantrium.ai

--

In this blog post, I am going to throw a spotlight on Microsoft Azure’s Sentiment Analysis Service and its pythonic way of implementation.

What is Microsoft Azure?

Microsoft Azure, commonly referred to as Azure (formerly known as Windows Azure) is a cloud computing service created by Microsoft for building, testing, deploying, and managing applications and services through Microsoft-managed data centers. It provides a range of cloud services, including compute, analytics, storage and networking. Users can pick and choose from these services to develop and scale new applications, or run existing applications in the cloud.

Microsoft Azure ML

Azure Machine Learning (termed as Azure ML) is managed machine learning service. From classical ML to Deep Learning, Supervised, and Unsupervised Learning Azure ML offers an integrated development environment called ML Studio. People without a data science background can also build data models through drag-and-drop gestures and simple data flow diagrams.

You can build, train, and track machine learning and deep-learning models in an Azure Machine Learning Workspace. Now let’s move on to Azure ML’s Text Analytics APIs.

Azure’s Text Analytics API

Azure ML’s Text Analytics API enables you to perform Natural Language Processing functionalities over raw text. Some of the valuable services provided by Azure’s Text Analytics API are sentiment analysis, key phrase extraction, named entity recognition, and language detection. In this guide we will setup and use Sentiment Analysis APIs.

Sentiment Analysis using Azure’s Text Analytics API

Sentiment Analysis API evaluates the user input sentences and return sentiment (confidence) scores for each sentence as well as at the document-level along with sentiment labels (such as “negative”, “neutral” and “positive”).

Preparing your Request

To make use of this API all you need to do is send a request (which should be of POST type) and can contain one or more documents. A document can have any number of sentences. Each document should be associated with a unique ID and language used in that particular document. Following is a sample request having 2 documents in it.

{
"documents": [
{
"language": "en",
"id": "1",
"text": "This movie had an awesome storyline. But not a convincing screenplay."
},
{
"language": "en",
"id": "2",
"text": "Excited and sad to hear the end of our college. Life has to move on right."
}]
}

Scoring and Labeling Guidelines

A confidence score for a sentence and the entire document can be maximum of 1.0 and minimum of 0. More the confidence score for a sentiment, more is the API confident about the sentiment returned for the particular sentence. The possible sentiment labels for a sentence/document are positive, negative, and neutral. The sentiment of the document is determined below.

Sentiment Label Guidelines

Opinion Mining Feature of Sentiment Analysis

This feature is commonly called Aspect-based Sentiment Analysis. It gives more information about sentiment of opinions with respect to the aspects in text. To make use of the opinion mining feature, you need to set property opinionMining = True while sending the request.

Opinion mining will consider different aspects located in texts and their corresponding sentiment/opinion. Consider the sentence “This movie had an awesome story line. But poor screenplay”.

Opinion Mining Results

NOTE: This feature is available only starting from version 3.1-preview.1.

Pythonic implementation of Text Analytics Sentiment Analysis API

Now let’s see how you can use Azure ML’s Sentiment Analysis service using Python program. To make requests to Azure ML Text Analytics — Sentiment Analysis API, you need credentials and the endpoint.

  1. Subscription key — which you can find in Keys and Endpoint under Resource Management of a particular resource. There will be 2 keys for a resource and both are usable. If needed, we can regenerate keys to ensure confidentiality.
  2. Endpoint — To access the service, we need an endpoint for that particular service, which is also available where you could find the Subscription key.

After acquiring these credentials, we can set the Headers for the POST request. In the Header part, we need to include the Subscription Key.

While sending the POST request, along with the Headers, append the Endpoint and the request (set of documents) on which Sentiment analysis is to be performed as the parameters of this POST request. The requested documents should be in python dictionary format. Refer to the dictionary structure in the example mentioned earlier.

Following is the python snippet for the above mentioned steps

Code snippet for setting up request to Sentiment Analysis API

If the POST request to the sentiment analysis is successful, we’ll get the response back from the API with status 200 and the Sentiment Analysis results(along with opinion mining results if set to TRUE while calling) of the requested documents.

Code snippet to get response from Sentiment Analysis API

Use json() method over the returned response to treat it as a JSON structure and use pretty print to make the response more readable.

Along with sentiment labels and confidence scores, API also returns the offset (starting index) and length of each sentence within a document.

Response generated by the Sentiment Analysis API for the example we have considered earlier is:

Sample response from the Sentiment Analysis API

I hope that this blog post would have given a clear idea on how to use Microsoft Azure’s Sentiment Analysis API’s and its Pythonic way of implementation.

--

--