September 19, 2022
Fiction Sentiment Analysis: Examples and Methods
The term “sentiment analysis” in the contexts of texts refers to a program (to put it simply) that detects emotions in a text. A typical application is for detecting whether clients are happy or angry, but large-scale analysis – for instance, analyzing tweets – can also give a sense of public opinion on a matter. I’m not interested in any of that. Instead, I wanted to experiment with fiction sentiment analysis.
What kind of applications could it have? How difficult is it to implement?
That’s what we’ll be looking at in this post. Though I’ll offer you details and links along the way, I’ll also keep it accessible to a lay audience. Whether you’re a beginner/intermediate programmer looking for ideas, or simply someone interested in seeing the possibilities, there’s something here that will interest you.
Sneak preview: I’ll also share with you a link to my program, so that you can perform a fiction sentiment analysis on your novel or short story!
Fiction Sentiment Analysis: the Basics
We’ll see more details about the “how” in “Methods”, below, but in a nutshell, fiction sentiment analysis is fairly simple.
Basically, a program analyzing sentiment does it by comparing the input text to a reference, then returning a score. This score determines whether a text is negative or positive, whether it’s toxic or aggressive, and so on. It depends on the specific program.
Of course, the comparison as well as the reference determine the accuracy of the whole process.
But before I show you some methods I tried for my fiction sentiment analysis programs, let’s first see some possible applications. I came up with three, and, obviously enough, imagination and creativity are the limit here.
Fiction Sentiment Analysis: Examples
I came up with three possible applications for my fiction sentiment analysis project. I named them:
- Tell Me, Reader
- Sentifiction
- Fiction Sentiment Analysis (I reserved the most unimaginative title for the most mundane application)
Let’s briefly see them one by one.
Tell Me, Reader
If the title rings a bell, you probably remember my short story collection, Tell Me, Mariner. Indeed, it uses several of the stories in that volume.
The program offers an interactive fiction experience – not in the form of a game, such as Mansion Escape, neither in the form of those stories where the plot takes a divergent path, leading to a different conclusion.
Rather, the program is an example of user-modified text: Along the way, questions posed by characters – in particular, the Mariner – are offered to the reader. Depending on the reader’s response, the subsequent text is modified. For instance, negative responses mean that the next n paragraphs will feature a more negative vocabulary.
Of course, the options are multiple. In principle it’s possible to offer divergent plots, as I mentioned earlier; it’s just that I’m not interested in such a project.
Sentifiction
This is a much more straightforward application. It’s basically a reading interface – you read through the story, without interacting with it. However, what the sentiment analysis code does is to analyze each page (each excerpt it shows the reader) before displaying it. Then, it adds color to the background in hues according to the emotions of the excerpt.
Is the excerpt negative? The page becomes red. Is it calm? It becomes blue. And so on… It’s an interesting example of combining “facets” of art, offering a more holistic experience. Of course there are many opportunities for reader manipulation, too. A page could display the color of the next excerpt, thus offering a certain foreshadowing experience to the reader.
Fiction Sentiment Analysis
This is the most boring title, and in a sense the most boring example. Ironically enough, it might also be the most useful.
As the name implies, Fiction Sentiment Analysis returns an analysis for the sentiments present in an entire narrative, offering a display of the progression throughout the text. Any interpretations are of course the responsibility of the author.
A narrative that begins with negativity and ends up being very positive might be just what you needed; or then not. The image above shows the analysis for “The Dunwich Horror” – seeing it becoming positive toward the end was a bit surprising, to say the least.
Fiction Sentiment Analysis: Methods
So, how can we actually implement such a sentiment analysis? I tried three different ways. I’ll list them all – together with pros and cons – and then I’ll share with you which was my favorite.
TensorFlow.JS Toxicity Classifier
This pre-trained AI model detects toxicity and classifies it according to several categories – see more on the relevant TensorFlow page. Of course, one can also dedicate the time and effort to train their own custom model, too.
- + As an AI model, it’s far more sensitive to actual semantics rather than word-for-word interpretation.
- + Very easy to implement.
- – Though it’s pre-trained, it’s still slow.
- – Toxicity isn’t quite the same as negativity.
Sentiment Analysis APIs
There are plenty of sentiment analysis APIs out there, but most of them are paid. More still, most of them seem to be tuned for nonfiction and short texts. Nonetheless, there are some free options worth exploring, such as Meaningcloud.
- + Even easier to implement.
- + Smart, efficient, great at detecting sentiment.
- – Free models are very limited in terms of queries.
- – Unknown suitability for fiction sentiment analysis.
Dictionary-Based Fiction Sentiment Analysis
This is as simple as it gets. The input text (that is, the narrative) is compared against a word list. In its simplest iteration, the dictionary returns a score (e.g. 0 for negative, 5 for neutral, 10 for positive), but it can also contain other markers – imagine an array of objects in the style of:
var wordlist = [{"word":"failure", "score":2, "aggression":false}, {"word":"idiot", "score":3, "aggression":true}, …];
You get the idea. You could customize such a dictionary according to your needs.
- + Super-fast, it takes a few seconds to parse an entire narrative.
- + It can be flexible and efficient.
- – Not as intelligent on a semantic level (sentences such as “Oh, look, we have a genius among us” wouldn’t be detected as ironic/negative), unless…
- – … plenty of work required to set up the dictionary according to one’s wishes/needs. Not to mention, setting up a dictionary from scratch is a very toilsome process.
So, which one did I pick? Was it worth it?
Is It Worth It?
In the end, I opted for speed and simplicity, and chose a dictionary-based sentiment analysis. You really can’t beat the simplicity and speed, and the efficiency is perfectly fine. The more time one dedicates at fine-tuning such a dictionary, the better the results.
Moreover, let’s remember something important about fiction sentiment analysis: It’s predicated on a vastly larger amount of text. In other words, whereas analyzing e.g. a tweet gives you only a couple of sentences to work with, a 100,000-word novel allows for far greater room for maneuvering.
Practically speaking, this means that we don’t need a super-precise tool, one deploying irony detectors or semantic analysis.
So, was this whole thing worth it?
Well, as a learning experience and a fun little experiment, I’d say most definitely! I didn’t have to dedicate more than a few hours for all these examples, so we’re not talking about any unreasonable time or effort investment.
Try a Fiction Sentiment Analysis for Your Text!
In the meantime, you can use my own simple program to analyze your narrative. You can try it following this link. For a full list of my available programs, see the relevant page on the main site.
Home for Fiction apps are Patreon-only content. To get access, become a patron of Home for Fiction, then follow the link to the app shared in this post.
You can also try the programs for free by joining Home for Fiction on Patreon (as a non-paying patron). You get 3 tries per month to use on any of the programs.