Sentiment Analysis Python | Twitter Sentiment Analysis Python | Intellipaat

Sentiment Analysis Python | Twitter Sentiment Analysis Python | Intellipaat


Hey guys, welcome back to the Python
Certification Course. In today’s session, we are going to learn about
Sentiment Analysis. So, we’ll start of by understanding what exactly is sentiment
analysis, then we’ll understand the need of it, following which we will look at some very interesting applications of Sentiment
Analysis. Finally, we’ll do Twitter Sentiment Analysis. So, all of the big
companies out there try to understand the sentiments of their
customers. They try to analyze what are the customers talking about, how are
they saying it, and what do they exactly mean by it. So, this is where Sentiment
Analysis comes in. So, Sentiment Analysis is basically that particular domain
where you try to understand human emotions with a software. If these
human emotions are in written form, we can go ahead and classify these
sentiments to be positive, negative, or neutral. Sentiment Analysis is also
known as Opinion Mining because what we are basically doing is, trying to figure
out the opinion or the attitude of the customer with respect to a particular
product. So, this is basically Sentiment Analysis. Now we’ll go ahead and
understand the need of Sentiment Analysis. So, today, a customer plays a
very big role in the market. He is responsible for making or breaking your
business, and if a company is able to tap into all of the sentiments of the
customer, then it could be very beneficial for the company. So, this is
where a company can take the help of Sentiment Analysis and obtain useful
information which can be used to determine market strategy, improve
business KPIs, generate leads, and so on. This is basically the need of Sentiment
Analysis. So, now we’ll go ahead and look at some interesting applications of
Sentiment Analysis. So, Sentiment Analysis can be used for review classification. There are a lot of customers who post a lot of reviews, but then again how do we
know the sentiment associated behind these reviews. So, this is where we
can take the help of Sentiment Analysis and classify these reviews to be
positive, negative, or neutral. Another application of Sentiment
Analysis is product review mining. So, let’s say there’s a company which has a
product ‘A.’ Now the company wants to know what are the features which are
liked by the customer, and what are the features which are disliked by the
customer. So again, the company can take the help of Sentiment Analysis and
figure out what are the features liked by the customer and what are the features
disliked by the customer, and depending on that it can go ahead and improve this
particular product. So, Sentiment Analysis can also be used during election times.
Let’s say there are two candidates: candidate A and candidate B. With the
help of Sentiment Analysis, we can understand which candidate is more
popular in that particular area. In the past decade or so, there has been a
huge increase in the online activity across the globe. So, every single second,
people make millions of posts, and this is where social media plays a pivotal
role. Now social media is not just any other platform. People go on to social
media and express their views. They talk about their likes and they talk about
their dislikes, and this is where if a company is able to tap into all of these
sentiments, it can be very useful to it. There are a lot of social media
sites such as Twitter, Facebook, and LinkedIn. So, today, we’re going to do a
bit of Twitter Sentiment Analysis. So to do Sentiment Analysis with Twitter, we will actually need a developer account. So we’ll go to developer.twitter.com
and over here this is my profile which is already there. I’ll click on apps and then
go ahead and create a new application. So, we have this option to create an app.
If you guys want to create a new app, you’d have to click on this, fill up a
few details, and you’ll have your new app ready. But then again, I have already
created an app with this name over here: Sentiment123Demo. Now, let me have a
glance of the details. So, this is my app, and it contains these keys and
tokens. So in our code, we’d have to use these Consumer API keys and Access token and access token secret. So you’d have to note down these values and then put that
inside your code. Now let’s go ahead and do some Sentiment Analysis. So first, we need this tweepy package which would basically act as the API with Twitter,
and then with the help of text blob we can understand the sentiment of
different tweets. So, we’ll import these two: first is tweepy, next is textblob.
Then we’ll go ahead and give the values of these four: consumer key,
consumer key secret, access token, and access token secret. So, you have the same
values. This is the API key, API secret key, access token, and access token secret.
I’ve given the same values over here. After that, using tweepy.OAuthHandler, I would have to first give the consumer key and the consumer key secret,
and I’ll store that in this object auth. Now again, this auth object contains set
access token. Now I’ll give the values of access
token and access token secret. Now finally, I will send this object inside
the tweepy.API. So this is how we are basically establishing the connection
with Twitter. So, we have given all of these four values, and we are
authenticating this API. Then finally, we can go ahead and give the hashtag
for which we’d have to find the sentiment. Let’s say I want to find the
sentiment for this word ‘AVENGERS.’ So, using this object api, I’ll just type in
api.search, and it contains the keyword ‘AVENGERS,’ and I’ll store this in
the object public tweets. Now, I’ll start a ‘for’ loop. So, I will go through all of the
tweets which are stored in public tweets. I’ll start off by printing the first
tweet and then I’ll pass the tweet.text as a parameter for this text block
function and I’ll store the result in this analysis object. Now, this analysis
object helps me to find out the polarity and the subjectivity of these tweets. It
has these two things: one is polarity, next is subjectivity.
So first, what we’ll do is we’ll just check if the sentiment analysis.
sentiment[0]. So basically, this is the first element inside this list. So,
the value inside this is greater than zero. So, we are basically checking if
polarity is greater than zero, then we will just print that the sentiment
of this tweet is positive. On the other hand, if the polarity of the sentiment is
less than zero, then we’ll print negative. If it is not greater than zero, if it
is not less than zero, then it basically means that it is equal to zero. So, we’ll
just print that the tweet is neutral. Now, let me go ahead and run this. So, these are all of the tweets which
contain the keyword ‘AVENGERS,’ so this is a first tweet. We see that the polarity is 0.0, subjectivity is 0.0, and it is neutral. Next again this
is the tweet. Again polarity is 0.0, subjectivity is 0.0, and
then again it is neutral. We see that for most of these polarity, subjectivity is
zero, and it as neutral over here. For this case over here, polarity is 0.6. If the value of polarity is greater to +1, then it would mean
that it is highly positive. Similarly, if the value of polarity is closer to -1, then it would mean that the sentiment of the tweet is very negative.
What we have over here is subjectivity. So, subjectivity basically
tells us how subjective or how personal the tweet is. So, we see in this tweet that, I
just needed to share with everyone how Hertz is a truly amazing company, and for
this polarity is 0.6 and subjectivity is 0.9. Then again for
the statement over here, we see that polarity is 0.375, subjectivity is 4.7, and we see that it is positive. Then again for this
statement over here, polarity is 0.0, subjectivity 0.0, and it
is neutral. Guys, so this is how we can do Sentiment Analysis in Python. So,
this brings us to the end of this session, and do stay tuned to Intellipaat’s YouTube channel for more such informative videos.

6 thoughts to “Sentiment Analysis Python | Twitter Sentiment Analysis Python | Intellipaat”

  1. Guys, which technology you want to learn from Intellipaat? Comment down below and let us know so we can create in depth video tutorials for you.:)

  2. Guys everyday we upload in depth tutorial on your requested topic/technology so kindly SUBSCRIBE to our channel👉( http://bit.ly/Intellipaat ) & also share with your connections on social media to help them grow in their career.🙂

  3. Informative. Thanks!! Is that all we need to know about sentiment analysis or there is something more we can calculate?

  4. could you please tell the recruitment part for a become data scientist in product based company? many companies say they require experienced data scientist is there any space for fresher? if yes what path do they have to follow.?

Leave a Reply

Your email address will not be published. Required fields are marked *