Social Media Sentiment Analysis
What is Sentiment?
~ image source https://lct-master.org
Collecting Data
In this first part, we’ll see different options to collect data from Twitter. Once we have built a data set, in the next episodes we’ll discuss some interesting data applications.
Step 1.- Register Your App
In order to have access to Twitter data programmatically, we need to create an app that interacts with the Twitter API.
The first step is the registration of your app. In particular, you need to point your browser to http://apps.twitter.com, log-in to Twitter (if you’re not already logged in) and register a new application . You can now choose a name and a description for your app (for example “Social Media Data analysis ” or similar). You will receive a consumer key and a consumer secret: these are application settings that should always be kept private.
Accessing the Data
Twitter provides REST APIs you can use to interact with their service. In particular, Tweepy in one of the most interesting and straightforward to use, so let’s install it:
pip install tweepy==3.3.0
In order to authorise our app to access Twitter on our behalf, we need to use the OAuth interface:
import tweepy
from tweepy import OAuthHandler
consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
Whats Polarity & Subjectivity?
Polarity: Polarity score is a float within the range [-1.0, 1.0], also known as orientation polarity is the emotion expressed in the sentence. It can be positive, negative or neutral.
Subjectivity: It measures subjectivity of sentence or to what extent it represents someones personal feelings, views, or beliefs compared to objective truth or facts. subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.
import tweepy
from textblob import TextBlob
import os
import csv
# Step 1 - Authenticate
consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Step 3 - Retrieve Tweets
public_tweets = api.search('Soulworker ')
# CHALLENGE - Instead of printing out each tweet, save each Tweet to a CSV file
# and label each one as either 'positive' or 'negative', depending on the sentiment
# You can decide the sentiment polarity threshold yourself
print("Sentiment for Individual Tweet")
for tweet in public_tweets:
print(tweet.text)
# Step 4 Perform Sentiment Analysis on Tweets
analysis = TextBlob(tweet.text)
print(analysis.sentiment)
print("")
print('polarity: {}'.format(analysis.sentiment.polarity))
print("")
print('subjectivity: {}'.format(analysis.sentiment.subjectivity))
print("")
Output Response
Sentiment for Individual Tweet
More soulworker grind! @SoulWorkerGF #gameforge #NA
[https://t.co/7RdUaZy3B0](https://t.co/7RdUaZy3B0)
Sentiment(polarity=0.625, subjectivity=0.5)
polarity: 0.625
subjectivity: 0.5
@SoulWorkerGF [https://t.co/zVOnpP5yPH](https://t.co/zVOnpP5yPH)
Sentiment(polarity=0.0, subjectivity=0.0)
polarity: 0.0
subjectivity: 0.0
@SoulWorkerGF You should be doing this on every weekends to increase player base. Considering its not that easy to… [https://t.co/MHp6rwGUBt](https://t.co/MHp6rwGUBt)
Sentiment(polarity=-0.18333333333333335, subjectivity=0.9166666666666667)
polarity: -0.18333333333333335
subjectivity: 0.9166666666666667
Back on that soulworker grind! @SoulWorkerGF #soulworker #gameforge #NA
[https://t.co/Hojy5ldeyR](https://t.co/Hojy5ldeyR)
Sentiment(polarity=0.0, subjectivity=0.0)
polarity: 0.0
subjectivity: 0.0
RT @SoulWorkerGF: [19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% more ex…
Sentiment(polarity=0.1875, subjectivity=0.725)
polarity: 0.1875
subjectivity: 0.725
RT @SoulWorkerGF: [19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% more ex…
Sentiment(polarity=0.1875, subjectivity=0.725)
polarity: 0.1875
subjectivity: 0.725
@SoulWorkerGF Merci (>o<)! , je viens de modifier pas mal de chose ce matin mais je sais pas si je refait des scree… [https://t.co/ka14DPShIu](https://t.co/ka14DPShIu)
Sentiment(polarity=0.3125, subjectivity=1.0)
polarity: 0.3125
subjectivity: 1.0
@SoulWorkerGF does the event work on raids too?
Sentiment(polarity=0.0, subjectivity=0.0)
polarity: 0.0
subjectivity: 0.0
RT @SoulWorkerGF: [19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% more ex…
Sentiment(polarity=0.1875, subjectivity=0.725)
polarity: 0.1875
subjectivity: 0.725
RT @SoulWorkerGF: [19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% more ex…
Sentiment(polarity=0.1875, subjectivity=0.725)
polarity: 0.1875
subjectivity: 0.725
@SoulWorkerGF Remove IP block plz
Sentiment(polarity=0.0, subjectivity=0.0)
polarity: 0.0
subjectivity: 0.0
[19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% m… [https://t.co/Zgwgk8wOD8](https://t.co/Zgwgk8wOD8)
Sentiment(polarity=0.5, subjectivity=0.7)
polarity: 0.5
subjectivity: 0.7
[19/10 - 22/10/2018] 50% More Experience Points and Item Drops
Battling pays in the latest event: you’ll get 50% m… [https://t.co/BxCXhMpIf9](https://t.co/BxCXhMpIf9)
Sentiment(polarity=0.5, subjectivity=0.7)
polarity: 0.5
subjectivity: 0.7
@SamaYuusha Yuusha, your house looks great! You made a very nice place for your SoulWorkers! :)
Sentiment(polarity=0.8250000000000001, subjectivity=0.9166666666666666)
polarity: 0.8250000000000001
subjectivity: 0.9166666666666666
@SoulWorkerGF lily adv when🙄
Sentiment(polarity=0.0, subjectivity=0.0)
polarity: 0.0
subjectivity: 0.0
Leave a Comment