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 and . tweepy textblob 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 first before proceeding with this tutorial. emotion detection from the text 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 Libray (Twitter functionalities) tweepy Install Library (Natural language processing) TextBlob Twitter Developer (API KEYS) You need to to signup for twitter Developer Account and then apply to access for API Keys, Apply now 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. Tweepy Installation The easiest way to install the latest version from is by using pip: PyPI $ pip install tweepy You can also use Git to clone the repository from GitHub to install the latest development version: $ git clone https: $ cd tweepy $ pip install . //github.com/tweepy/tweepy.git Textblob Installation $ 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. . ├── API_KEYS.py └── app.py directories, files 0 2 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 api_key = api_secret_key = 'your api key' 'your api secret key' Now Let's start coding our script Importing Libraries and dependencies tweepy API, OAuthHandler textblob TextBlob API_KEYS api_key, api_secret_key from import from import from import Authenticating our script To start fetching tweets from Twitter, firstly we have to authenticate our app using the and . API key secret key To authenticate our API we will use as shown below; OAuthHandler 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; public_tweets = api.search(Topic) is iterable of tweets objects but in order to perform sentiment analysis, we only require the tweet text. public_tweets Therefore in order to access text on each tweet, we have to use the text property on the just as shown in the example below. tweet object from tweepy API, OAuthHandler from textblob TextBlob from API_KEYS api_key, api_secre authentication = OAuthHandler(api_key, api_secret_key) api = API(authentication) corona_tweets = api.search( ) tweet corona_tweets: = tweet. print( ) import import import 'corona virus' for in text text text When you run the above script it will produce a result similar to what shown below. $ 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 We all need therapy. RT @ug_chelsea: Corona virus symptoms basically are the same feelings you get when your wife is checking your phone 😟 https://s.w.org/images/core/emoji/12.0.0-1/svg/1f61f.svg Performing Sentiment analysis Now Let's use to perform sentiment analysis on those tweets to check out if they are or . TextBlob positive negative Textblob Syntax to checking positivity or negativity polarity = TextBlob(Text).sentiment.polarity If the polarity is less than it s positive 0 's negative If the polarity is greater than 0 it' 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 tweepy API, OAuthHandler textblob TextBlob API_KEYS api_key, api_secret_key def clean_tweets(tweet): tweet_words = str(tweet).split( ) clean_words = [word word tweet_words not word.startswith( )] .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= ) cleaned_tweets = [clean_tweets(tweet.text) tweet public_tweets] tweet cleaned_tweets: tweet_polarity = TextBlob(tweet).sentiment.polarity tweet_polarity< : negative_tweets.append(tweet) positive_tweets.append(tweet) positive_tweets, negative_tweets positive, negative = analyze( ) print(positive , , negative) print(len(positive), , len(negative)) from import from import from import ' ' for in if '#' return ' ' 10 for in for in if 0 continue return 'Magufuli' '\n\n' ' VS ' To change a topic you want to analyze or change the topic parameter in the to the topic of your interest. analyze function 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 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, to share this good news with your fellow developers. Tweet now I also recommend you to read this; How to translate languages using Python Emotion detection from the text in Python 3 ways to convert text to speech in Python How to perform speech recognition in Python Make your own Plagiarism detector in Python Learn how to build your own spam filter in Python Make your own knowledge-based chatbot in Python How to perform automatic spelling correction in Python The full code for this article can be found on :-) My Github Previously published at https://kalebujordan.com/twitter-sentiment-analysis/