A quick guide to Twitter sentiment analysis in Python
Tags: python, nlp, machinelearning, datascience
Published: 2020-11-09 09:34:42

>The [original article](https://kalebujordan.dev/analyze-tweets-sentiment-using-python/) can be found at [kalebujordan.dev](https://kalebujordan.dev/)*

Hello, Guys,

In this tutorial, I will guide you on how to perform sentiment analysis on textual data fetched directly from Twitter about a particular matter using [*tweepy *](https://www.tweepy.org/)and [*textblob*](https://textblob.readthedocs.io/en/dev/).



### what is sentiment analysis?

Sentiment analysis is a process of analyzing emotion associated with textual data using natural language processing and machine learning techniques.

#### If you're new ...

If you're new to sentiment analysis in python I would recommend you watch [emotion detection from the text](https://kalebujordan.dev/how-to-detect-emotions-associated-with-text-using-python/) first before proceeding with this tutorial.

### what are we going to build ..

We are going to build a python command-line tool/script for doing sentiment analysis on Twitter based on the topic specified.

#### How will it work ?

You will just enter a topic of interest to be researched in twitter and then the script will dive into Twitter, scrap related tweets, perform sentiment analysis on them and then print the analysis summary.

### Project requirements

To follow through tutorial you need the following

-   Sign up for twitter to Developers to get API Key
-   Install [tweepy](http://docs.tweepy.org/en/latest/) Libray (Twitter functionalities)
-   Install [TextBlob](https://textblob.readthedocs.io/en/dev/) Library (Natural language processing)

#### Twitter Developer (API KEYS)

You can follow through this link [Signup](https://developer.twitter.com/en/apply-for-access) in order to signup for twitter Developer Account to get API Key.

Once you signup for a developer account and apply for Twitter API, It might take just a few hours to a few days to get approval.

After being approved Go to your app on the Keys and Tokens page and copy your api_key and API secret key in form as shown in the below picture.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yevksyh8871kcxsewd2a.png)
#### Tweepy Installation

The easiest way to install the latest version from [PyPI ](https://pypi.org/)is by using pip:

```bash
$ pip install tweepy
```

You can also use Git to clone the repository from GitHub to install the latest development version:

```bash
$ git clone https://github.com/tweepy/tweepy.git

$ cd tweepy

$ pip install .
```

#### TextBlob Installation
```bash
$ pip install -U textblob

$ python -m textblob.download_corpora

```

### Building our Twitter analysis tool

Now after everything is clearly installed, let's get hand dirty by coding our tool from scratch.

#### Put API_KEYS on a separate file

First of all, I have separated the project into two files, one consisting of API keys while the others consisting of our code for the script.

```bash
.

├── API_KEYS.py

└── app.py
​

0 directories, 2 files
```

If we look inside the API_KEYS.py it looks as shown below whereby the value of api_key and api_secret_key will be replaced by your credentials received from Twitter

- API_KEYS.py

```python
api_key = 'your api key'

api_secret_key = 'your api secret key'
```

### Now Let's start coding our script

#### Importing Libraries and dependencies
```python

from tweepy import API,  OAuthHandler

from textblob import TextBlob

from API_KEYS import api_key, api_secret_key
```

#### Authenticating our script

To start fetching tweets from Twitter, firstly we have to authenticate our app using the **API key** and **secret key**.

To authenticate our API we will use **OAuthHandler** as shown below

```python
authentication = OAuthHandler(api_key, api_secret_key)

api = API(authentication)

```

#### Fetching tweets on Particular Topic

To fetch tweets from Twitter using our Authenticated API use the **search** method fetch tweets about a particular matte just as shown below;

```python
public_tweets = api.search(Topic)
```
public_tweets is iterable of tweets objects but in order to perform sentiment analysis, we only require the tweet text.

Therefore in order to access text on each tweet, we have to use the text property on the **tweet object** just as shown in the example below.

```python
from tweepy import API,  OAuthHandler

from textblob import TextBlob

from API_KEYS import api_key, api_secre

​

authentication = OAuthHandler(api_key, api_secret_key)

api = API(authentication)

corona_tweets = api.search('corona virus')

for tweet in corona_tweets:

 text = tweet.text

 print(text)
```

When you run the above script it will produce a result similar to what shown below.

```bash 
$ python example.py

......

RT @amyklobuchar: So on Frontier Airlines you now have to pay an extra fee to keep yourself safe from corona virus. As I said today at the...

There are so many disturbing news these days ON TOP OF CORONA VIRUS. It just sinks my heart![😟](https://s.w.org/images/core/emoji/12.0.0-1/svg/1f61f.svg) We all need therapy.

RT @ug_chelsea: Corona virus symptoms basically are the same feelings you get when your wife is checking your phone
```

#### Performing Sentiment analysis

Now Let's use **TextBlob** to perform sentiment analysis on those tweets to check out if they are *positive* or *negative*.

#### Textblob Syntax to checking positivity or negativity

```python
polarity = TextBlob(Text).sentiment.polarity
```

If the polarity is less than 0 it's negative

If the polarity is greater than 0 it's positive


#### Final Application

I then compiled the above knowledge we just learned to build the below script with the addition of the clean_tweets function to remove hashtags in tweets

```python
from tweepy import API,  OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secret_key

def clean_tweets(tweet):
    tweet_words = str(tweet).split(' ')
    clean_words = [word for word in tweet_words if not word.startswith('#')]
    return ' '.join(clean_words)

def analyze(Topic):
    positive_tweets, negative_tweets = [], []
    authentication = OAuthHandler(api_key, api_secret_key)
    api = API(authentication)
    public_tweets = api.search(Topic, count=10)
    cleaned_tweets = [clean_tweets(tweet.text) for tweet in public_tweets]
    for tweet in cleaned_tweets:
        tweet_polarity = TextBlob(tweet).sentiment.polarity
        if tweet_polarity<0:
            negative_tweets.append(tweet)
            continue
        positive_tweets.append(tweet)
    return positive_tweets, negative_tweets


positive, negative = analyze('Magufuli')
print(positive , '\n\n', negative)
print(len(positive), ' VS  ', len(negative))
```

To change a topic you want to analyze or change the topic parameter in the **analyze function** to the topic of your interest.

Also, you can specify the number of tweets to be fetched from Twitter by changing the count parameter.

When you run the above application it will produce results to what shown below

```bash
python app.py

.....................

 ['@o_abuga Obvious, the test kits the results are doubtful!! Magufuli said it']

9  VS   1

```

🎉🎉🎉 Congratulations you have just completed a tutorial on Twitter sentiment analysis using python, You should be proud of yourself, [Tweet now](https://twitter.com/intent/tweet?text=Hello guys just completed how tutorial on Twitter sentiment analysis using python by @j_kalebu, check it out at https://kalebujordan.com/twitter-sentiment-analysis/) to share this good news with your fellow developers.

### I also recommend you to read this;

-   [How to translate languages using Python](https://kalebujordan.dev/translating-languages-using-python/)
-   [Emotion detection from the text in Python](https://kalebujordan.dev/how-to-detect-emotions-associated-with-text-using-python/)
-   [How to perform speech recognition in Python](https://kalebujordan.dev/speech-recognition-using-python/)
-   [Make your own Plagiarism detector in Python](https://kalebujordan.dev/how-to-detect-plagiarism-in-text-using-python/)

{% github https://github.com/Kalebu/Twitter-Sentiment-analysis-Python %}