Data import

The NGO text reports are in the pdf form and are imported into the R environment. The text from each page are scraped and ready for further text processing

library(pdftools)
library(tidyverse)
library(quanteda)
library(tm)
library(wordcloud)
library(Rstem)
text <- pdf_text("C:/Users/Janani Selvaraj/Desktop/AR 2015.pdf")
text2 <- text %>% str_squish()%>%
  str_replace_all(",","")%>%
  strsplit(split = " ")

Text pre processing

text2 <- tolower(text2)
text2<- removeWords(text2, c("\\f", stopwords()))
corpus <- Corpus(VectorSource(text))
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
corpus <- tm_map(corpus, content_transformer(removeNumbers))
tdm <- TermDocumentMatrix(corpus)
m <- as.matrix(tdm)
d <- data.frame(freq = sort(rowSums(m), decreasing = TRUE))
d$stem <- wordStem(row.names(d), language = "english")
d$word <- row.names(d)
d <- d[nchar(row.names(d)) < 20, ]
agg_freq <- aggregate(freq ~ stem, data = d, sum)
agg_word <- aggregate(word ~ stem, data = d, function(x) x[1])
d <- cbind(freq = agg_freq[, 2], agg_word)
d <- d[order(d$freq, decreasing = T), ]
wordcloud(d$word, d$freq)

library(tidytext)
library(dplyr)
library(topicmodels)
library(ggplot2)
terms <- Terms(tdm)
td <- tidy(tdm)
sentiments <- td %>%
  inner_join(get_sentiments("bing"), by = c(term = "word"))
sentiments %>%
  count(sentiment, term, wt = count) %>%
  ungroup() %>%
  filter(n >= 3) %>%
  mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
  mutate(term = reorder(term, n)) %>%
  ggplot(aes(term, n, fill = sentiment)) +
  geom_bar(stat = "identity") +
  ylab("Contribution to sentiment") +
  coord_flip()