Analyze Emotion in Your Tweets

Sapana Subedi(She/Her)
3 min readMar 6, 2021

Text analysis is about collecting data, using machine learning to analyze it, and visualize the results to increase comprehension and identify trends.

Text analytics is a part of NLP, the subfield of AI that deals with language. Text analytics first needs data to work with then it needs machine language to imitate human abilities, and finally, need visualization of results to tell a meaningful story.

In this post, I have analyzed my own Twitter data using “Tweepy”, which is an open-source python package that provides a convenient way to access Twitter API. Before you hop into the text analysis, you need to have a Twitter developer account and sample codes to extract tweets from your Twitter account.

Install & Import Libraries

You need to install textblob and tweepy libraries using the pip install command in your terminal.

#Install Librariespip install tweepy
pip install textblob
pip install Pillow
pip install matplotlib
pip install numpy

Import libraries that you will use while analyzing text.

#Import Librariesfrom textblob import TextBlob
import tweepy
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from PIL import Image
import numpy as np

Authentication for Twitter API

#Extracting your tweetsimport tweepy
from textblob import TextBlob
import io
from decouple import config

consumer_key = config('consumer_key')
consumer_secret = config('consumer_secret')
access_token = config('access_token')
access_token_secret = config('access_token_secret')

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

tweets = api.user_timeline(screen_name='', count=)
lis = []
for tweet in tweets:
content = tweet.text
lis.append(content)
listToStr = ' '.join(map(str, lis))
print(listToStr)

fp = io.open("filename.txt", "w", encoding="utf-8")
fp.writelines(listToStr)

This way you can extract your tweets from your timeline and write the extracted tweets in a file.

Cleaning extracted tweets to analyze the emotions

import string
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import numpy as np
from PIL import Image


# reading text file
text = open("filename.txt", encoding="utf-8").read()

# converting to lowercase
lower_case = text.lower()

# Removing punctuations
cleaned_text = lower_case.translate(str.maketrans('', '', string.punctuation))

# splitting text into words
tokenized_words = cleaned_text.split()

stop_words = ["..."]

# Removing stop words from the tokenized words list
final_words = []
for word in tokenized_words:
if word not in stop_words:
final_words.append(word)

emotion_list = []
only_emotion =""
with open('emotion.txt', 'r') as file:
for line in file:
clear_line = line.replace("\n", '').replace(",", '').replace("'", '').strip()
word, emotion = clear_line.split(':')

if word in final_words:
emotion_list.append(emotion)
for i in emotion_list:
only_emotion=only_emotion+""+i

wordcloud = WordCloud(
background_color='white',
contour_width=3,
max_font_size=300,
min_font_size=25 )

wordcloud.generate(only_emotion)

# plot the WordCloud image
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad=0)
plt.margins(x=0, y=0)
plt.savefig('graph.png')
plt.show()

In order to clean the retrieved data, you need to remove the punctuation presented, convert the text in either lower or uppercase followed by removing the stop words, and scarp the emotions out of words used in your tweets. And finally, visualize the pattern using matplotlib.

Word Cloud of the emotions
Masking Word Cloud in a Twitter Image

For masking the word cloud in an image. You can find the code here in my Github Repository. Thank you for reading. Happy Coding!

--

--

Sapana Subedi(She/Her)

Co-founder @Swastha Naari | One step towards Data Science World