Code Challenge 07 – Twitter Sentiment Analysis

By on 19 February 2017

This week, each one of you has a homework assignment … – Tyler Durden (Fight club)

Perform a sentiment analysis on a popular topic on Twitter

A new week, more coding! You are free to pick a topic. This can be a trend, news or movie. We will take latter:

How do Tweeters react on Fifty Shades Darker, positive or negative?

Getting ready

  • Register an Twitter app if not done already to get keys, put those in config.py (copying the config-template.py)

  • Make a virtual environment and pip install Twython (to follow along with our approach just do pip install -r requirements.txt)

  • We have provided a getting_data.py script (from Joel Grus) that uses the Twitter Streaming APIs to collect tweets, run it as follows. It takes 1000 tweets, adjust as necessary (this is also an experiment for us!)

    $ python getting_data.py Fifty Shades Darker
    # replacing with your topic of interest
    # generates data_unix_timestamp.json
    
  • If you prefer Tweepy, you can use this article/ script (and pip install that library …)

  • The script you will write is sentiment.py, it contains some code to read the stored json back in memory:

    $ python sentiment.py data_1487544849.json
    
  • Clean up the data, we did some of this in part 2 of this challenge series.

  • The sentiment analysis … is the audience positive or negative? We will try TextBlob for this, use any (Python) tools you prefer …

To follow along with our challenges

Start coding by forking our challenges repo:

$ git clone https://github.com/pybites/challenges

If you already forked it sync it:

# assuming using ssh key
$ git remote add upstream git@github.com:pybites/challenges.git
$ git fetch upstream
# if not on master:
$ git checkout master
$ git merge upstream/master
$ cd 07
$ cp sentiment-template.py sentiment.py
# if you want to follow along with our recommended libraries (assuming py >= 3.3)
$ python -m venv venv
$ source venv/bin/activate'
$ pip install -r requirements.txt
# code

Remember: there is no best solution, only learning more/ better Python. We’re looking forward reviewing our and your solutions end of this week. Good luck and have fun!


About PyBites Code Challenges

More background in our first challenge article.

Want a career as a Python Developer but not sure where to start?