Twitter Sentiment Analysis using Python Program


In this article, we will be learning about the twitter sentimental analysis. We will register for twitter oAuth API, install all the dependencies and finally write our sentimental analyzer script.

An API(Application programming interface) is a gateway that allows you to access some servers(Twitter) internal functionality.

The prerequisite is that we have a twitter account set up with verified phone number.

After this, we visit the twitters website and tap on the create a new app icon. Now we fill all the credentials i.e. name and accept the developer agreement and finally click on create.

Now our app is created , on the top menu, we will click on the keys tab . Here we will be obtaining our oAuth verfication details and all the tokenizers.

Now let's install all the dependencies 

1. tweepy module

>>> pip install tweepy

2. textblob module

>>> pip install textblob

what is textblob ?

It is a module used in sentiment analysis. It contains an inbuilt method to calculate sentiments on a scale of -1 to 1 .

"token.sentiment.polarity"

First we need all the access tokenizer from the twitter application website as created initially −

#Twitter credentials for the app interface
consumer_key = 'xxxxx'
consumer_secret = 'xxxx'
access_key= 'xxxx'
access_secret = 'xxxx'

No we need to authenticate the credentials via script. For that, we create an authentication variable auth.

auth = tweepy.OauthHandler(consumer_key,consumer_secret)

now we set the access token with the help of authentication variable

auth.set_access_token(access_token,access_token_secret)

Now we create an API variable to perform our operations

api = tweepy.API(auth)

we need to get the public tweets via search method and store it in the form of a list.

public_tweet = api.search('Tutorialspoint')
for tweet in public_tweet:
   print(tweet.text)
   analysis = TextBlob(tweet.text)
   print(analysis)

In the output, we observe to thing i.e. the polarity and subjectivity.

Polarity measures how positive or negative some text is.

Subjectivity measures the text that how much it is opinionated as compared to factual.

Conclusion

By the help of this sentiment analyzer, we are able to understand and extract human feelings out of the data.

Updated on: 16-May-2022

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements