Sentiment Analysis with Text Mining in R: Reproduction and Extension

Author

Muhammad Imran

Published

November 8, 2026

1 Introduction

Sentiment analysis (or opinion mining) allows data scientists to programmatically evaluate the emotional content of written text. By converting unigrams (single words) into tidy data frames, I can use simple relational operations, such as inner_join(), to measure sentiment trajectories and word distributions across literary works or modern corpora.

This report is divided into two primary sections:

1.1 Step 1: Reproduce the Base Example:

Replicating the core sentiment analysis workflows from Chapter 2 of Text Mining with R by Julia Silge and David Robinson, using Jane Austen’s novels and the bing, nrc, and afinn lexicons.

1.2 Step 2: Extend the Analysis:

Extending the methodology to a new corpus, specifically, classic science fiction novels by H.G. Wells obtained via gutenbergr and incorporating a fourth sentiment dictionary, the domain-tailored Loughran-McDonald lexicon (loughran). I compare narrative sentiment arcs, lexicon performance, and structural differences in emotion classification.

Step 1: Reproducing the Base Example

Original Reference:

Silge, J., & Robinson, D. (2017). Text Mining with R: A Tidy Approach. O’Reilly Media.

Chapter 2: “Sentiment analysis with tidy data”.

Online resource is below: https://www.tidytextmining.com/sentiment.html

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

1.1 Tokenizing Jane Austen’s Novels

In this step, I would tokenize Jane Austen’s six completed novels into a tidy dataset where each row corresponds to one word, keeping track of the line number and chapter structure. The sample of 6 rows is as under:

# A tibble: 6 × 4
  book                linenumber chapter word       
  <fct>                    <int>   <int> <chr>      
1 Sense & Sensibility          1       0 sense      
2 Sense & Sensibility          1       0 and        
3 Sense & Sensibility          1       0 sensibility
4 Sense & Sensibility          3       0 by         
5 Sense & Sensibility          3       0 jane       
6 Sense & Sensibility          3       0 austen     

1.2 Exploring NRC Joy Words in Emma

Using an inner_join() with the NRC lexicon filtered for “joy”, I wil find the most frequent joyful words in Austen’s Emma. The sample table showing the 10 rows is as under.

# A tibble: 10 × 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

1.3 Plotting Narrative Sentiment Trajectories across Austen’s Novels

Using the Bing et al. lexicon, I would divide each novel into 80-line chunks, calculate positive and negative word counts, and plot the net sentiment ($\text{sentiment} = \text{positive} - \text{negative}$) over narrative time.

1.4 Comparing Three Lexicons on Pride and Prejudice

Thereafter, I compare how AFINN, Bing, and NRC score narrative trajectory in Pride and Prejudice.

1.5 Most Common Positive and Negative Words

Identifying which unigrams contribute most to Bing sentiment classification highlights potential domain mismatch (e.g., “miss” used as a formal honorific rather than a negative verb).

Step 2: Extending the Analysis

To extend the textbook example, I apply sentiment mining to a different literary genre and incorporate a fourth sentiment lexicon:

New Corpus (Simple Literary Genre): Grimm’s Household Tales (Fairy Tales) by Jacob and Wilhelm Grimm, downloaded via gutenbergr (Gutenberg ID: 2591). Fairy tales have simple, recognizable emotional trajectories (e.g., hardship leading to a happy ending).

Binary Lexicon Comparison: I will focus exclusively on binary sentiment classification (positive vs. negative). We construct a dedicated NRC Binary Lexicon (filtering NRC to only positive and negative entries) and compare its performance directly against the Bing Lexicon across the fairy tales.

2.1 Corpus Acquisition & Tidy Preparation

I download four classic works by H.G. Wells from Project Gutenberg as follows:

# A tibble: 4 × 2
  title                                                          n
  <chr>                                                      <int>
1 Court Life in China: The Capital, Its Officials and People 72689
2 The war of the worlds                                      60632
3 The island of Doctor Moreau                                44249
4 The Time Machine                                           32762

2.2 Integrating the Fourth Lexicon: Loughran-McDonald

The Loughran-McDonald dictionary classifies words into financial and operational sentiments: positive, negative, litigious, uncertainty, constraining, and superfluous. I filter for positive and negative tokens to allow direct comparison with Bing and NRC binary scores. which are as follows:


negative positive 
    2355      354 

2.3 Comparative Sentiment Trajectories across 4 Lexicons

Thereafter, I compute sentiment trajectories for H.G. Wells’ The War of the Worlds using AFINN, Bing, NRC, and Loughran-McDonald.

Total rows in war_of_worlds: 60632 
Total rows in AFINN trajectory: 80 
Total rows in Binary trajectories: 240 
Total rows in Final Merged Data: 320 

2.4 Word Clouds and Word Contribution Comparison

To pinpoint which terms drive positive and negative scoring in The War of the Worlds, I construct a sentiment word cloud and top-word contribution plot.

2.5 Multidimensional Emotion Breakdown Across Wells’ Novels

Instead of restricting our study to positive vs. negative binary metrics, I measure eight specific emotion categories from the NRC lexicon across all four H.G. Wells works.

2.6 Contextual Sentiment Analysis: Identifying Negation Words

A core limitation of unigram sentiment analysis is negation (e.g., “not good” is classified as positive because of “good”). Here I extract bigrams from The War of the Worlds to quantify how frequently negation words reverse unigram sentiments.

1.3 Conclusion

This report successfully reproduced core sentiment analysis workflows from Silge and Robinson (2017) using Jane Austen’s novels and extended the methodology to classic science fiction by H.G. Wells.

  • Lexicon Sensitivity & Mismatch: Unigram-based dictionary approaches (Bing, AFINN, NRC, Loughran-McDonald) reveal stark differences in net sentiment trajectory. Lexicons designed for general or financial domains struggle with genre-specific vocabulary. For instance, formal honorifics like "miss" in 19th-century literature miscategorize positive/neutral social interactions as negative, while non-emotional descriptive words like "like" falsely boost positive scores in sci-fi narratives.

  • Multidimensional Profiles: Restricting sentiment analysis to binary metrics (positive vs. negative) masks deeper narrative dynamics. Evaluating multidimensional NRC emotion categories demonstrated that The War of the Worlds is predominantly driven by fear, trust, sadness, and anticipation.

  • Contextual Breakdown: Bigram analysis confirms that unigram models frequently miscalculate sentiment due to negation modifiers (e.g., "not good", "no doubt"), demonstrating the structural vulnerability of simple word-matching methods.

1.4 Limitations

  • Unigram Negation & Context Loss: Single-word tokenization fails to account for valence shifters, contrastive conjunctions (e.g., "but"), sarcasm, or ironies. Negation analysis reveals substantial score distortions where positive terms preceded by "not" or "no" artificially inflate positivity.

  • Domain Mismatch & Out-of-Vocabulary Words: General-purpose lexicons (Bing/NRC) do not account for historical language shifts or genre-specific terminology. Similarly, domain-tailored dictionaries like Loughran-McDonald (built for financial/operational contexts) yield low coverage when applied to literary fiction.

  • Arbitrary Chunking Windows: Dividing narratives into fixed 80-line sections can obscure precise plot climaxes or artificially average out sudden emotional shifts that occur across chapter breaks.

  • Equal Weighting Fallacy: Treating all tokens with equal weight ignores differences in semantic intensity across different parts of speech or story positions.

1.5 Recommendations

  1. Implement Context-Aware Preprocessing: Before matching tokens against dictionaries, apply custom stop-word lists tailored to period-specific literature (e.g., reclassifying "miss" as a neutral honorific rather than a negative verb).

  2. Shift to N-gram and Sentence-Level Analysis: Move beyond unigrams to bigrams, trigrams, or dependency parsing to explicitly capture negations ("not bad"), intensifiers ("very happy"), and structural shifts.

  3. Utilize Multidimensional Emotion Frameworks: Prefer multi-class emotion lexicons (such as the 8-category NRC model) over simple binary (positive/negative) metrics when analyzing narrative arcs to capture nuances like anxiety, fear, and anticipation.

  4. Calibrate Chunking Sizes: Dynamically adjust section sizes based on natural structural boundaries (e.g., by chapter or paragraph) rather than arbitrary fixed line counts to obtain cleaner narrative arcs.

1.6 Way Forward

  • VADER & Valence Shifter Integration: Incorporate rule-based algorithms like VADER or R packages like sentimentr that automatically account for valence shifters, punctuation intensity, and sentence structure.

  • Transformer-Based Embeddings: Extend the analysis beyond dictionary lookup methods by implementing transformer models (e.g., BERT, RoBERTa, or fine-tuned LLMs) to capture deep contextual sentiment and subtle nuance in classical literature.

  • Aspect-Based Sentiment Mining: Apply topic modeling (LDA) prior to sentiment classification to evaluate sentiment towards specific characters, settings, or themes (e.g., sentiment toward the Martians vs. human survivors in The War of the Worlds).

  • Cross-Genre Benchmark Dataset: Develop a standardized, domain-specific sentiment lexicon for 19th-century classical literature and early science fiction to mitigate domain bias.