Calculating quantities with text

Note: Due to missing packages in DataCamp light, namely quanteda and tidytext, I have provided sample code that you can run on your own computer in RStudio. Make sure to run install.packages("quanteda") and install.packages("tidytext") to install those packages if you don’t have them.

Each of the three exercises below can be run as standalone scripts, as they contain all needed imports within their code blocks

Exercise 6: Readability with Quanteda

How does the readability of JPMorgan’s annual report compare to the Citigroup annual report from class?

# load in readr (or tidyverse) to get read_file() function
library(readr)
# Load in all of JPM's 2014 annual report
doc <- read_file("https://rmc.link/Slides/acct420/Session_8/0000019617-14-000289.txt")
# Load in quanteda
library(quanteda)
# Calculate the three readability measures
textstat_readability(doc, "Flesch.Kincaid")
textstat_readability(doc, "FOG")
textstat_readability(doc, "Coleman.Liau")
#END

Exercise 7: Readability with Quanteda

How does the sentiment of JPMorgan’s annual report compare to the Citigroup annual report from class?

# load in readr (or tidyverse) to get read_file() function
library(readr)
# Load in all of JPM's 2014 annual report
doc <- read_file("https://rmc.link/Slides/acct420/Session_8/0000019617-14-000289.txt")
# Load in tidytext
library(tidytext)
# Load some components of tidyverse
library(dplyr)  # for the usual commands
library(tidyr)  # for spread
package 㤼㸱tidyr㤼㸲 was built under R version 3.5.1
# convert document to tidy format
df_doc <- data.frame(ID=c("0000019617-14-000289"), text=c(doc),
                     stringsAsFactors = F) %>%
  unnest_tokens(word, text)
# Calculate term frequency
terms <- df_doc %>%
  count(ID, word, sort=TRUE) %>%
  ungroup()
package 㤼㸱bindrcpp㤼㸲 was built under R version 3.5.1
total_terms <- terms %>% 
  group_by(ID) %>% 
  summarize(total = sum(n))
tf <- left_join(terms, total_terms) %>% mutate(tf=n/total)
Joining, by = "ID"
# Get the Loughran McDonald sentiment dictionary
sentiment <- get_sentiments("loughran")
# Merge in sentiment
tf_sent <- tf %>% left_join(sentiment)
Joining, by = "word"
# Calculate the three readability measures
tf_sent %>%
  spread(sentiment, tf, fill=0) %>%
  select(constraining, litigious, negative, positive, superfluous, uncertainty) %>%
  colSums()
constraining    litigious     negative     positive  superfluous  uncertainty 
0.0127076134 0.0178352469 0.0308215361 0.0055735147 0.0001672054 0.0220153829 
#END

Exercise 8: Make a word cloud after removing stopwords

# load in readr (or tidyverse) to get read_file() function
library(readr)
# Load in all of JPM's 2014 annual report
doc <- read_file("https://rmc.link/Slides/acct420/Session_8/0000019617-14-000289.txt")
# Load in quanteda and tidytext
library(quanteda)
library(tidytext)
# Load in some of tidyverse
library(dplyr)
# convert document to tidy format
df_doc <- data.frame(ID=c("0000019617-14-000289"), text=c(doc),
                     stringsAsFactors = F) %>%
  unnest_tokens(word, text)
# Pull a list of stopwords
stopwords <- stopwords(source="smart")
# Remove stopwords
df_doc_stop <- df_doc %>%
  anti_join(data.frame(word=stopwords, stringsAsFactors=F))
Joining, by = "word"
# Build a corpus object for quanteda
corp <- corpus(df_doc_stop, docid_field="ID", text_field="word")
# Plot a word cloud -- If you don't have RColorBrewer install, you can
# remove the `color=` option.
textplot_wordcloud(dfm(corp), color = RColorBrewer::brewer.pal(10, "RdBu"))

#END
LS0tDQp0aXRsZTogIlNlc3Npb24gOCBSIHByYWN0aWNlIChvZmZsaW5lKSINCm91dHB1dDogaHRtbF9ub3RlYm9vaw0KYXV0aG9yOiAiRHIuIFJpY2hhcmQgTS4gQ3Jvd2xleSINCmRhdGU6ICJBQ0NUIDQyMCwgRmFsbCAyMDE4LCBTZXNzaW9uIDgiDQotLS0NCg0KIyMgQ2FsY3VsYXRpbmcgcXVhbnRpdGllcyB3aXRoIHRleHQNCg0KTm90ZTogRHVlIHRvIG1pc3NpbmcgcGFja2FnZXMgaW4gRGF0YUNhbXAgbGlnaHQsIG5hbWVseSBgcXVhbnRlZGFgIGFuZCBgdGlkeXRleHRgLCBJIGhhdmUgcHJvdmlkZWQgc2FtcGxlIGNvZGUgdGhhdCB5b3UgY2FuIHJ1biBvbiB5b3VyIG93biBjb21wdXRlciBpbiBSU3R1ZGlvLiAgTWFrZSBzdXJlIHRvIHJ1biBgaW5zdGFsbC5wYWNrYWdlcygicXVhbnRlZGEiKWAgY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