Twitter Sentiment Analysis using Python

Twitter is one of those social media platforms where people are free to share their opinions on any topic. Sometimes we see a strong discussion on Twitter about someone’s opinion that sometimes results in a collection of negative tweets. With that in mind, if you want to learn how to do sentiment analysis of Twitter, this article is for you. In this article, I will walk you through the task of Twitter sentiment analysis using Python.

Twitter Sentiment Analysis

Sentiment analysis is a task of natural language processing. All social media platforms should monitor the sentiments of those engaged in a discussion. We mostly see negative opinions on Twitter when the discussion is political. So, each platform should continue to analyze the sentiments to find the type of people who are spreading hate and negativity on their platform.

For the task of Twitter sentiment analysis, I have collected a dataset from Kaggle that contains tweets about a long discussion within a group of users. Here our task is to identify how many tweets are negative and positive so that we can give a conclusion. So, in the section below, I’m going to introduce you to a task of Twitter sentiment analysis using Python.

Twitter Sentiment Analysis using Python

Let’s start the task of Twitter sentiment analysis by importing the necessary Python libraries and the dataset:

   Unnamed: 0  count  hate_speech  offensive_language  neither  class  \
0           0      3            0                   0        3      2   
1           1      3            0                   3        0      1   
2           2      3            0                   3        0      1   
3           3      3            0                   2        1      1   
4           4      6            0                   6        0      1   

                                               tweet  
0  !!! RT @mayasolovely: As a woman you shouldn't...  
1  !!!!! RT @mleew17: boy dats cold...tyga dwn ba...  
2  !!!!!!! RT @UrKindOfBrand Dawg!!!! RT @80sbaby...  
3  !!!!!!!!! RT @C_G_Anderson: @viva_based she lo...  
4  !!!!!!!!!!!!! RT @ShenikaRoberts: The shit you...  

The tweet column in the above dataset contains the tweets that we need to use to analyze the feelings of those engaged in the discussion. But to go further, we have to clean up a lot of errors and other special symbols because these tweets contain a lot of language errors. So here is how we can clean up the tweet column:

Now, the next step is to calculate the sentiment scores of these tweets and assign a label to the tweets as positive, negative, or neutral. Here is how you can calculate the sentiment scores of the tweets:

Now I will only select the columns from this data that we need for the rest of the task of Twitter sentiment analysis:

                                               tweet  Positive  Negative  \
0   rt mayasolov woman shouldnt complain clean ho...     0.147     0.157   
1   rt  boy dat coldtyga dwn bad cuffin dat hoe  ...     0.000     0.280   
2   rt urkindofbrand dawg rt  ever fuck bitch sta...     0.000     0.577   
3             rt cganderson vivabas look like tranni     0.333     0.000   
4   rt shenikarobert shit hear might true might f...     0.154     0.407   

   Neutral  
0    0.696  
1    0.720  
2    0.423  
3    0.667  
4    0.440  

Now let’s have a look at the most frequent label assigned to the tweets according to the sentiment scores:

Neutral 🙂 

So the most of the tweets are neutral, which means they are neither positive nor negative. Now let’s have a look at the total of the sentiment scores:

Positive:  2880.086000000009
Negative:  7201.020999999922
Neutral:  14696.887999999733

The total of neutral is way higher than negative and positive, but out of all the tweets, the negative tweets are more than the positive tweets, so we can say that most of the opinions are negative.

Summary

So this is how you can perform the task of Twitter sentiment analysis by using the Python programming language. Analyzing sentiments is a task of natural language processing. All the social media platforms need to keep a check on the sentiments of people engaged in a discussion. I hope you liked this article on Twitter sentiment analysis using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1170

Leave a Reply