Analysing Tweet Sentiment with Azure (Python)

Azure Twitter Sentiment Analysis

Today I decided to try and analyse the text of Twitter tweets using Microsoft Azure and the cognitive services that it offers, particularly text sentiment. Text sentiment reviews the text and then gives it a score between 0.0 (negative) and 1 (positive), with 0.5 being neutral. This could help if for example you wanted to tackle negative tweets (i.e. you run a company and want to respond to negative tweets) or wanted to auto retweet positives (i.e. you ran a company and want tweets that say nice things about your products auto retweeted).

I’m building on / reusing some of my previous work, so if you want to follow my full thinking you may want to check out: My Twitter Bot and my first use of AzureIf you are skipping that reading then you will need the following to run this project:

  • Twitter – A Twitter account and then also a consumer key, a consumer secret, an access token and an access token secret. I’ve saved all of these into a file called and then importing them into my project. You will need your own from Twitter and their Developer section:
  • Azure – an Azure account. I’m currently using a free trial (free trails are awesome).

Within Azure open “All services“, scroll down to “AI + Machine Learning” and open “Cognitive Services“.

Azure - Cognitive Services
Azure – Cognitive Services

Within Cognitive Services click Create and enter:

  • Name: I went with Text_Analytics
  • Subscription: I’m using the Free Trial, if you have an ongoing Azure subscription then choose wisely.
  • Location: I went with UK South as it is the location nearest to me and UK West was not an option.
  • Pricing tier: I went with F0. Again, this depends on your Azure subscription.
  • Resource group: I created a new one called Text_Analytics.
Azure – Creating Text Analytics

Once the resource is created click “Overview” and it should confirm the location of the resource, the pricing tier and the endpoint that is in use. Also note the “Manage keys” link – clicking this will show two generated keys. The endpoint and a key will be needed for this program.

Cognitive Services – Overview

Also on the Overview screen is the quota information, make sure that this is monitored as you do not want to go over your allowance.

Azure Quota Info
Azure Quota Info

I’ve uploaded my Python program to my GitHub to make it easier for others to review / copy and made sure I’ve commented it. The link is

Azure Twitter Sentiment Analysis
Azure Twitter Sentiment Analysis – uncommented


# geektechstuff
# Twitter Azure Sentiment Application
from twython import Twython
import requests
from pprint import pprint
# Azure API endpoint
# Azure key
headers ={“Ocp-Apim-Subscription-Key”:”ENTER AZURE KEY HERE”}
# imports the Twitter API keys from a .py file called auth
from auth import (
# sets up a variable called Twitter that calls the relevent Twython modules
twitter = Twython(
def get_tweets(search_term):
# this searches Twitter
results = twitter.cursor(, q=search_term)
# this then pulls each individual tweet
for result in results:
# Tweet details that I may be interested in
tweet_id = result[‘id_str’]
tweet_text = result[‘text’]
tweeted_time = result[‘created_at’]
name = result[‘user’]
tweet_screen_name = name[‘screen_name’]
# Preparing the data to give to Azure
documents = {‘documents’ : [
{‘id’: tweet_id, ‘language’: ‘en’, ‘text’: tweet_text},
# Sending the data to Azure
response =, headers=headers, json=documents)
# Getting response back from Azure
languages = response.json()
# Printing the response


Currently my program returns the Tweet ID and the sentiment score. I’ll be looking to output the data in a different way soon, I’m thinking of using my automated Twitter bot to retweet positive tweets about Python.