
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r echo = TRUE, message = FALSE, warning = FALSE}
library(tidyverse)
library(tidytext)
library(rvest)
```

```{r}
get_words <- function(url, start, end, title) {
  word_lists <- read_html(url) %>%
    html_nodes("body") %>%
    html_nodes("p") %>%
    html_text() %>%
    str_remove_all("[:punct:]") %>%
    str_split("\r\n") %>%
    pluck(1) 
  word_lists[start:end] %>%
    tibble() %>%
    rename(text = ".") %>%
    unnest_tokens(word, text) %>%
    mutate(title = title)
}
```


```{r}
macbeth_url <- "http://www.gutenberg.org/cache/epub/1129/pg1129.txt"
macbeth <- get_words(macbeth_url, 212, 3183, "macbeth")

ado_url <- "http://www.gutenberg.org/cache/epub/1519/pg1519.txt"
ado <- get_words(ado_url, 28, 4624, "ado")

plays <- rbind(macbeth, ado)
plays
```

```{r}
plays_sentiment <- plays %>%
  inner_join(get_sentiments()) %>%
  group_by(title) %>%
  count(sentiment) %>%
  pivot_wider(
    names_from = sentiment, 
    values_from = n, 
    values_fill = 0) %>%
  mutate(
    sentiment = 
      (positive - negative) / (positive + negative)
  )
plays_sentiment
```


