Coders Packet

Twitter sentiment analysis by streaming live tweets in Python

By Shreya Singh

To perform sentiment Analysis on the extracted tweets and classify them into Positive, Negative, Neutral using Python language.

The goal is to perform sentiment analysis on live tweets in Python language wherein the user will Enter a topic, which will be searched on Twitter, and tweets related to that topic will be extracted. Sentiment Analysis will be performed on the extracted tweets and will be classified into Positive, Negative, Neutral. Further visualizations are provided so the Data can be further analyzed by the user.

The first step is Connecting with Twitter API and extracting the data. For this you will need a Twitter Developers account if you don't have one it is easy to get, you just need to apply for a Twitter Developer Account. Once you get the account Just create a dummy app, and from that app, you will get the necessary Keys & Tokens which we need for the API.
It can be done by following the 2 steps:

-A Twitter app can be created via the Twitter app dashboard page with an approved developer account.
-Generate access tokens on the “Keys and Tokens” tab in an app’s “Details” section within the Twitter app dashboard. Click the “Create” button in the “Access token & access token secret” section.

After the connection is established, we query the Twitter API and ask for the data we want. A DataFrame is then created ready to store the extracted data. Using tweepy library in Python we will fetch the tweets for the topic we selected that is information such as username of the person who tweeted it, Likes for that tweet, Location of the User, and so on. The attributes are stored in the DataFrame so we can further process them. I have used the tweepy.cursor() because we want to extract a larger number of tweets i.e over 100,500 etc

The next step is preprocessing or cleaning the data which was done using the re library in Python which can be used to work with Regular Expressions. It is a sequence of characters that forms a search pattern and can be used to check if a string contains the specified search pattern.

Result
Original tweet:
Good Night to the casts of "#KindredSpirits" team: @amybruni, @AdamJBerry & @chipcoffey... GOOD LUCK ON YOUR NEW… https://t.co/vJWBFwBKQ8

Clean tweet:
good night to the casts of kindredspirits team amp good luck on your new

Sentiment of the tweet:
Positive

Once we get the sentiments, we can analyze the data by making plots. In the project visualization was made using barplot wherein we see that most of the tweets have neutral sentiment followed by positive with least negative.

 

Download project

Reviews Report

Submitted by Shreya Singh (shreyasingh)

Download packets of source code on Coders Packet