ONLINE SUMMER TRAINING Online Courses Free Tutorials 
 Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
0 like 0 dislike
in Python Programming by Goeduhub's Expert (3.1k points)
How do you do sentiment analysis on Twitter data using Natural Language Processing

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-


Course Name

Apply Coupon


Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon


Complete Machine Learning & Data Science with Python Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon


Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
Best answer

what is sentiment analysis ?

Sentiment analysis (opinion mining), it is the process of determining the emotional tone behind a series of words, used to gain an understanding of the the attitudes, opinions and emotions expressed within an online mention.

Why do we need sentiment analysis ?

Let's take an example of mobile phones

Suppose you have a plan to buy a phone but before buying a mobile, we read the reviews  and see the rating, which shows us how successful the product is. 

This what we are talking about is sentiment analysis.

Sentiment analysis is important from the customer's point of view because they can gain information about the product they are thinking of buying.

It is also useful for companies for making product better on customer demand by identifying and categorizing opinion expressed in piece of text, rating etc...

How sentiment analysis works ?

Sentiment Analysis is a procedure used to determine if a chunk (group) of text is positive, negative or neutral and emotions behind the text.


Polarity is a float value within the range [-1.0 to 1.0] where 0 indicates neutral+1 indicates a very positive sentiment and -1 represents a very negative sentiment.


subjectivity is a float value within the range [0.0 to 1.0] where 0.0 is very objective and 1.0 is very subjective. Subjective sentence expresses some personal feelings, views, beliefs, opinions, allegations, desires, beliefs, suspicions, and speculations where as Objective sentences are factual.


TextBlob is a Python 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.

For conceptual clarity of NLP (Natural Language Processing) -Click Here 

Tweepy: Tweepy is a Python library for accessing the Twitter API.

Installation of TextBlob and Tweepy 

-pip install textblob 

-pip install tweepy

Practical Implementation

#Import required libraries 

import tweepy 

from textblob import TextBlob

#Getting access to twitter data 





auth = tweepy.OAuthHandler(consumer_key,consumer_secret)



#Searching public tweet on a topic'Coronavirus India')

#Preprocessing of data (twitter data)

for tweet in public_tweets:






Let's understand the code first 

After importing required libraries we accessed Twitter data for which we need some tokens.After that, we searched a topic to see the tweets (opinion) of people/nation  about the topic ,here (India Coronavirus).

In order to machine to understand and read our comments, we used natural language processing (TextBlob).


As you can see in the output there are comments saying about topic (that we searched -coronavirus India).

I have marked two comments with a red circle to understand how sentiment analysis works.As you can see in our first comment polarity and subjectivity both are 0 means the comment is Neutral and very Objective (Factual).

And in the second circle (Second comment) polarity is in positive means the comment says something positive (here India is much better ... ) and comment is little subjective means some emotions are reflected in comment.

How to get consumer key , consumer secret, access token and access token secret ?

  1. To get a consumer key sign in with your twitter account on
  2. Create a Twitter developer app.
  3. Follow the steps and answer the questions asked.
  4. After authentication app (you will receive a email for this).
  5. Generate your app's API keys and user's access tokens.
  6. Copy and use them.

Machine Learning Tutorial

Free Online Tutorials

NLP Tutorial  

Our Mentors(For AI-ML)

Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune


Ms. Nisha

An alumna of IIT-BHU

Related questions

0 like 0 dislike
1 answer 300 views
0 like 0 dislike
1 answer 539 views
0 like 0 dislike
2 answers 257 views


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |