├── README.md └── demo.py /README.md: -------------------------------------------------------------------------------- 1 | # twitter_sentiment_challenge 2 | Twitter Sentiment Analysis Challenge for Learn Python for Data Science #2 by @Sirajology on Youtube 3 | 4 | ##Overview 5 | 6 | This is the code for the Twitter Sentiment Analyzer challenge for 'Learn Python for Data Science #2' by @Sirajology on [YouTube](https://youtu.be/o_OZdbCzHUA). The code uses the [tweepy](http://www.tweepy.org/) library to access the Twitter API and the [TextBlob](https://textblob.readthedocs.io/en/dev/) library to perform Sentiment Analysis on each Tweet. We'll be able to see how positive or negative each tweet is about whatever topic we choose. 7 | 8 | ##Dependencies 9 | 10 | * tweepy (http://www.tweepy.org/) 11 | * textblob (https://textblob.readthedocs.io/en/dev/) 12 | 13 | Install missing dependencies using [pip](https://pip.pypa.io/en/stable/installing/) 14 | 15 | ##Usage 16 | 17 | Once you have your dependencies installed via pip, run the script in terminal via 18 | 19 | ``` 20 | python demo.py 21 | ``` 22 | 23 | ##Challenge 24 | 25 | Instead of printing out each tweet, save each Tweet to a CSV file with an associated label. The label should be either 'Positive' or 'Negative'. You can define the sentiment polarity threshold yourself, whatever you think constitutes a tweet being positive/negative. Push your code repository to [github](https://help.github.com/articles/set-up-git/) then post it in the comments. I'll give the winner a shoutout a week from now! 26 | 27 | ##Credits 28 | 29 | This code is 100% Siraj baby. 30 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | import tweepy 2 | from textblob import TextBlob 3 | 4 | # Step 1 - Authenticate 5 | consumer_key= 'CONSUMER_KEY_HERE' 6 | consumer_secret= 'CONSUMER_SECRET_HERE' 7 | 8 | access_token='ACCESS_TOKEN_HERE' 9 | access_token_secret='ACCESS_TOKEN_SECRET_HERE' 10 | 11 | auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 12 | auth.set_access_token(access_token, access_token_secret) 13 | 14 | api = tweepy.API(auth) 15 | 16 | #Step 3 - Retrieve Tweets 17 | public_tweets = api.search('Trump') 18 | 19 | 20 | 21 | #CHALLENGE - Instead of printing out each tweet, save each Tweet to a CSV file 22 | #and label each one as either 'positive' or 'negative', depending on the sentiment 23 | #You can decide the sentiment polarity threshold yourself 24 | 25 | 26 | for tweet in public_tweets: 27 | print(tweet.text) 28 | 29 | #Step 4 Perform Sentiment Analysis on Tweets 30 | analysis = TextBlob(tweet.text) 31 | print(analysis.sentiment) 32 | print("") 33 | --------------------------------------------------------------------------------