library(tidyverse)
library(tidytext)
library(janeaustenr)
library(quanteda)
library(syuzhet)
library(knitr)
# Make the plots reproducible and visually consistent.
theme_set(theme_minimal(base_size = 12))Assignment 10A: Reproducing and Extending Sentiment Analysis with R
Jane Austen Novels and U.S. Presidential Inaugural Addresses
0.1 Purpose and assignment requirements
This report reproduces the principal sentiment-analysis workflow from Chapter 2 of Text Mining with R: A Tidy Approach by Julia Silge and David Robinson, then extends that workflow to a different corpus and adds an additional sentiment lexicon. The original example uses Jane Austen’s novels. The extension uses U.S. presidential inaugural addresses supplied by the quanteda package. All reported statistics and charts below are calculated during rendering, not hard-coded.
Research questions. (1) How does net sentiment vary across Jane Austen’s novels? (2) What words and emotions drive the findings? (3) How does sentiment vary across inaugural addresses? (4) How do Bing, AFINN, and an additional Jockers–Rinker lexicon differ when applied to the new corpus?
Corpus-choice note. My earlier approach proposed publicly available NYC transportation commentary. For this implementation I selected the documented, reproducible inaugural-address corpus to avoid reliance on an unstable external reviews source. This changes the proposed extension corpus but still satisfies the assignment’s requirement to analyze a different text corpus.
0.2 Setup and data preparation
Run this once in the RStudio Console if any packages are missing:
install.packages(c("tidyverse", "tidytext", "janeaustenr", "textdata", "quanteda", "syuzhet", "knitr"))One-time preparation for AFINN and NRC. Before your first render, run the following two commands in the interactive RStudio Console, not inside the QMD or a background render, and type 1 to accept the download terms when prompted:
textdata::lexicon_afinn()
textdata::lexicon_nrc()After these resources are cached, Quarto can access them non-interactively. The added Jockers–Rinker sentiment method is bundled with the syuzhet package and does not require downloading a separate textdata lexicon. If syuzhet is missing, install it using the package-installation command above.
1 Part I: Reproduction of Chapter 2
1.1 1. Tidy Jane Austen’s novels
Following Chapter 2, the janeaustenr lines are grouped by novel, assigned a line number and chapter number, and tokenized into a one-word-per-row tidy data frame. inner_join() will then connect the tokens to sentiment dictionaries.
tidy_austen <- janeaustenr::austen_books() |>
group_by(book) |>
mutate(
linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter\\s+[ivxlcdm]+\\b", ignore_case = TRUE)))
) |>
ungroup() |>
unnest_tokens(word, text)
kable(tidy_austen |> count(book, name = "total_tokens"),
caption = "Number of word tokens in each Jane Austen novel")| book | total_tokens |
|---|---|
| Sense & Sensibility | 119957 |
| Pride & Prejudice | 122204 |
| Mansfield Park | 160460 |
| Emma | 160996 |
| Northanger Abbey | 77780 |
| Persuasion | 83658 |
1.2 2. Bing positive/negative scores across narrative position
This reproduces the principal example of Chapter 2: count positive and negative words in blocks of 80 original text lines; define net sentiment as positive minus negative. A positive value indicates more Bing-positive word matches than negative word matches in that block.
bing <- get_sentiments("bing")
austen_bing <- tidy_austen |>
inner_join(bing, by = "word") |>
count(book, index = linenumber %/% 80, sentiment) |>
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) |>
mutate(net_sentiment = positive - negative)
kable(austen_bing |> group_by(book) |>
summarise(mean_net_per_80_lines = round(mean(net_sentiment), 2),
n_blocks = n(), .groups = "drop"),
caption = "Mean Bing net sentiment by novel, averaged across 80-line blocks")| book | mean_net_per_80_lines | n_blocks |
|---|---|---|
| Sense & Sensibility | 7.99 | 158 |
| Pride & Prejudice | 8.59 | 163 |
| Mansfield Park | 10.01 | 192 |
| Emma | 11.57 | 203 |
| Northanger Abbey | 7.33 | 99 |
| Persuasion | 12.11 | 105 |
ggplot(austen_bing, aes(x = index, y = net_sentiment, fill = book)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ book, ncol = 2, scales = "free_x") +
labs(x = "Text position (blocks of 80 lines)",
y = "Positive minus negative Bing matches")Interpretation. The plot shows changes in positive and negative vocabulary over the course of each narrative. It does not establish the author’s feelings, a character’s emotions, or a statistical estimate of readers’ experiences. Comparing raw block counts also depends on the amount of sentiment-bearing language in each block.
1.3 3. AFINN: intensity-weighted sentiment
AFINN assigns individual words integer scores, typically ranging from -5 (negative) to +5 (positive), rather than only positive or negative categories. As in the book, the following chart follows Emma in 80-line blocks.
austen_afinn_emma <- tidy_austen |>
filter(book == "Emma") |>
inner_join(get_sentiments("afinn"), by = "word") |>
group_by(index = linenumber %/% 80) |>
summarise(afinn_score = sum(value), .groups = "drop")
kable(head(austen_afinn_emma, 10), caption = "First ten AFINN blocks in Emma")| index | afinn_score |
|---|---|
| 0 | 37 |
| 1 | 24 |
| 2 | 19 |
| 3 | 69 |
| 4 | 72 |
| 5 | 29 |
| 6 | 43 |
| 7 | 51 |
| 8 | 55 |
| 9 | 63 |
ggplot(austen_afinn_emma, aes(index, afinn_score)) +
geom_col(fill = "steelblue") +
labs(x = "Text position (blocks of 80 lines)", y = "Summed AFINN score")1.4 4. NRC: emotions beyond positive/negative
NRC assigns words to categories such as joy, trust, sadness, fear, anger, and anticipation. One word may belong to multiple categories; consequently, the counts below are dictionary matches, not mutually exclusive emotion observations.
nrc_pandp <- tidy_austen |>
filter(book == "Pride & Prejudice") |>
inner_join(get_sentiments("nrc"), by = "word") |>
count(sentiment, sort = TRUE)
kable(nrc_pandp, caption = "NRC sentiment and emotion word matches in Pride & Prejudice")| sentiment | n |
|---|---|
| positive | 7273 |
| trust | 4177 |
| negative | 3630 |
| anticipation | 3463 |
| joy | 3203 |
| fear | 1767 |
| sadness | 1752 |
| surprise | 1535 |
| anger | 1295 |
| disgust | 969 |
ggplot(nrc_pandp, aes(x = reorder(sentiment, n), y = n)) +
geom_col(fill = "slateblue") +
coord_flip() +
labs(x = NULL, y = "Number of dictionary matches")1.5 5. Most frequent positive and negative words
The contribution of frequent sentiment words can help explain the scores. This is another core analysis in Chapter 2.
austen_top_words <- tidy_austen |>
inner_join(bing, by = "word") |>
count(word, sentiment, sort = TRUE) |>
group_by(sentiment) |>
slice_max(n, n = 12, with_ties = FALSE) |>
ungroup()
kable(austen_top_words, caption = "Frequent Bing positive and negative words across Austen's novels")| word | sentiment | n |
|---|---|---|
| miss | negative | 1855 |
| poor | negative | 424 |
| doubt | negative | 281 |
| object | negative | 233 |
| sorry | negative | 219 |
| impossible | negative | 215 |
| afraid | negative | 198 |
| bad | negative | 174 |
| scarcely | negative | 174 |
| anxious | negative | 165 |
| pain | negative | 151 |
| spite | negative | 143 |
| well | positive | 1523 |
| good | positive | 1380 |
| great | positive | 981 |
| like | positive | 725 |
| better | positive | 639 |
| enough | positive | 613 |
| happy | positive | 534 |
| love | positive | 495 |
| pleasure | positive | 462 |
| happiness | positive | 369 |
| right | positive | 329 |
| best | positive | 323 |
ggplot(austen_top_words, aes(x = reorder(word, n), y = n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ sentiment, scales = "free") +
coord_flip() +
labs(x = NULL, y = "Word count")2 Part II: Extension to a different corpus
2.1 6. Inaugural addresses: source and tokenization
The quanteda::data_corpus_inaugural object contains U.S. presidential inaugural addresses and metadata, including speech year and president. We analyze only documents with an available year. Each speech is a document; this is a different genre from literary fiction and therefore offers a meaningful comparison.
inaugural_corpus <- quanteda::data_corpus_inaugural
speech_texts <- as.character(inaugural_corpus)
speech_metadata <- as_tibble(quanteda::docvars(inaugural_corpus))
speeches <- tibble(
speech_id = seq_along(speech_texts),
document = names(speech_texts),
text = unname(speech_texts)
) |>
bind_cols(speech_metadata) |>
mutate(Year = as.integer(Year)) |>
filter(!is.na(Year))
speech_words <- speeches |>
select(speech_id, document, Year, President, text) |>
unnest_tokens(word, text)
speech_sizes <- speech_words |>
count(speech_id, document, Year, President, name = "total_tokens")
kable(speech_sizes |> arrange(desc(Year)) |> head(12),
caption = "Most recent 12 speeches represented in the installed corpus")| speech_id | document | Year | President | total_tokens |
|---|---|---|---|---|
| 60 | 2025-Trump | 2025 | Trump | 2918 |
| 59 | 2021-Biden | 2021 | Biden | 2377 |
| 58 | 2017-Trump | 2017 | Trump | 1446 |
| 57 | 2013-Obama | 2013 | Obama | 2106 |
| 56 | 2009-Obama | 2009 | Obama | 2399 |
| 55 | 2005-Bush | 2005 | Bush | 2073 |
| 54 | 2001-Bush | 2001 | Bush | 1585 |
| 53 | 1997-Clinton | 1997 | Clinton | 2158 |
| 52 | 1993-Clinton | 1993 | Clinton | 1600 |
| 51 | 1989-Bush | 1989 | Bush | 2318 |
| 50 | 1985-Reagan | 1985 | Reagan | 2571 |
| 49 | 1981-Reagan | 1981 | Reagan | 2436 |
2.2 7. Bing: net polarity per 1,000 words
Unlike the Austen analysis, inaugural addresses differ substantially in length. Therefore, the primary cross-speech comparison normalizes Bing net matches to 1,000 tokens. The score is not the percentage of positive statements; it is based on dictionary word matches.
speech_bing_counts <- speech_words |>
inner_join(bing, by = "word") |>
count(speech_id, sentiment) |>
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0)
speech_bing <- speech_sizes |>
left_join(speech_bing_counts, by = "speech_id") |>
mutate(
positive = replace_na(positive, 0L),
negative = replace_na(negative, 0L),
bing_net_per_1000 = 1000 * (positive - negative) / total_tokens,
period = if_else(Year < 1900, "Before 1900", "1900 onward")
)
kable(speech_bing |>
arrange(desc(bing_net_per_1000)) |>
select(Year, President, total_tokens, bing_net_per_1000) |>
head(10) |>
mutate(bing_net_per_1000 = round(bing_net_per_1000, 2)),
caption = "Top ten inaugural addresses by Bing net sentiment per 1,000 tokens")| Year | President | total_tokens | bing_net_per_1000 |
|---|---|---|---|
| 1989 | Bush | 2318 | 52.63 |
| 1849 | Taylor | 1091 | 51.33 |
| 1817 | Monroe | 3378 | 51.21 |
| 1949 | Truman | 2281 | 49.98 |
| 1977 | Carter | 1226 | 48.94 |
| 1945 | Roosevelt | 559 | 48.30 |
| 1829 | Jackson | 1128 | 46.99 |
| 1801 | Jefferson | 1730 | 46.24 |
| 1809 | Madison | 1177 | 44.18 |
| 1973 | Nixon | 1805 | 43.77 |
ggplot(speech_bing, aes(Year, bing_net_per_1000)) +
geom_hline(yintercept = 0, linetype = "dashed", color = "gray50") +
geom_point(alpha = 0.75, color = "steelblue", size = 2) +
geom_smooth(method = "loess", se = FALSE, color = "firebrick", linewidth = 0.7) +
labs(x = "Speech year", y = "Bing positive minus negative matches per 1,000 tokens")2.3 8. AFINN: intensity as an alternative measurement
We also sum AFINN scores and normalize per 1,000 tokens, making scores more comparable between different-length speeches. AFINN weights some words more strongly than others, so it need not rank speeches identically to Bing.
speech_afinn <- speech_words |>
inner_join(get_sentiments("afinn"), by = "word") |>
group_by(speech_id) |>
summarise(afinn_sum = sum(value), .groups = "drop")
speech_compare <- speech_bing |>
left_join(speech_afinn, by = "speech_id") |>
mutate(afinn_sum = replace_na(afinn_sum, 0),
afinn_per_1000 = 1000 * afinn_sum / total_tokens)
kable(speech_compare |>
select(Year, President, bing_net_per_1000, afinn_per_1000) |>
arrange(desc(Year)) |>
head(10) |>
mutate(across(ends_with("1000"), ~ round(.x, 2))),
caption = "Bing and AFINN score comparison (ten most recent speeches)")| Year | President | bing_net_per_1000 | afinn_per_1000 |
|---|---|---|---|
| 2025 | Trump | 36.33 | 87.05 |
| 2021 | Biden | 18.93 | 33.66 |
| 2017 | Trump | 37.34 | 83.68 |
| 2013 | Obama | 25.64 | 43.21 |
| 2009 | Obama | 17.92 | 26.68 |
| 2005 | Bush | 34.25 | 69.46 |
| 2001 | Bush | 38.49 | 63.09 |
| 1997 | Clinton | 35.22 | 73.68 |
| 1993 | Clinton | 25.62 | 41.88 |
| 1989 | Bush | 52.63 | 102.67 |
ggplot(speech_compare, aes(bing_net_per_1000, afinn_per_1000)) +
geom_point(alpha = 0.7, color = "darkcyan") +
geom_smooth(method = "lm", se = FALSE, color = "gray30") +
labs(x = "Bing net matches per 1,000 tokens", y = "AFINN weighted score per 1,000 tokens")2.4 9. Additional lexicon: Jockers–Rinker (syuzhet)
This is the additional lexicon used for the extension. The syuzhet package provides the Jockers–Rinker sentiment dictionary (method = "syuzhet"), a weighted sentiment approach different from Bing and AFINN. Because the package ships with this lexicon, no further first-run textdata agreement is needed. We score each complete speech and normalize by its total token count. Note that the numerical scales of the three methods are not directly interchangeable.
speech_jockers <- speeches |>
transmute(speech_id,
jockers_sum = syuzhet::get_sentiment(text, method = "syuzhet"))
speech_compare <- speech_compare |>
left_join(speech_jockers, by = "speech_id") |>
mutate(jockers_per_1000 = 1000 * jockers_sum / total_tokens)
comparison_table <- speech_compare |>
arrange(desc(Year)) |>
select(Year, President, bing_net_per_1000, afinn_per_1000,
jockers_per_1000) |>
mutate(across(ends_with("1000"), ~ round(.x, 2)))
kable(head(comparison_table, 15),
caption = "Bing, AFINN, and Jockers–Rinker scores per 1,000 speech tokens")| Year | President | bing_net_per_1000 | afinn_per_1000 | jockers_per_1000 |
|---|---|---|---|---|
| 2025 | Trump | 36.33 | 87.05 | 11.48 |
| 2021 | Biden | 18.93 | 33.66 | 8.83 |
| 2017 | Trump | 37.34 | 83.68 | 28.67 |
| 2013 | Obama | 25.64 | 43.21 | 15.65 |
| 2009 | Obama | 17.92 | 26.68 | 15.49 |
| 2005 | Bush | 34.25 | 69.46 | 16.71 |
| 2001 | Bush | 38.49 | 63.09 | 21.29 |
| 1997 | Clinton | 35.22 | 73.68 | 24.40 |
| 1993 | Clinton | 25.62 | 41.88 | 17.31 |
| 1989 | Bush | 52.63 | 102.67 | 25.80 |
| 1985 | Reagan | 33.84 | 70.40 | 22.48 |
| 1981 | Reagan | 19.29 | 57.06 | 20.09 |
| 1977 | Carter | 48.94 | 82.38 | 37.07 |
| 1973 | Nixon | 43.77 | 97.51 | 20.80 |
| 1969 | Nixon | 30.13 | 67.33 | 16.41 |
speech_compare |>
select(Year, speech_id, bing_net_per_1000, afinn_per_1000,
jockers_per_1000) |>
pivot_longer(cols = ends_with("1000"), names_to = "lexicon", values_to = "score") |>
mutate(lexicon = recode(lexicon,
bing_net_per_1000 = "Bing (net matches)",
afinn_per_1000 = "AFINN (weighted sum)",
jockers_per_1000 = "Jockers–Rinker (weighted sum)")) |>
ggplot(aes(Year, score)) +
geom_point(alpha = 0.5, color = "steelblue") +
geom_smooth(method = "loess", se = FALSE, color = "firebrick") +
facet_wrap(~ lexicon, ncol = 1, scales = "free_y") +
labs(x = "Year", y = "Sentiment score per 1,000 tokens")2.5 10. How do the lexicons differ?
Bing counts positive and negative word matches. AFINN and Jockers–Rinker use weighted word scores, but they use different lexicons and scoring rules. Therefore, rankings are more informative than comparing the raw score magnitudes. We examine how closely the three methods rank the same speeches.
rank_compare <- speech_compare |>
mutate(bing_rank = rank(-bing_net_per_1000, ties.method = "average"),
afinn_rank = rank(-afinn_per_1000, ties.method = "average"),
jockers_rank = rank(-jockers_per_1000, ties.method = "average"))
rank_correlations <- tibble(
comparison = c("Bing vs AFINN", "Bing vs Jockers–Rinker",
"AFINN vs Jockers–Rinker"),
spearman_rho = c(
cor(rank_compare$bing_rank, rank_compare$afinn_rank,
method = "spearman"),
cor(rank_compare$bing_rank, rank_compare$jockers_rank,
method = "spearman"),
cor(rank_compare$afinn_rank, rank_compare$jockers_rank,
method = "spearman")
)
) |>
mutate(spearman_rho = round(spearman_rho, 3))
kable(rank_correlations,
caption = "Spearman rank correlations between sentiment methods")| comparison | spearman_rho |
|---|---|
| Bing vs AFINN | 0.837 |
| Bing vs Jockers–Rinker | 0.732 |
| AFINN vs Jockers–Rinker | 0.639 |
The results quantify how much the three sentiment methods agree about speech order. A higher rank correlation indicates more similar relative rankings, not that any particular method is more accurate. Differences arise from vocabulary coverage, weighting, and handling of context; none of these word-based methods independently understands negation or irony.
3 Part III: Interpretation, original-vs-extension comparison, and limitations
3.1 11. Original example vs. new corpus
| Dimension | Reproduction: Jane Austen | Extension: inaugural addresses |
|---|---|---|
| Text type | Nineteenth-century literary fiction | U.S. political speeches |
| Unit of analysis | 80-line narrative block | Whole speech |
| Bing output | Positive minus negative word matches per block | Net matches per 1,000 tokens |
| AFINN output | Weighted sentiment score per block | Weighted score per 1,000 tokens |
| NRC | Emotions in Pride & Prejudice | Not necessary for additional-lexicon requirement |
| Additional lexicon | None in the reproduced core workflow | Jockers–Rinker via syuzhet |
| Key interpretive caveat | Dialogue, irony, and plot context | Political rhetoric, historical context, and speech length |
The Austen results focus on within-book narrative changes; the inaugural-speech analysis focuses on between-document comparisons across time. This is an intentional design change: speeches vary in length, so normalized scores are more suitable than raw counts. The lexical approach is consistent across both corpora, but results are not directly interchangeable across genres or units.
3.2 12. Limitations and responsible interpretation
- Context and negation: Dictionary matching typically treats a word the same way in phrases such as “happy” and “not happy.” Sarcasm, irony, and negation can change actual meaning.
- Word sense: A word may have different connotations in literature, politics, and finance. Lexicons differ in their word associations and may not reliably measure political approval.
- Different units and historical vocabulary: Jane Austen 80-line blocks and entire inaugural addresses are not equivalent observations. Speech composition and vocabulary also changed across centuries.
- Lexicon coverage: Unmatched words contribute nothing to scores. Dictionaries may differ in size and the degree to which they recognize historical words.
- No causal claims: Historical trends in word polarity do not show whether presidents were successful, optimistic as people, or responding to specific events. Counts are descriptive only.
- Corpus version: The set of addresses depends on the installed
quantedaversion; the code prints the speech-year coverage and current included documents at render time.
3.3 13. Conclusion
This report reproduces the central Chapter 2 sentiment-mining technique with the Jane Austen corpus, including tidy tokenization, dictionary joins, Bing narrative trends, AFINN intensity, NRC emotions, and sentiment word frequencies. It then applies the same tidy principles to an independently chosen corpus of presidential inaugural addresses. By adding the Jockers–Rinker lexicon, normalizing scores per 1,000 tokens, and examining ranking correlations, the extension demonstrates why sentiment findings are sensitive to both corpus choice and lexicon definitions. Sentiment scores are a useful exploratory tool, but should always be interpreted with genre, context, and measurement differences in mind.
3.4 References and source code
- Silge, Julia, and David Robinson. 2017. Text Mining with R: A Tidy Approach. O’Reilly Media. Chapter 2, “Sentiment analysis with tidy data.” Online chapter and primary example code: https://www.tidytextmining.com/sentiment
- Silge, Julia, and David Robinson.
tidytextpackage,get_sentiments()reference: https://juliasilge.github.io/tidytext/reference/get_sentiments.html - Benoit, Kenneth, et al.
quantedapackage and inaugural-address corpus documentation: https://rdrr.io/cran/quanteda/man/data_corpus_inaugural.html - Jockers, Matthew L. syuzhet: Extract Sentiment and Plot Arcs from Text. R package documentation: https://cran.r-project.org/package=syuzhet
- Mohammad, Saif M., and Peter D. Turney. 2013. “Crowdsourcing a Word–Emotion Association Lexicon.” Computational Intelligence 29(3): 436–465. https://doi.org/10.1111/j.1467-8640.2012.00460.x
3.5 Reproducibility information
cat("Corpus covers years:", min(speeches$Year), "through", max(speeches$Year), "\n")Corpus covers years: 1789 through 2025
cat("Number of inaugural speeches:", nrow(speeches), "\n")Number of inaugural speeches: 60
cat("Number of inaugural-speech word tokens:", nrow(speech_words), "\n")Number of inaugural-speech word tokens: 140857
cat("R version:", R.version.string, "\n")R version: R version 4.6.1 (2026-06-24)
cat("tidytext version:", as.character(packageVersion("tidytext")), "\n")tidytext version: 0.4.3
cat("quanteda version:", as.character(packageVersion("quanteda")), "\n")quanteda version: 4.5.1
cat("syuzhet version:", as.character(packageVersion("syuzhet")), "\n")syuzhet version: 1.0.7