Sentiment Analysis on Farmer’s Protest in India.

RAJNEESH PANDEY
3 min readJan 25, 2021

Creating a Sentiment Analysis model on twitters for Farmer Protest in India

Sentiment Analysis, also known as opinion mining, is a powerful tool you can use to build smarter products. It’s a natural language processing algorithm that gives you a general idea about the positive, neutral, and negative sentiment of texts

We can follow these steps to make a sentiment analysis model.

Step 1–2: First we will Tokenize our tweets/create a “Bag of Words ”and then apply cleaning so that we can remove the special characters which are not responsible for our sentiment analysis.

Step 3–4: Now we will remove the stop words which again not going to affect our analysis and then we need to classify our cleared data as positive, negative, and neutral.

Step-5: After that, we have to Apply a Supervised Algorithm for the classification of the bag of the word created after cleaning the data and calculate the accuracy score, and then apply the calculation so that the outcome results in the sentiment of the tweet.

  • Firstly, I created a TwitterClient class. This class contains all the methods to interact with Twitter API and parsing tweets. I use __init__ function to handle the authentication of API clients.
  • In get_tweet_sentiment we use the textblob module.
  • analysis = TextBlob(self.clean_tweet(tweet))
  • TextBlob is actually a high-level library built over top of the NLTK library. First I call the clean_tweet method to remove links, special characters, etc. from the tweet using some simple regex.
    Then, as we pass tweet to create a TextBlob object, the following processing is done over text by textblob library:
  • Pass the tokens to a sentiment classifier which classifies the tweet sentiment as positive, negative, or neutral by assigning it a polarity between -1.0 to 1.0.
  • Here is how a sentiment classifier is created:
  • Training data now consists of labeled positive and negative features. This data is trained on a Naive Bayes Classifier.
  • Then, we use a sentiment.polarity method of TextBlob class to get the polarity of tweets between -1 to 1.
    Then, we classify polarity as:
  • if analysis.sentiment.polarity > 0: return ‘positive’ elif analysis.sentiment.polarity == 0: return ‘neutral’ else: return ‘negative’
  • Finally, parsed tweets are returned.

Here are the WordCloud and Polarity of tweets.

--

--