You work in an event management company. On Mother's Day, your company has
organized an event where they want to cast positive Mother's Day related tweets
in a presentation. Data engineers have already collected the data related to
Mother's Day that must be categorized into positive, negative, and neutral
tweets.
You are appointed as a Machine Learning Engineer for this project. Your task is
to build a model that helps the company classify these sentiments of the tweets
into positive, negative, and neutral.
Download the data
Show code
import requestszip_file = requests.get('https://he-s3.s3.amazonaws.com/media/hackathon/hackerearth-test-draft-1-102/predicting-tweet-sentiments-231101b4/fa62f5d69a9f11ea.zip?Signature=v92IcNfljnopA9xQoCPCftwg1g0%3D&Expires=1590318817&AWSAccessKeyId=AKIA6I2ISGOYH7WWS3G5')withopen('data.zip', 'wb') as f: f.write(zip_file.content)
I see that most tweets are in English, but it seems that some entries in the data are not actually indicating any language.
Show code
print('Total Other language tweets: ', train_data.shape[0].item()-2994)
Total Other language tweets: 241
Let’s see if they are actually in different languages.
Show code
train_data.loc[train_data['lang'] !='en', :]
original_text
lang
retweet_count
original_author
sentiment_class
id
1.244590e+18
Happy mothersday to all those celebrating toda...
-0.0138325017
en
11
0
1.244823e+18
Exactly what my late mum aka hype mama would d...
-0.9677309496
en
0
0
1.246515e+18
It's the world's most difficult job No sick le...
-0.3876905537
en
1
0
1.244226e+18
Happy Mother’s Day! To all the amazing Mums ou...
0.5309553602
en
0
0
1.244419e+18
Happy Mothers Day , Mummy! Nearly 90 and still...
-0.045423609
en
2
0
...
...
...
...
...
...
1.246356e+18
Happy Mothers Day All My Nigerian Massive Fami...
0.2117897904
en
0
0
1.245821e+18
HAPPY MOTHERS DAY ! HAPPY MOTHERS DAY !!Now th...
-0.8739088126
en
0
0
1.246719e+18
Isan Elba celebrates Happy Mothers’ Day with h...
0.4945825935
en
0
0
1.245565e+18
Still miss my mom she passed 18th of March 201...
0.6927740873
0
0
0
1.245871e+18
I’m so thankful for my 5, healthy happy joy br...
0.2522315249
en
1
0
241 rows × 5 columns
I think all the tweets are in English, you can see that the en value representing English is misplaced in other columns retweet_count and original_author. They are either filled with random float numbers or some other tweet text, maybe the data was not scraped properly?
prHowever from what I know, I think we can ignore the columns other than the original_text column which has the tweet text, which is the most important for analysis for sentiment of text. You can see that almost all of the texts have some link embedded to them, They are not likely to help in getting to know the sentiment of the tweeter.
The pattern here is that most of the links either are images which start with pic.twitter.* or links referring to other sites like http://www.instagram.*, We should be able to identify the pattern with a Regular expression. Let’s try to test the assumption.
Cleaning links
Show code
import resample_tweet ="""Happy Mothers Day Mothers are very valuable to the society because they build families that make up the general population of every nation.They also contribute immensely to nation building and capacity building as caregivers.....https://www. facebook.com/36426361058377 0/posts/1130869587256498/ … #happymothersday2020 pic.twitter.com/ZCZOF1xb6Kwo"""
Happy Mothers Day Mothers are very valuable to the society because they build families that make up the general population of every nation.
They also contribute immensely to nation building and capacity building as caregivers.....
This will remove most of the links, but it will also remove the text between the links like in the case above the #happymothersday2020 hashtag is removed.
# Download an image to use as mask for wordcloudimport numpy as npfrom io import BytesIOfrom PIL import Imagefrom PIL.ImageOps import invertimg_file = requests.get('http://www.agirlandagluegun.com/wp-content/uploads/blogger/-ox_bazyTgmQ/TcNwpMfduLI/AAAAAAAAOX4/hcxXcz0A8-A/s1600/scan0001.jpg')img = BytesIO(img_file.content)img_mask = np.array(Image.open(img))# Check if the image was downloaded properlyimg_mask.shape
(718, 1600, 3)
Show code
# To transform the mask image to display proper output# From https://www.datacamp.com/community/tutorials/wordcloud-pythondef transform_format(val):# Just trying to invert pixelsifany(v ==255for v in val):return0else:return255# Transform the mask into a new one that will work with the function:transformed_mask = np.ndarray((img_mask.shape[0], img_mask.shape[1]), np.int32)for i inrange(len(img_mask)): transformed_mask[i] =list(map(transform_format, img_mask[i]))
Show code
from wordcloud import WordCloud, STOPWORDSimport matplotlib.pyplot as pltwc = WordCloud(background_color="white", max_words=2000, mask=transformed_mask, stopwords=set(STOPWORDS), contour_width=1, contour_color='steelblue')# generate word cloudwc.generate('\n'.join(train_data['original_text'].values.tolist()))