Week 9: Sentiment Analysis

Author

Jocelyn Slater

Published

November 8, 2026

Introduction

This assignment explores sentiment analysis through two lenses. The first is a straightforward example, taken directly from Chapter 2 of Text Mining with R, where the authors introduce sentiment analysis.

In the second part of the assignment, I elected to extend the analyis to one of my favorite books and a seasonal re-read, Mary Shelley’s Frankenstein. With the help of Claude, I compared the sentiment analysis of the novel using both the AFINN and NRC lexicons. I aligned the sentiment inflection points with the plot points I am familiar with. I also compared the sentiment based on the chapter narrator for the two lexicons.

Chapter 2 Example

First to build familiarity with sentiment analysis, I recreate the example covered in Chapter 2 of Text Mining with R. These are the code experts presented in the chapter, organized into code blocks.

# 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

# A tibble: 2 × 2
  sentiment     n
  <chr>     <int>
1 negative   3316
2 positive   2308

# A tibble: 6 × 2
  book                chapters
  <fct>                  <int>
1 Sense & Sensibility       51
2 Pride & Prejudice         62
3 Mansfield Park            49
4 Emma                      56
5 Northanger Abbey          32
6 Persuasion                25
# A tibble: 6 × 5
  book                chapter negativewords words  ratio
  <fct>                 <int>         <int> <int>  <dbl>
1 Sense & Sensibility      43           161  3405 0.0473
2 Pride & Prejudice        34           111  2104 0.0528
3 Mansfield Park           46           173  3685 0.0469
4 Emma                     15           151  3340 0.0452
5 Northanger Abbey         21           149  2982 0.0500
6 Persuasion                4            62  1807 0.0343

Sentiment Analysis in Frankenstein

To extend the sentiment analysis, I chose to apply the same process to one of my favorite novels, Mary Shelly’s Frankenstein. I used Claude to do the initial sentiment comparison between the AFINN and NRC dictionaries (see AI Disclaimer). AFINN uses a 5 point scoring system on approximately 2500 words while NRC uses a binary yes/no with a word count of approximately 6500.

A very short summary of Frankenstein

My analysis will be more meaningful if you are familiar with the novel, which is very different from the popular modern retellings of Frankenstein.

The story is framed as Robert Walton writes letters to his sister describing the tale that a man rescued from the arctic relayed to him. That man was the scientist Victor Frankenstein. Victor tells of his happy childhood with his family and friend Elizabeth. He then describes how he built and animated a Creature from dead body parts. Horrified by his creation, he abandoned it. The Creature is intelligent. The Creature learned from the DeLacey family as they tutored a foreign visitor/family member, Safie. He befriended the blind father only to be rejected by the rest of the family upon discovery. Shunned by society and desperate for affection, it turned vengeful after Victor refused to create a companion. Victor’s adopted sister Justine is convicted and executed for the murder of their brother, William - a murder Victor is sure that the Creature committed. The Creature murders Victor’s best friend Henry Clerval and his bride Elizabeth on their wedding night. Consumed by guilt and grief, Victor chases the Creature into the arctic on dog sled where he meets with Walton’s ship. Victor dies shortly after telling his story to Walton. The Creature mourns Victor and sets out into the arctic alone, intending to die there.

Method

Topic What I did
Text Project Gutenberg #84 via gutenbergr: 75,143 words in 4 letters and 24 chapters. (This is the revised 1831 text)
Scoring AFINN adds each word’s score from -5 to +5. NRC counts positive words as +1 and negative words as -1. Both are reported as net sentiment per 1,000 words.
Excluded words Character names and labels (victor, frankenstein, creature, monster, fiend, dæmon, demon, devil, wretch) are not scored.
Sentiment curve I used a Gaussian smoother with an SD of 750 words, which spans roughly one chapter.
Turning/inflection points Peaks and troughs on the curve. I use these for “inflection points”, in the story and add extra plot context to frame the sentiment changes.
Character focus Walton (letters and closing), Victor (Ch 1-10 and Ch 17-24), and the Creature (Ch 11-16). These are very rough distinctions, as there is some nesting of narrators.

The local extrema in the smoothed sentiment curve, where sentiment shifts between positive to negative, are called “wiggles”. However, to determine if these wiggles are true inflection points and not just due to random chance, the code first performs a shuffle. We take the paragraphs in the book and shuffle them so that the appear in a random order. From this shuffled version, we record the maxima and minima. This process is repeated 200 times and results are recorded. In the final, ordered version, if a wiggle is not distinct from the shuffled statistical maxima and minima 95% of the time, then the wiggle is not a true inflection point and is instead due to random fluctuation of happy and sad paragraphs. The wiggles defined as inflection points are statistically significant turning points in the sentiment.

Results - Sentiment over time

AFINN

NRC

AFINN and NRC Overlayed

Lexicon Comparison

Shape: the smoothed curves correlate at r = 0.91. The chapter rank correlation is 0.90.

Turning points: several shared inflection points (Chp 1/2, Chp 8, Chp 12/13, Chp 19, Chp 20)

When the inflection points and chapter sentiments are plotted against each other for AFINN and NRC, we can see a strong agreement for most of the story. Overall, the two dictionaries score the narrators and chapter sentiment very similarly.

Lexicon Contrast

Although the two dictionaries share most inflection points and general shapes, the shared beats are pinpointed in slightly different locations, sometimes differing by entire chapters. The NRC’s inflection points also weight to the beginning of the story more heavily (identifying the creature’s creation and Elizabeth’s letter) while AFINN favors the end of the book (identifying Victor’s return home and Victor’s death).

Category AFINN NRC
Scale -5 to +5 +1 / -1, plus 8 emotions
Number of words scored 891 1,648
Share of all words scored 7% 10%

Changing tone with narrator

Narrator sentiment largely follows the same trend between the two dictionaries. Major differences arise in Victor’s initial chapters, his final chapters, and Walton’s closing letters where NRC interprets a more positive sentiment then AFINN. The relative position of sentiment for the creature’s chapters is similar between the two dictionaries. The scales for the two dictionaries are difference because the lexicon scores sentiment differently.

NRC emotions

The NRC scores on specific emotions, not just a general negative/positive sentiment. This allows us to look at the emotion score per 1000 words for Victor and the Creature. Victor’s narration leans toward the negative emotions and the Creature’s toward joy. Victor is at the end of his life telling his story to Walton with the framing of a failed, haunting experiment, so it follows that his emotions would be more negative. Although the Creature is reviled and longs for companionship, he also delights in learning about the world. His initial childlike wonder and attempts to find community mean his emotions are much more positive overall.

Overall take aways

The two lexicons broadly tell the same story. Their chapter scores correlate at r = 0.89, and they agree on whether a section is net positive or negative in 27/28 sections. 6 of the 10 distinct inflection points appear in both, never more than 600 words apart.

They differ most in baseline sentiment. NRC reads the book as mildly positive (+8.7 per 1,000 words) and AFINN as neutral (+0.2). On the words both lexicons score, the gap disappears once AFINN’s intensity weights are set aside (-3.8 vs -3.7). NRC’s positive tilt seems to come from words only it scores.

The Creature’s narration scores brighter than Victor’s, but that result is fragile. Victor minus Creature is -24.8 (95% interval -39.4 to -9.7) with AFINN and -8.6 (95% interval -16.8 to -0.1) with NRC, where the interval sits right at zero. With the De Lacey family chapters removed, neither interval excludes zero and therefore his narration is more neutral. Much of the Creature’s positive language describes the family he watches, not his own experience.

Victor’s language around the Creature contributes signifigantly to his negative sentiment rating. For most of the novel, Victor calls the Creature a monster, fiend or dæmon. These sentences score -50.1 (AFINN) and -22.6 (NRC), against -4.2 and +7.3 for his other sentences.

Limitations

Attempting to quantitatively analyze something that is inherently qualitative means that the list of limitations to this effort is significant. Some of the limitations are as follows:

  • Word counting misses context in a text. Lexicons can’t see irony, the speaker’s stance, or most negation. In the sentence “I am not ever happy”, many lexicons will see “happy” and score the sentence positively. the About 4% of AFINN’s and 3% of NRC’s scored words come within two words after a negator such as “not” or “never”, and these are not reversed.
  • The lexicons are modern and the novel isn’t. The dated vernacular used in the novel means that intention behind some of the words and phrases is not caputured by the dictionaries sentiment score.
  • The narrators are nested, making it difficult to track the narrator. The Creature’s story is told by Victor, who is then recorded by Walton. “The Creature’s narration” means the passages in his voice, and quoted speech inside other narrators’ sections stays with those narrators unless noted.
  • The turning-point/inflection point threshold developed by comparing to statistical wiggling is a screening rule rather than a formal test.
  • The “beat boundaries” are my reading of the plot which is naturally going to be subjective and based on my very fallible memory and interpretation of the novel.
  • This analysis is based on the 1818 version of the novel which is Shelley’s original work published anonymously in 3 volumes. She later published an edited version in one book in 1831 which is considered darker tonally.

Future Work

If I were to continue with this analysis, I would be interested in exploring:

  • Comparing the results of the 1818 with 1831 version of the novel.
  • Address the issues around the sentiment of negation.
  • Develop handling for old fashioned words and phrases.
  • Tag the narrators more rigorously to mitigate the nested narrator problem.
  • Use a different binary dictionary like Bing to compare with AFINN more directly.
  • Use a model that can read context like Transformers in Python.

AI Disclaimer

I created this report using the results from Anthropic’s Claude Opus 5.5, via Claude Code. This was my original prompt:

I would like to perform some sentiment analysis on Frankenstein using the gutenberg package. I would like this in R. This should be done with two different lexicons AFINN and NRC. Then results should be compared in two plots with inflection points labeled. At the very least, I would like two plots (one for each lexicon). Thank you.

After my initial prompt, I asked for help placing the plot points in the right place on the plots.

Thanks. Now I need help placing my plot points on the seniment graphs. Those are located in plot_beats.csv

Finally I edited this report, eliminating some of the literary analysis that Claude included. I feel comfortable using AI for code development, but not for literary analysis, as that is still something I think requires a human touch.

I have edited my report document, Week10A_CodeBase.qmd, removing some plots and analysis you originally generated. Please edit frankenstein_sentiment.R to remove the associated code so that we are only generating what is used in the final report. Thank you.

Sources

Shelley, Mary Wollstonecraft. (1818) 1993. Frankenstein; or, The Modern Prometheus. Project Gutenberg. https://www.gutenberg.org/ebooks/84.

Silge, Julia, and David Robinson. 2017. “Sentiment Analysis with Tidy Data.” In Text Mining with R: A Tidy Approach. Sebastopol, CA: O’Reilly Media. https://www.tidytextmining.com/sentiment.