Analysing Tweet Sentiment with Azure -Part 2 (Python)

get_tweets() modified with if, elif and else

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 thought on “Analysing Tweet Sentiment with Azure -Part 2 (Python)

Comments are closed.