In the same directory as this script, you'll need a file called 'config.py'. In that file, you'll need to put your twitter authentication credentials. Such as:
consumer_key = 'your_twitter_key'
consumer_secret = 'your_twitter_secret'
access_token = 'your_token'
access_token_secret = 'your_token_secret'
These are imported at the top of this script and are required for the script to run correctly.
- Pick a search phrase to pull tweets from twitter: search_phrase = '#abcxyz' # change this to the search phrase
- Set a search limit, the total number of tweets to pull in. Keep in mind twitter has a daily limit on API calls.
- Make a new TweetHandler, like this: handler = TweetHandler()
- Pull tweets with your search phrase and size limit: tweets = handler.get_tweet_attributes_map(search_phrase, tweet_limit)
- Optional: you can take out the 'RT' and '@' symbols from the tweet texts.
- Optional: get the nltk_sentiment scores for each tweet: sent_map = handler.add_nltk_sentiment_map(clean)
- Optional: geth the TextBlob sentiment score for each tweet. TextBlob ranks phrases by -1, 0, 1 (-1: negative, 0: neutral, 1: positive)
- Optional: stem the tweet texts. Stemming can change the meaning and sentiment of a phrase though.
You will now have a number of tweets with a datetime, text, device (such as "Android" or "iPhone" or "Web"), and optionally you can have stemmed or sentiment analyzed texts for each tweet. The tweet dictionary structure is like:
{
0: {
'date': datetime.datetime(2018, 8, 30, 19, 24, 47),
'device': 'Twitter for Android',
'text': 'As resident of AndrewGillum's Tallahassee...',
'nltk_pos': 0.26249691321696916,
'nltk_neu': 0.8573198090476511,
'nltk_neg': 0.7375030867830308,
'blob_sent': -1
},
1: {
'date': datetime.datetime(2018, 8, 30, 19, 24, 6),
'device': 'Twitter for Android',
'text': ' ScottPresler: The socialist and democrat ...',
'nltk_pos': 0.592341302535156,
'nltk_neu': 0.8948539758194366,
'nltk_neg': 0.40765869746484396,
'blob_sent': 0
}
}Note: This example shows unstemmed tweets. Stemmed tweets will have the same structure, but the text will be stemmed.