The President of the United States, Donald Trump is arguably the most polarizing figure in the world today. No matter where your political allegiances lie, you likely have at least a somewhat strong opinion on Trump. Whether itā€™s the volatility of his actions or the brashness of the way he does what he does, it seems everyone is fixated on him.

This has been reflected in tons of interesting projects such as Trump Tracker, FiveThirtyEights Popularity Ratings, and Track Trump. With these projects in mind, I thought I would throw my hat in the ring and ship something of my own.

trump.png

Overview

The Trump Sentiment Tracker uses real-time twitter data to determine the current public perception of President Donald Trump. The web application streams all tweets containing the word ā€˜Trumpā€™ within the last five minutes of being launched. The tweets are then put through sentiment analysis in order to determine how positive or negative they are. Lastly, the sentiment scores are averaged and reported to you using an intuitive visualization.

Not doing so hot right nowā€¦
Not doing so hot right nowā€¦

See the Project

If you arenā€™t interested in any of the behind the scenes stuff, no worries. Feel free to skip all the detail below and check out the code on my Github or the work itself at this link below:

Trump Sentiment Tracker

Outline

The full process is outlined below and explained in more detail throughout the rest of the article:

  1. Created a twitter streamer that collects tweets with the word ā€˜Trumpā€™ and transfers them into a DataFrame
  2. Utilized the VADER Sentiment Analysis package in order to speculate how positive or negative each given tweet was
  3. Adjusted the time frame to the last five minutes and averaged the sentiment scores for all the tweets
  4. Transferred the back-end to a Flask application for deployment
  5. Planned out how I wanted to effectively convey insights and designed a user interface
  6. Put it all together and hosted the web app on Heroku

Goals

I came into this project wanting to accomplish several things. First and foremost, I wanted to improve my full-stack development related skill sets. Prior to this, I had done back-end and front-end programming separately but I had never taken a project the full distance. On top of this, I was also eager to take the opportunity to design something. Iā€™ve recently developed an interest in design and I had never developed a web application user interface before diving into this project.

Tools and Software

Coming into this project with little development background with Python, my first task was to decide on an environment. After a bit of consideration, I ended up going with Pycharm (Version 2017.1.5). I found Pycharm to be very intuitive and effective for web application development. Iā€™m sure Iā€™ll be going back to it sometime in the near future. If you are a student and are interested in Pycharm, you can get it for free here.

I also used Python 3.5.2 and several other dependencies that you can find specified in the Github repository under the requirements.txt file. Lastly, I used Heroku to host the web application.

Quick look at the Pycharm environment and workflow
Quick look at the Pycharm environment and workflow

Back-End

The first step of the project was to figure out a way to stream tweets and then format them properly for analysis. For the streaming, I choose to use Tweepy and implemented a basic listener that while being run would collect every tweet with the word ā€˜Trumpā€™ in it. The class then takes the current tweet and formats it properly to be added to the ongoing pandas DataFrame. The DataFrame is constantly updated to only keep data within a certain time threshold (5 minutes) in order to get virtually real-time insights.

After taking in data and formatting it correctly, sentiment analysis is performed. Following a bit of research, I decided to go with VADER (Valence Aware Dictionary and Sentiment Reasoner). I choose VADER because of itā€™s aptitude for social media data specifically.

Front-End

As far as the user interface goes, it was created using D3.js and HTML primarily. The meter was developed using Matt Magoffinā€™s open source project as a jumping off point and the general feel of the website was in large part motivated by Track Trump.

One of the things that I struggled with the most was integrating the back-end with the front-end so that the meter would correspond with the current mean sentiment score. Eventually, I was able to work around this problem by combining different JavaScript files and using getters and setters to my advantage.

Deployment

Once the project was working locally, the next step was to figure out how I wanted to deploy it. I considered a few different alternatives but ended up going with Heroku due to the abundance of great tutorials out there. That, and the fact that itā€™s free. I found the following video especially helpful, if any of you plan on checking out Heroku anytime soon, I highly recommend it.

Learning Resources

As you can probably tell, I came into this journey needing to acquire some skills that werenā€™t quite where I wanted them to be. In order to do this, I used a seemingly infinite amount of online resources. However, there were two that really stood out to me in the form of the free Full Stack Development course by Udacity and various Youtube tutorials by sentdex.

Reflection

Now that the project is shipped, I can look back and be very proud of what I was able to accomplish. There was times throughout the learning process where I undoubtedly struggled and thought about quitting but Iā€™m glad that I stuck it out. By sticking with it and staying patient, I ended up acquiring some skills and techniques that I wouldnā€™t have right now otherwise.

Coming into this endeavor, I was admittedly quite inexperienced in full-stack development and web design. However, thanks to this project and all the steps I had to take in order to complete it, I can say that I am a much more competent and confident developer now.

Moving forward, Iā€™m excited to see if Trump Sentiment Tracker gets any interest and most of all, Iā€™m excited to start my next project.

Wrapping Things Up

I hope you enjoyed hearing about the making of Trump Sentiment Tracker. If youā€™re interested in the code, be sure to check it out on Github.


Thanks for reading! If you enjoyed this post and youā€™re feeling generous, perhaps follow me on Twitter. You can also subscribe in the form below to get future posts like this one straight to your inbox. šŸ”„