| | - philosophers_stone: Harry Potter and the Philosophers Stone (1997) - chamber_of_secrets: Harry Potter and the Chamber of Secrets (1998) - prisoner_of_azkaban: Harry Potter and the Prisoner of Azkaban (1999) - goblet_of_fire: Harry Potter and the Goblet of Fire (2000) - order_of_the_phoenix: Harry Potter and the Order of the Phoenix (2003) - half_blood_prince: Harry Potter and the Half-Blood Prince (2005) - deathly_hallows: Harry Potter and the Deathly Hallows (2007) |
|
- Capture your results below:
#install.packages("Rling")
library(Rling)
library(modeest)
#install.packages("textdata")
library(textdata)
#install.packages("tidytext")
library(tidytext)
library(knitr)
if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}# data manipulation & plotting
## Loading required package: tidyverse
## Registered S3 method overwritten by 'httr':
## method from
## print.response rmutil
## ── Attaching packages ────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
if (!require("stringr")) {
install.packages("stringr")
library(stringr)
}# text cleaning and regular expressions
if (!require("tidytext")) {
install.packages("tidytext")
library(tidytext)
}# provides additional text mining functions
if (!require("harrypotter")) {
install.packages("harrypotter")
library(harrypotter)
}# provides the first seven novels of the Harry Potter series
## Loading required package: harrypotter
if (!require("sentimentr")) {
install.packages("sentimentr")
library(sentimentr)
}
## Loading required package: sentimentr
if (!require("devtools")) {
install.packages("devtools")
library(devtools)
}
## Loading required package: devtools
## Loading required package: usethis
install_github('trinker/sentimentr')
## Skipping install of 'sentimentr' from a github remote, the SHA1 (645401e8) has not changed since last install.
## Use `force = TRUE` to force installation
if (packageVersion("devtools") < 1.6) {
install.packages("devtools")
}
devtools::install_github("bradleyboehmke/harrypotter")
## Skipping install of 'harrypotter' from a github remote, the SHA1 (51f71461) has not changed since last install.
## Use `force = TRUE` to force installation
library(harrypotter)
str(prisoner_of_azkaban)
## chr [1:22] "  OWL POST  Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays"| __truncated__ ...
azkaban_sentences <- tibble(chapter = 1:length(prisoner_of_azkaban),
text = prisoner_of_azkaban) %>%
unnest_tokens(sentence, text, token = "sentences")
book_sentence <- azkaban_sentences %>%
group_by(chapter) %>%
mutate(sentence_num = 1:n(),
index = round(sentence_num / n(), 2)) %>%
unnest_tokens(word, sentence) %>%
inner_join(get_sentiments("afinn")) %>%
group_by(chapter, index) %>%
summarise(sentiment = sum(value, na.rm = TRUE)) %>%
arrange(desc(sentiment))
## Joining, by = "word"
book_sentence
## # A tibble: 1,848 x 3
## # Groups: chapter [22]
## chapter index sentiment
## <int> <dbl> <dbl>
## 1 4 0.01 13
## 2 1 0.49 12
## 3 4 0.9 12
## 4 5 0.87 12
## 5 6 0.75 12
## 6 11 0.87 12
## 7 5 0.88 11
## 8 8 0.07 11
## 9 8 0.45 11
## 10 11 0.56 11
## # … with 1,838 more rows
ggplot(book_sentence, aes(index, factor(chapter, levels = sort(unique(chapter), decreasing = TRUE)), fill = sentiment)) +
geom_tile(color = "white") +
scale_fill_gradient2() +
scale_x_continuous(labels = scales::percent, expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
labs(x = "Chapter Progression", y = "Chapter") +
ggtitle("Sentiment of Prisoner of Azkaban") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top")