
Examples from https://www.tidytextmining.com/sentiment.html

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

```{r}
tidy_austen_books <- austen_books() %>%
  group_by(book) %>%
  mutate(
    linenumber = row_number(),
    chapter = cumsum(str_detect(text, 
                                regex("^chapter [\\divxlc]", 
                                      ignore_case = TRUE)))) %>%
  ungroup() %>%
  unnest_tokens(word, text)
tidy_austen_books 
```

```{r}
tidy_austen_books %>%
  filter(book == "Pride & Prejudice")
```

```{r}
positive_words <- get_sentiments() %>%
  filter(sentiment == "positive")
positive_words

tidy_austen_books %>%
  inner_join(positive_words, by = "word") %>%
  count(word, sort = TRUE)
```

This explains why I like Jane Austen - every book is net positive in sentiment.

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


