Sentiment Analysis of Trump's Inauguration Speech

Converted from old blog, please excuse any oddities!

Figure 1

During the 2016 election, Trump came off to myself as an intrinsically negative candidate. I wondered after his election how his rhetoric would be adjusted as he had just won the presidency. After all, most of the time it is far easier to critisize someone’s performance. I figured this would be an excellent opportunity to break out R and use various packages to allow us to potentially gain insight into the speech structure of Trump’s Inaugural Address. I also plotted it alongside the first address from President Obama as a neat comparison. The code used to derive these charts is provided below.

require(tm)
require(ggplot2)
require(SnowballC)
require(plyr)
require(wordcloud)
#require(devtools)
#install.packages("tm.lexicon.GeneralInquirer", repos="http://datacube.wu.ac.at", type="source")
require(tm.lexicon.GeneralInquirer)



trump_2017 = readLines("trump_speech.txt")
obama_2009 = readLines("obama_speech.txt")

remove_language = function(x){
  x = gsub("[[:punct:]]", "", x)
  x = gsub("[[:digit:]]", "", x)
  x = gsub("http\\w+", "", x)
  x = gsub("[ \t]{2,}", "", x)
  x = gsub("^\\s+|\\s+$", "", x)
  return(x)
}

trump_2017 = remove_language(trump_2017)
obama_2009 = remove_language(obama_2009)

trump_corpus = Corpus(VectorSource(trump_2017))
obama_corpus = Corpus(VectorSource(obama_2009))

# Compute Polarity

compute_polarity = function(x){
  pos <- sum(sapply(x, tm_term_score, terms_in_General_Inquirer_categories("Positiv")))
  neg <- sum(sapply(x, tm_term_score, terms_in_General_Inquirer_categories("Negativ")))
  pos.score <- tm_term_score(TermDocumentMatrix(x, control = list(removePunctuation = TRUE)), 
                             terms_in_General_Inquirer_categories("Positiv")) 
  neg.score <- tm_term_score(TermDocumentMatrix(x, control = list(removePunctuation = TRUE)), 
                             terms_in_General_Inquirer_categories("Negativ")) 
  total.df <- data.frame(positive = pos.score, negative = neg.score)
  total.df <- transform(total.df, net = positive - negative)
  return(total.df)
}

pol_series = function(x){
  rows = nrow(x)
  for (a in 1:rows){
    if (a == 1){
      x[a,4] <- x[a,3]
    } else {
      x[a,4] <- x[a-1,4] + x[a,3]
    }
  }
  return(x)
}

trump_pol = compute_polarity(trump_corpus)
obama_pol = compute_polarity(obama_corpus)
tpol_series = pol_series(trump_pol)
opol_series = pol_series(obama_pol)


par(mfrow=c(2,1))

barplot(c(sum(obama_pol[,3]),sum(trump_pol[,3])),
        main = "Total Sentiment Score For Inaugural Address",
        ylab = "Sentiment Score", ylim = c(0,100),
        names = c("Obama (2009)","Trump (2017)"),
        xlab = "Speech", col = c("blue","red")
        )

plot(NA,
     xlim = c(0,80), ylim = c(-100,100),
     ylab = "Sentiment Score", xlab = "Corpus ID",
     main = "Cumulative Sentiment Score During Inaugural Address"
     )
abline(h = 0, lty = 2)
lines(tpol_series[,4], lty = 1, col = "red")
lines(opol_series[,4], lty = 1, col = "blue")
legend(55, -35, c("Trump (2017)","Obama (2009)"), 
       col=c("red","blue"), lty=1)

par(mfrow=c(1,1))