Sentiment Analysis in Python with Microsoft Cognitive Services

Sentiment analysis refers to the use of text analytics, natural language processing among other techniques to automatically identify the writers attitude towards a given product, service or topic. The aim is to classify the sentiment of a text into positive, negative or neutral categories. In this blog post we will use a set of available API’s from Microsoft cognitive services to perform sentiment analysis using python.

After reading this blog post you will be able to:

• Use Microsoft Cognitive Services for Sentiment Analysis

• Perform Sentiment Analysis in Python

Python Sentiment Analysis

Use Cases of Sentiment Analysis

Sentiment analysis has a wide variety of applications in business, politics and healthcare to name a few. For example, social networks provide a wide array of non-structured text data available which is a goldmine for Marketing teams. Tapping into this text information, marketing and customer service teams apply sentiment analysis to identify the attitude of customers towards their products, services and brand.  This information is then utilized to improve the product, services and the messaging of such. It’s valuable information that drives business strategy.

In politics, analytics teams apply sentiment analysis to social networks and other sources of information to gauge the voter’s sentiments towards political candidates, issues or legislation to name a few. This is important information which helps their teams better shape the messaging, legislation and understanding of what is important to the voter base. 

In healthcare, sentiment analysis has a variety of applications such as measuring the perceived quality of care at various levels of the patient interaction with the providers be it a specific doctor, clinic or large practice. This combined with other sources of information such as surveys allows the healthcare network to manage the patient experience and identify areas of improvement and their underlying cause.

Microsoft Cognitive Services

People express their opinions in complex ways, and as such, there is a variety of algorithms available to perform sentiment analysis from Naïve Bayes to Neural Networks or other machine learning models each with its pros and cons. Instead of building our own, there are a variety of API’s out there to help us with such a task. One of them is Microsoft Cognitive Services which provides various API’s for text analytics, one of which is sentiment analysis. 

Exposed through a REST based interface, we can access these API’s using various programming languages. We will be using python to call Microsoft’s Sentiment Analysis API to classify the sentiment of various documents.

Cognitive Services API

To get started, we first need to obtain an access key for Microsoft Cognitive services to use with the Text Analytics API. Don't worry, there is a free tier available.

In the Azure Portal you first need find the Microsoft’s Cognitive Services as shown below.

Microsoft Cognitive Services - Text Analytics

In the next screen, do a search for Text Analytics API.

Microsoft Text Analytics

Now it’s time to create your API. For the pricing tier, I chose the free tier which provides enough capability for testing this API.

Text Analytics API Microsoft Python

That was it. Now go to your newly created services and obtain the API key which we will use in the next python steps.

Cognitive Services Text Analytics Python

The other piece of information you will need is the endpoint. This varies according to the region where your service was created. Click on the overview tab to obtain the URL under endpoint.

Cognitive Services API Text ANalytics Python

Sentiment Analysis With Python

We now have all the necessary piece of information to successfully make calls to the sentiment analysis API. 

The code is implemented in Python 3.6.1.

To do so we will be using the urrlib library to make the rest calls to our API. Because the calls are made through https, we will import the HTTPSConnection from the http.client library. 

import urllib
import json
from http.client import HTTPSConnection

Next, we initialize 2 variables which will be used throughout our code. The uri variable will hold the endpoint we obtained when creating our API service. Keep in mind only the base URL path is used in this variable. The rest will be implemented in our functions. The accessKey variable holds your API Key for the Text Analytics API.

uri = ''
accessKey = '#######################'

We now implement the GetSentiment function which takes in the variable documents and makes an HTTPS Post to the text analytics sentiment analysis api to obtain a response.

def GetSentiment (documents):
    path = '/text/analytics/v2.0/sentiment'
    headers = {'Ocp-Apim-Subscription-Key': accessKey}
    conn = HTTPSConnection (uri)
    body = json.dumps (documents)
    conn.request ("POST", path, body, headers)
    response = conn.getresponse ()
    return ()

We will now use our function to perform sentiment analysis on 2 documents. We first create our documents. Each document contains an ID, Language and the Text. This is the format expected by the sentiment analysis API.

The Sentiment Analysis API returns an ID and Score for each document. The score is between 0 and 1. The most negative is 0 and the most positive will be 1. Neutral will be right in the middle 0.5.

documents = { 'documents': [
    { 'id': '1', 'language': 'en', 'text': 'I had a horrible customer experience at your store yesterday.' },
    { 'id': '2', 'language': 'en', 'text': 'Your restaurant is great! I would love to visit it again in the future' }

result = GetSentiment(documents)
sentiments = json.loads(result)["documents"]

After calling the GetSentiment function, we convert the response into json and extracting the documents key. 

Our sentiments is now a list of sentiments as mentioned previously with an id referring to the document that was passed and a score.

As expected, our first document is a Negative one, returning a score of 0.12. The second document is a Positive score of 0.91.

[{'id': '1', 'score': 0.12316212058067322},
 {'id': '2', 'score': 0.9141618013381958}]



Sentiment analysis is a text analytics technique to categorize the opinions of people as being positive, negative or neutral. This information is extremely valuable to organizations in a wide array of industry as it enables organizations to better understand the views of their customers towards the company’s products and services which helps drive strategy.

We have implemented sentiment analysis in Python using the Sentiment Analysis API provided by the Microsoft Cognitive Services.