Master Python through building real-world applications (Part 5)

Because whether we like it or not, people around the world put thousands of reactions and opinions on every topic they might encounter, every day, every second.

Before diving into the code, let’s first understand the basic mechanism behind sentiment analysis.

How Sentiment Analysis WorksWe receive some input text, in our case, a tweet.

We split it into several words or small sentences, depending on the length of the input.

This process is called Tokenization.

Creating small tokens from big texts.

Once we have words, we can count the number of times each word shows up, which is called the Bag of Words model (these are just names, you don’t have to remember it)Now, as a final step, the sentiment value of each word is determined and once that is done, the overall sentiment value of our input is calculated.

That’s that.

That is how it works.

Now that you know how it works theoretically, let’s learn how it works practically.

Step 1 — Getting things readyWe only need two libraries to perform sentiment analysis using Twitter.

First is tweepy, a Python library for accessing Twitter API.

And the second is textblob, which is a library for processing textual data.

Also, it provides simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, and more.

To use Twitter’s data, you’ll have to go to the Developer Apps page on Twitter and create an application.

Creating an application will give you your own set of keys that we will use later in this article.

Step 2 — Authenticate your applicationAs soon as you have the keys and tokens, the next thing to do is authenticate yourself.

0AuthHandler takes the authentication keys.

Access tokens define the permission — Read, Write or Both.

Once that is complete, provide that to tweepy’s API method and it’s done.

Step 3 — Sentiment AnalysisWith help of our authenticated account (api), we can search for specific keywords for which we need sentiment analysis.

Once the keyword is set and tweets are called, we will handle tweets with textblob.

First, we will print out the latest tweets related to our keyword.

After that, we will use textblob to find the sentiment of that particular tweet and print that out too.

Let’s see what Apple’s CEO Tim Cook has to say.

As you see the output, It prints out series of tweets and with that its sentiment analysis.

The important thing to note here is, Polarity indicates how positive or negative the tweet is ( -1 < sentiment < 1) and subjectivity measure how much of a personal opinion is there in the text.

End NotesI tried to keep this post concise as Sentiment Analysis is an important aspect in Data Science and information overload might set you off even without starting.

There are enormous applications where Sentiment Analysis is used, I encourage you to explore them.

You can reach out to me via email, Twitter or even Linkedin if you have any doubts.

Also, the entire code can be found on my GitHub repository.

Happy Learning.

___________Part 1 Building an Interactive DictionaryPart 2 Creating Web Maps using FoliumPart 3 Building a Website BlockerPart 4 Build and Deploy a Website using Flask and Heroku AppPart 5 Twitter Sentiment Analysis.

. More details

Leave a Reply