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
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
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
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
consumer_key='NiQsBDjlRWpQ3feDFlFPOIQ1S'
consumer_secret='eet7axREyPLMfzK2ejVaJjETzUiwGf3I9tGFFciJTNn4YJ6IHQ'
access_token='750782455683121154-4K1Ltq50DjXRs6A2fn3psytkspDl5o8'
access_token_secret='LRozr69SdslinsHa5vjluCrbKIEZRx7iaezt77ANDiH92'
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api=tweepy.API(auth)
#Searching public tweet on a topic
public_tweets=api.search('Coronavirus India')
#Preprocessing of data (twitter data)
for tweet in public_tweets:
print(tweet.text)
analysis=TextBlob(tweet.text)
print(analysis.sentiment)
|
Output
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).
Output
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 ?
- To get a consumer key sign in with your twitter account on https://developer.twitter.com/en.
- Create a Twitter developer app.
- Follow the steps and answer the questions asked.
- After authentication app (you will receive a email for this).
- Generate your app's API keys and user's access tokens.
- Copy and use them.
Machine Learning Tutorial
Free Online Tutorials
NLP Tutorial