The best way to guage how people feel about a certain topic is to perform analysis on the words they use in a piece of text to best guess their overall emoticon.
This process is more commonly known as sentiment analysis.
Combine this technique with the treasure trove of opinions and reactions to a wide variety of topics on Twitter and we have a whole datamine at our disposal!
Thats all well and good, but how does sentiment analysis work?
Sentiment analysis at its core works of the idea that each word in a sentence has its own sentiment value which is looked up in a predefined lexicon.
First of all, the sentence is tokenized (each word is extracted from the overall sentence) and a sentiment value is assigned.
This is known as the bag of words model.
Ok, but how do we get the Twitter data to perform analysis on?
First of all, we need to import our dependencies and authenticate ourselves with the Twitter API.
import tweepy from textblob import TextBlob import datetime import csv
To access Twitters developer tools which include searching for Tweets, you need to sign into Twitter and then generate yourself a consumer & access token/secret.
# Enter your Twitter credentials below consumer_key = '' consumer_secret = '' access_token = '' access_token_secret = '' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Authenticates with Twitter api = tweepy.API(auth, wait_on_rate_limit=True)
The next step is simply to acess each Tweet and for demo purposes. Simply print it out to the console.
for tweet in tweepy.Cursor(api.search, q="Trump", result_type="recent", lang="en").items(100): print(tweet)
You can then easily print out the sentiment value for each Tweet by adding in:
analysis = TextBlob(tweet.text) print(analysis.sentiment)
This can then be outputted to a CSV file of parsed out in any other format that you would like.
I have taken the time to put together a quick script that shows the complete process from aquiring the tweets from Twitter. Through to generating a CSV file.
This can be seen here.