The approach for this assignment Is to obtain the primary code and insert it into the QMD File. Determine a different text corpus and insert a lexicon and understand what a lexicon is. I don’t fully understand what is being asked for a sentiment lexicon.
tidy_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)# Joy words in Emmanrc_joy <-get_sentiments("nrc") %>%filter(sentiment =="joy")tidy_books %>%filter(book =="Emma") %>%inner_join(nrc_joy) %>%count(word, sort =TRUE)
Joining with `by = join_by(word)`
# A tibble: 301 × 2
word n
<chr> <int>
1 good 359
2 friend 166
3 hope 143
4 happy 125
5 love 117
6 deal 92
7 found 92
8 present 89
9 kind 82
10 happiness 76
# ℹ 291 more rows
# Sentiment across novelstidy_books %>%inner_join(get_sentiments("bing")) %>%count(book, index = linenumber %/%80, sentiment) %>%pivot_wider(names_from = sentiment, values_from = n, values_fill =0) %>%mutate(sentiment = positive - negative) %>%ggplot(aes(index, sentiment, fill = book)) +geom_col(show.legend =FALSE) +facet_wrap(~book, ncol =2, scales ="free_x")
Joining with `by = join_by(word)`
Warning in inner_join(., get_sentiments("bing")): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 435434 of `x` matches multiple rows in `y`.
ℹ Row 5051 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
"many-to-many"` to silence this warning.
Extend the Analysis –> Different Corpus, Lincoln Speech and Syuzhet sentiment lexicon
We reproduced the sentiment analysis from Chapter 2 which was on Jane Austen’s novels and applied these techniques to Abraham Lincolns speech with a different lexicon from “syuzhet” package.
Some takeaways are the difference in sentiment analysis. Austen’s novels trend more positive. Lincolns speech is heavier and more negative reflecting the nature of the speech. All lexicons captured similar relative sentiment trajectories.