Just finished a few hours text mining lesson, can’t wait to put my new skill into practice, starting from Trump’s tweets.
First, apply API keys from twitter.
The maximize request is 3200 tweets, I got 815, which is not bad.
Have a look the first three, then convert all these 815 tweets to a data frame.
Text cleaning process, which includes convert all letters to lower case, remove URL, remove anything other than English letter and space, remove stopwords and extra white space.
Look at these three tweets again.
Need to replace a few words, such as “peopl” to “people”, “whitehous” to “whitehouse”, “countri” to “country”.
Building term document matrix
This is a matrix of numbers (0 and 1) that keeps track of which documents in a corpus use which terms.
As you can see, the term-document matrix is composed of 2243 terms and 815 documents(tweets). It is very sparse, with 100% of the entries being zero. Let’s have a look at the terms of ‘clinton’, ‘bad’ and ‘great’, and tweets numbered 21 to 30.
What are the top frequent terms?
A picture worth a thousand words.
Word Cloud
Which word/words are associated with ‘will’?
Which word/words are associated with ‘great’? - This is obvious.
Which word/ words are associated with ‘bad’?
Clustering Words
We can see the words in the tweets, words “will, great, thank, us, join, today” are not clustered into any group, “hillari, clinton” are clustered into one group, “now, president, elect, time, go, make, america, state, watch, get, vote” are clustered in one group, “people, country” are clustered into one group, and “just, nows” are clustered into one group.
Clustering Tweets with the k-means Algorithm
Check the top three words in every cluster
I have admit that I can’t easily distinguish cluster 2, cluster 3 and cluster 6 of Trump’s tweets are of different topics.
Sentiment Analysis
The sentiment analysis algorithm used here is based on NRC Word Emotion Association Lexion, available from the tidytext package which developed by Julia Silge and David Robinson. The algorithm associates with eight basic emotions (anger, fear, anticipation, trust, surprise, sadness, joy, and disgust) and two sentiments (negative and positive).
Sometimes there are tweaks I need to do to get rid of the problem characters.
Have a look the head of Trump’s tweets sentiment scores:
Then combine Trump tweets dataframe and Trump sentiment dataframe together.
Let’s visualize it!
Trump’s tweets appear more positive than negative, more trust than anger. Has Trump’s tweets always been positive or only after he won the election?
The positive sentiment scores are always higher than the negative sentiment scores. And the negative sentiment experienced a significant drop recently, the positive sentiment have increased to the highest point. However, the simple text mining process conducted in this post does not make this conclusion. A more sophisticated text analysis of Trump’s tweets by David Robinson found that Trump writes only the angrier half from Android, and another postive half from his staff using iPhone.
The end
I really enjoyed working on Trump Tweets analysis. Learning about text mining and social network analysis is very rewarding. Thanks to Julia Silge and Yanchang Zhao’s tutorials to make it possible.
Source code that created this post can be found here. I am happy to hear any feedback or questions.