Analysing Tweet Sentiment with Azure -Part 2 (Python)

Building on the work I did yesterday using Azure to analyse Twitter tweet sentiment I have amended the function of the code so that it now marks the tweet as positive, neutral or negative. This is building up so that I can (hopefully) get my Twitter bot to retweet positive tweets about a subject of my choosing.

Full code available at: https://github.com/geektechdude/Tweet_Text_Analysis

————
def get_tweets(search_term):
# this searches Twitter
results = twitter.cursor(twitter.search, 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 = requests.post(sentiment_uri, headers=headers, json=documents)
# Getting response back from Azure
azure_response = response.json()
# Stripping the score out
azure_documents = azure_response[‘documents’]
azure_score = azure_documents[0][‘score’]
if azure_score >=0.6:
print(tweet_id,”\n”, tweet_text, “from \n”, tweet_screen_name)
print(azure_documents,”\n is postive”)
elif azure_score ==0.5:
print(tweet_id,”\n”, tweet_text, “from \n”, tweet_screen_name)
print(azure_documents,”\n is neutral”)
else:
print(tweet_id,”\n”, tweet_text, “from \n”, tweet_screen_name)
print(azure_documents,”\n is negative”)
——–
get_tweets() modified with if, elif and else
get_tweets() modified with if, elif and else

One response to “Analysing Tweet Sentiment with Azure -Part 2 (Python)”

  1. Retweeting Positive Tweets, Or Part 3 Of Analysing Tweet Sentiment with Azure (Python / Raspberry Pi) – Geek Tech Stuff Avatar

    […] the Python program (see part 1 and part 2) collecting Tweets from Twitter and then analysing the data via Azure to see if the sentiment is […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect