0% found this document useful (0 votes)
16 views5 pages

Twitter Analysis

Uploaded by

killersuapri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Twitter Analysis

Uploaded by

killersuapri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sentimental Analysis of realtime tweets on Russia-Ukrain War

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.

Objective: Performing realtime sentimental analysis on realtime data from twitter

#Importing neccessary libraries


import tweepy # To fetch data from twitter using api
import pandas as pd
import numpy as np
from IPython.display import display
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

About Library

Tweepy is important library we will using to fetch data from twitter by api

For more on tweepy documentation please click here Click here

Enter your credentials below

CONSUMER_KEY = "CRE4O8N0LsWUFLYCFKNRwV7H1"
CONSUMER_SECRET = "ZflVoFPdZY65jfRERJhpV45X9tsRFWrHB5KNuOX8VH6VNIajrY"
ACCESS_TOKEN = "1464479492227969024-GlUuFz960YYOSVIwm9fyZEYW3oASS0"
ACCESS_SECRET = "siI1kvB0d8rnfnWkcMFJxiBixtOOonxHt435sTT089f3X"

Defining utility function for authentication

# We imported our access keys in above cells:


from credentials import * # This will allow us to use the keys as variables

# 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

Creating Extractor to extract dat from twitter

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()

# We create a tweet list as follows:


tweets = extractor.user_timeline(screen_name="Russiaukrainwar", count=200)
print("Number of tweets extracted: {}.\n".format(len(tweets)))

# We print the most recent 5 tweets:


print("6 recent tweets:\n")
for tweet in tweets[:6]:
print(tweet.text)
print()
Number of tweets extracted: 6.

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

Creating a dataframe to store the data fetched

We can have a preview how our tweets would look in a dataframe

# We create a pandas dataframe as follows:


data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])

# We display the first 10 elements of the dataframe:


display(data.head(10))

Tweets

0 We'll also have as I stated earlier unexploded...

1 More & More cities continued to be bombed ...

2 treaty with Ukraine & Russia where they wo...

3 The possible solution to this idea is to end t...

4 This would lead to radiation spreading through...

5 Environmental effects of this invasion, with a...

#Preview
data

Tweets

0 We'll also have as I stated earlier unexploded...

1 More & More cities continued to be bombed ...

2 treaty with Ukraine & Russia where they wo...

3 The possible solution to this idea is to end t...

4 This would lead to radiation spreading through...

5 Environmental effects of this invasion, with a...

# Internal methods of a single tweet object:


print(dir(tweets[0]))

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribut


e__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subc
lasshook__', '__weakref__', '_api', '_json', 'author', 'contributors', 'coordinates', 'created_at', 'destroy',
'entities', 'favorite', 'favorite_count', 'favorited', 'geo', 'id', 'id_str', 'in_reply_to_screen_name', 'in_re
ply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'is_quote_sta
tus', 'lang', 'parse', 'parse_list', 'place', 'retweet', 'retweet_count', 'retweeted', 'retweets', 'source', 's
ource_url', 'text', 'truncated', 'user']

Printing Information about the 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]}]}

Adding relevent data in the dataframe which can help us in analysis

ID - Unique id for unique tweet


Likes - Number of people who have actually like the particular tweet
RTs - This feature gives us the no. of retweets
Date - Date and time tweet created
len - Length of the tweet

data['len'] = np.array([len(tweet.text) for tweet in tweets])


data['ID'] = np.array([tweet.id for tweet in tweets])
data['Date'] = np.array([tweet.created_at for tweet in tweets])
data['Source'] = np.array([tweet.source for tweet in tweets])
data['Likes'] = np.array([tweet.favorite_count for tweet in tweets])
data['RTs'] = np.array([tweet.retweet_count for tweet in tweets])

DataFrame would look like this post adding the relevent features

# Display of first 10 elements from dataframe:


display(data.head(10))

Tweets len ID Date Source Likes RTs

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'])

print("The lenght's average in tweets: {}".format(mean))

The lenght's average in tweets: 142.0

Tweet with most likes and retweets

# We extract the tweet with more FAVs and more RTs:


fav_max = np.max(data['Likes'])
rt_max = np.max(data['RTs'])
fav = data[data.Likes == fav_max].index[0]
rt = data[data.RTs == rt_max].index[0]
# Max FAVs:
print("The tweet with more likes is: \n{}".format(data['Tweets'][fav]))
print("{} characters.\n".format(data['len'][fav]))
# Max RTs:
print("The tweet with more retweets is: \n{}".format(data['Tweets'][rt]))
print("{} characters.\n".format(data['len'][rt]))

The tweet with more likes is:


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
140 characters.

The tweet with more retweets is:


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
140 characters.

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.

from textblob import TextBlob


import re

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

# We create a column with the result of the analysis:


data['Sentiment_analysis'] = np.array([ analize_sentiment(tweet) for tweet in data['Tweets'] ])

# We display the updated dataframe with the new column:


display(data.head(10))

Tweets len ID Date Source Likes RTs Sentiment_analysis

We'll also have as I stated earlier 2022-06-02 Twitter Web


0 140 1532285773667500032 0 0 0
unexploded... 08:59:31 App

More & More cities continued to be 2022-06-02 Twitter Web


1 144 1532285486068387845 0 0 1
bombed ... 08:58:23 App

treaty with Ukraine & Russia where they 2022-06-02 Twitter Web
2 144 1532285232489107456 0 0 1
wo... 08:57:22 App

2022-06-02 Twitter Web


3 The possible solution to this idea is to end t... 140 1532285036950691840 0 0 1
08:56:35 App

This would lead to radiation spreading 2022-06-02 Twitter Web


4 144 1532284801398493184 0 0 0
through... 08:55:39 App

2022-06-02 Twitter Web


5 Environmental effects of this invasion, with a... 140 1532284629020975104 0 0 -1
08:54:58 App

In the above execution we have exclusive feature named sentiment_analysis

positive_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['SA'][index] > 0]


neutral_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['SA'][index] == 0]
negitive_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['SA'][index] < 0]

if the score is close to -1 then it is under negitive sentiment


if the score is close to 0 then it is neutral sentiment
if the score is close to +1 then it is under Positive sentiment

# We print percentages:

print("Percentage of positive tweets: {}%".format(len(positive_tweets)*100/len(data['Tweets'])))


print("Percentage of neutral tweets: {}%".format(len(neutral_tweets)*100/len(data['Tweets'])))
print("Percentage de negative tweets: {}%".format(len(negitive_tweets)*100/len(data['Tweets'])))

Percentage of positive tweets: 50.0%


Percentage of neutral tweets: 33.333333333333336%
Percentage de negative tweets: 16.666666666666668%

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

33% of tweets are neutral

16% of tweets are negitive

Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like