This analysis takes a look at the sentiment of the tweets of Justin Trudeau, the 23rd Prime Minister of Canada. The idea here was inspired by the various analyses on the tweets of Donald Trump, the 45th President of the United States. I look at the source and length of Trudeau’s tweets, as well as the number of favourites and retweets he received, from December 2016 to early October 2017. I also look at the sentiment of his tweets over this time period. This analysis is done for both English and French tweets.
The code can be found in a Jupyter notebook on my github.
This project, written in Python, demonstrates my experience with web scraping, data visualization, and sentiment analysis using the VADER and TextBlob packages.
Discussion of TextBlob and VADER:
TextBlob is a popular text processing package in Python, based on two other packages: pattern, and NLTK. TextBlob provides a more user-friendly experience than these other two packages, since they can be difficult to get started with. There are two sentiment analyzers in TextBlob, and are based on PatternAnalyzer (from pattern) and NaiveBayesAnalyzer (an NTLK classifier trained on a movie reviews corpus). The default implementation is PatternAnalyzer, and this is what I use. The sentiment() function returns a (polarity, subjectivity)-tuple based on the adjectives it contains. I focus on the polarity, which is between -1.0 and 1.0.
VADER is a rule-based lexicon that has been trained on social media data. VADER outputs sentiment as a compound score, which its developers describe as a “normalized, weighted composite score.” The compound score can be further divided into negative, neutral, or positive sentiment. For this analysis, I focus on the compound score.
The data comes from the Twitter API, which I use to download Trudeau’s most recent tweets. Unfortunately, Twitter’s API limits you to the latest 3240 tweets, so I can only go so far back in time. A plan for the future is how to work around this limitation. I removed the retweets from Trudeau’s timeline, as I’m interested in just what he posts. I also split the data set into English and French tweets.
1. Basic Exploratory Data Analysis:
A first look at the sources of Trudeau’s tweets reveal that the majority of his posts are made from the Twitter web client and Twitter for iPhone. A very small minority of tweets are posted from Twitter for Android. I think we can safely say that Trudeau uses an iPhone.
Next, a time series of the length of his posts reveal that there isn’t much of a difference between the languages.
In English, his most “favourited” and “retweeted” tweet has to do with welcoming refugees to Canada. To me this says that Canadians are welcoming, and we like it when our Prime Minister portrays this image to the world.
Interestingly, the most favourited tweet in French is not to do with refugees (the most retweeted still is, though), but rather a tweet posted around the time of the Toronto Pride Parade in 2017. Perhaps French Canadians want to support the rights of the LGBTQ community, but are too shy to retweet?
2. Sentiment Analysis
In the second part of this report, we’ll look at the sentiment of Trudeau’s tweets, according to VADER and TextBlob, in English and French. VADER assigns a more positive sentiment to Trudeau’s English tweets.
TextBlob, however, asserts that Trudeau’s tweets are more positive in both languages.
Comparing the VADER English results to TextBlob English results, we can see in another way that TextBlob assigns a more positive sentiment, generally.
While in French, VADER and TextBlob show more similarities.
Finally, we consider the average monthly sentiment of Trudeau’s tweets. The average monthly sentiment of Trudeau’s tweets comes across as more positive in English according to VADER. However, the general trend is similar for all four cases, which is good.
It can be seen that Justin Trudeau’s tweets are generally positive, which is consistent with his public image. This is a huge contrast to Trump, whose tweets show a full range of emotion. Trump has been found to tweet spontaneously, while Trudeau’s tweets are carefully crafted and checked before publication. Sentiment analysis using TextBlob yields results that are more consistent across the two languages, while VADER suggests that Trudeau’s tweets are much more positive in English than in French. This could be due to the fact that TextBlob has been optimized for both languages, while VADER is mostly used for English social media posts. Regardless, it’s an interesting exercise.