Twitter Analysis
Twitter Analysis
The role of social media in public opinion has been profound and evident since it started gaining attention. Just
after the news of Russia-Ukraine war, citizens from across the globe started flooding the twitter platform with
their opinions. Analysis of these opinions can help us to understand the thinking of the public on different events
before and during the war. We will be fetching the data in realtime by using API from twitter.
Sentiment analysis is the process of identifying feelings and emotions expressed in words, through ML or AI.
About Library
Tweepy is important library we will using to fetch data from twitter by api
CONSUMER_KEY = "CRE4O8N0LsWUFLYCFKNRwV7H1"
CONSUMER_SECRET = "ZflVoFPdZY65jfRERJhpV45X9tsRFWrHB5KNuOX8VH6VNIajrY"
ACCESS_TOKEN = "1464479492227969024-GlUuFz960YYOSVIwm9fyZEYW3oASS0"
ACCESS_SECRET = "siI1kvB0d8rnfnWkcMFJxiBixtOOonxHt435sTT089f3X"
# API's setup:
def twitter_setup():
# Authentication and access using keys:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# Return API with authentication:
api = tweepy.API(auth)
return api
Post authentication, the utility function we defined in above cell will have access to the twitter
Assign a literal to store the data
Give the parameters accordingly
Screen_name determines what kind of tweets can be extracted
Count determines number of tweets needs to be fetched
extractor = twitter_setup()
6 recent tweets:
We'll also have as I stated earlier unexploded mines.But as well we'll have unexploded rockets in the region th
is c… https://t.co/teoE7Z33tM
More & More cities continued to be bombed and more and more chemicals are in use and illegal weapons are be
ing used… https://t.co/CE4W20c53J
treaty with Ukraine & Russia where they wouldn't be able to attack Nuclear Power Plants or any powergrid, t
his woul… https://t.co/WyYga82UaJ
The possible solution to this idea is to end the war in Ukraine. This would be all good for all parties. Not on
ly w… https://t.co/JYpCcFaBgx
This would lead to radiation spreading throughout Europe and specifically Ukraine & Russian neighbouring co
untries.… https://t.co/m6MRQAtjzq
Environmental effects of this invasion, with attacks on Chemical Plants and causing dangerous Chemicals to expl
ode.… https://t.co/1qMSyHkMr7
Tweets
#Preview
data
Tweets
All info can be extracted like, id, creation time, location/co-ordinates, source, entities
print(tweets[1].id)
print(tweets[1].created_at)
print(tweets[1].source)
print(tweets[1].favorite_count)
print(tweets[1].retweet_count)
print(tweets[1].geo)
print(tweets[1].coordinates)
print(tweets[1].entities)
1532285486068387845
2022-06-02 08:58:23
Twitter Web App
0
0
None
None
{'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/CE4W20c53J', 'expanded_url'
: 'https://twitter.com/i/web/status/1532285486068387845', 'display_url': 'twitter.com/i/web/status/1…', 'indice
s': [121, 144]}]}
DataFrame would look like this post adding the relevent features
0 We'll also have as I stated earlier unexploded... 140 1532285773667500032 2022-06-02 08:59:31 Twitter Web App 0 0
1 More & More cities continued to be bombed ... 144 1532285486068387845 2022-06-02 08:58:23 Twitter Web App 0 0
2 treaty with Ukraine & Russia where they wo... 144 1532285232489107456 2022-06-02 08:57:22 Twitter Web App 0 0
3 The possible solution to this idea is to end t... 140 1532285036950691840 2022-06-02 08:56:35 Twitter Web App 0 0
4 This would lead to radiation spreading through... 144 1532284801398493184 2022-06-02 08:55:39 Twitter Web App 0 0
5 Environmental effects of this invasion, with a... 140 1532284629020975104 2022-06-02 08:54:58 Twitter Web App 0 0
It would be insightful if we can know the average len by considering all the tweets
mean = np.mean(data['len'])
Textblob
TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language
processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and
more. ##### We are using textblob to process the data and to find polarity of the tweet
Sentiment polarity determines if the text expresses the positive, negative or neutral sentiment of the user about the entity in
consideration.
def clean_tweet(tweet):
'''
Utility function to clean the text in a tweet by removing
links and special characters using regex.
'''
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
def analize_sentiment(tweet):
analysis = TextBlob(clean_tweet(tweet))
if analysis.sentiment.polarity > 0:
return 1
elif analysis.sentiment.polarity == 0:
return 0
else:
return -1
In the above cell we defined a utility function to clean the data by removing links and special characters using regex, and one more utility
function which can analyze the sentiment of the tweet and assign score on same
treaty with Ukraine & Russia where they 2022-06-02 Twitter Web
2 144 1532285232489107456 0 0 1
wo... 08:57:22 App
# We print percentages:
According to our analysis by fetching live data from twitter, we got to know that the sentiment of
people on Russia-Ukraine war is
50% of tweets are positive
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js