Heroes & villains of Treasure Island
Can a program read a novel and work out who the heroes and villains are? An end-to-end NLP pipeline that tries.
- Where
- University of Adelaide, Master of Data Science
- When
- January to September 2025
- Data
- Treasure Island, from Project Gutenberg
- Tools
- Python, spaCy, regex, sentiment analysis, KDE
The question
A character in a novel appears under many names, and their role is never stated outright. The aim was to classify every character in Treasure Island as Hero, Villain, Morally Complex or Support/Neutral, using only the text.
The pipeline
Structure the novel
Cleaned the Project Gutenberg text and split it into 34 chapters with regex chapter detection, then into sentences with spaCy sentence tokenisation.
Find the characters
Extracted character mentions with spaCy named-entity recognition, then used alias mapping to collapse 149 raw mentions into 22 canonical characters, with manual role-based mapping such as "doctor" → Dr. Livesey.
Point the sentiment at the right person
Added quotation attribution and dependency-based sentiment targeting so sentiment lands on the correct character, falling back to sentence-level sentiment when needed.
Measure each character
Produced sentiment summary statistics, chapter-by-chapter sentiment arcs and a volatility score for every character.
Infer the role
Placed characters on a 2D map of sentiment against volatility and set the role boundaries with a KDE-based threshold.
What came out of it
- Visualisations of each character's sentiment arc across the book.
- A reproducible, long-form character-by-sentence dataset that supports interpretation and explainability.
Where this sits in the story
- Master of Data Science · Learn · 2023–25