NaNoWriMo Prompts with Code

As the calendar nears its end, a familiar event rolls around — one filled with anticipation, joy and frustration. Come November, thousands of writers will sit at their computers, tap on their phones and scribble in their notebooks. It’s National Novel Writing Month.

National Novel Writing Month, shortened to NaNoWriMo, and shortened further still to NaNo, is a writing event held over the month of November. From the first to the thirtieth, participants aim to write a novel of 50,000 words, with daily wordcount goals. The tricky part, of course, is meeting those goals.

Writing prompts and timed challenges can help with conquering a blank page, as can brainstorming through research. In today’s post, we’re going to look at how to combine playful randomness to build a Mad Libs-esque set of prompts with the Wolfram Language. (Just don’t expect it to do the writing for you — what would be the fun in that?)

The idea behind this project is to find a writer we’d like to emulate — say, the expressive prose of Jane Austen — and have the words from their novel help us brainstorm what to write about. For example, Austen’s noun list might include “pride” and “prejudice.” This gives us a personalized corpus, while allowing for some motivating silliness.

Let’s start!

Laptop with a screen reading “Just Start”
Photo by Dayne Topkin on Unsplash

Importing Our Text and Splitting It into Words

We’ll start by importing a novel from our source author. For some texts, you can import with the ResourceData function, but in this case, we’ll use Import to load a text file from the public domain-focused Project Gutenberg website. This imported text will be set to a variable that we can then use as a part of our code.

Code to import Pride and Prejudice from Project Gutenberg and set to the book variable

Next, let’s do a few things. First, we’ll set up an initial list where we can split our text into individual words. This will be introList. After that, we’ll remove stopwords (words like “the,” “an” and so on) and set that to noStop. Finally, we’ll create three variables to act as our word lists: adjectives, adverbs and nouns:

Creating a list, removing stop words, and setting three variables for different types of text

(Thanks to William J. Turkel for explaining how to break down text into words using punctuation as boundary markers here.)

And Now, Grammar

As Mathematica needs a bit of time to determine where words sit grammatically, as seen in the following message that comes up after running this initial code —

Alert that reads “Performing grammatical analysis”

— you can use this time to come up with some Mad Libs prompts. What do you want the prompts to ask you to do? We’ll see some sample prompts later on, but now that you have the start of your word lists, consider what sort of prompting will help you with brainstorming.

After the words are analyzed, let’s begin to clean things up by making all words lowercase. To do this, we’ll use Map[ToLowercase], targeting our different variables and setting them to new ones:

Mapping all variables and the words therein to lowercase

Creating Word Lists through Sort and Tally

What can we do with these lists? First, we’ll use Sort and Tally, the latter of which combines words if they are used more than once by tallying them. Doing this also gives you some data to play with if you want to try writing like Austen, pulling from her most commonly used words. The last bit, Short, gives us a snippet of the output to make sure we have valid data coming through:

Sorting each word list and getting a tally to create discrete items in the list
Example word list, including the word “abhorrence”

“Abhorrence,” you say? Sounds like Austen!

Snagging Random Words

Now that we have discrete items in our list, let’s snag some words. We’ll create three new variables: getAdv, getAdv and getN. Make sure to pull from the freqlists, as those lists are the ones that include actual data:

Getting random words from the lists, with each grammatical type in its own variable

You’ll see three words after running the code. Each time to execute, you’ll see new words. Here’s what we got:

Three words as output, including “probable,” “usually,” and “count”

Cleaning Up the Data

Unfortunately, the output is a little ugly. While the tally gives good info, it’s not helpful for creating a Mad Libs-style prompt. To change this, we’ll convert the output to a string, then use StringDrop to get rid of extra characters:

Changing the retrieved words to strings, then removing excess characters along the edges like tally numbers and brackets

This code won’t always work for the end of the word. While all results can drop the first two characters safely (the 2 in StringDrop[noun], 2]), depending on the tally, –5 might be incorrect. If you want to dive deeper into this project, consider other ways to clean up the data.

For now, this is enough; we don’t want to procrastinate on our writing too much, after all!

Prompts and Prompting

Now we need a prompt. To start, here’s a boring ask: “What should I write about?” Our fill-in-the-blank becomes “I will write about [noun],” with Austen’s work supplying the list of possible nouns. Here is the code and output:

Code resulting in the phrase, “I will write about a count”

Note that the phrase needs both the space at the end as well as the <> to concatenate, or combine, the different strings. Our end result is perfectly Austenian, encouraging us to write about a count. Looks like we’re writing a regency novel — or about cereal mascots with a taste for chocolate.

A big issue with how the code is set up is that we have to run different cell blocks to get new information. Even if we create more prompts in our prompt code block, there’s still less randomness than a prompt generator should have. To that end, let’s combine things into one cell block, including some prompts at the end:

Compiled code block of previous code to consolidate it

(Note the use of adjstem2 <> “ly” to create an adverb. While we have an adverb list, I thought it might be more fun to create an adverb.)

After running this code, we get this:

I shall write educationally with the word “craftily.” I shall write about a remedies. The main character is thinking about remedies in a educational way. What happens next?

Alas, we have run into an issue with “a remedies” and “a educational,” but that’s all right. The core ideas are there. Brainstorming can commence.

Using Our Prompts

Many of Austen’s characters are crafty, and a didactic style wouldn’t be unusual for her inquisitive leads. Perhaps our main character is the curious sort of young woman who daydreams of medicinal study while taking care of an injured count.

As she wraps his wounded arm with her handkerchief, she feels a sense of pride and wonders… could she be more than just a coddled lady?

Elizabeth wrapped the count’s arm in her handkerchief. She was surprised to discover she didn’t care if it became soiled, so long as he was no longer ailing. “Your Excellency,” she murmured, ignoring the stares of his attendants, “are you well?”

Prompting Again and Again

If we run the code a few more times, here’s another set of prompts:

I shall write candidly with the word “nimbleness.” I shall write about a transition. The main character is thinking about transition in a candid way. What happens next?

A poignant prompt…. What might be going on in our main character’s life? What sort of transitory thoughts are they having?

Going back to our young woman who dreams of medicine, perhaps the count was so grateful for her aid that he has requested courtship, but the status change to “betrothed” leaves her uneasy. Why is that?

It wasn’t even the Season, and here she was, being approached by a count no less! Elizabeth felt her heart stutter, but not for the reasons she’d expect. This wasn’t how her sisters had described. If this was love, it was all wrong.

Wrapping It Up

This project naturally shapes the story due to limiting the scope of words to a particular writing style. An erudite writer will necessitate eloquence, while a practical one might call for simplicity. But you could also mix and match. What if it’s a regency… in space?

“Your Excellency,” Elizabeth said, in a mimicry their first meeting, “I can’t travel with you to Mars.”

This code isn’t elegant, but it does the job. Each iteration inspires new ideas. Feel free to rework things as you see fit to make it run better or with more features.

Happy writing!

About the blogger:

Smiling white woman with brown wavy hair and glasses surrounded by a blue circle

Jesika Brooks

Jesika Brooks is an editor and bookworm with a Master of Library and Information Science degree. A lifelong learner herself, she has always been fascinated by the intersection of education and technology. She edits the Tech-Based Teaching blog (and always wants to hear from new voices!).

--

--

Tech-Based Teaching Editor
Tech-Based Teaching: Computational Thinking in the Classroom

Tech-Based Teaching is all about computational thinking, edtech, and the ways that tech enriches learning. Want to contribute? Reach out to edutech@wolfram.com.