How I Tackled NaNoGenMo 2020
By: Hazel Meades
Tags: Python, NaNoWriMo, Creative Process, Nanogenmo, Python Programming

How I Tackled NaNoGenMo 2020

“Spend the month of November writing code that generates a novel of 50k+ words” —https://nanogenmo.github.io/

NaNoGenMo, or National Novel Generation Month, is one of many events inspired by the popularity of National Novel Writing Month (NaNoWriMo). The goal of NaNoWriMo is to write over 50,000 words in the month of November. NaNoGenMo shares that goal, except you use code to create your novel!

A friend introduced NaNoWriMo to me during high school, but I stumbled across NaNoGenMo entirely by accident via Twitter about a year ago. I’ve successfully taken part in NaNoWriMo 3 out of 4 times since my GCSEs (I only got about halfway with my first attempt) so, this year, I felt it was time to up the creative challenge!

Getting Inspired

I’m familiar with the nitty gritty coding challenges of trying to make a website look pretty (the front-end bit, for those unfamiliar with coding jargon) as well as functional, but I’m less familiar with how to store and manipulate data behind the scenes (the back-end bit).

My interest in back-end web development originated in a music programming module I did too long ago to remember. That interest continued a couple of years later via an 8 week Python programming course with Code First Girls (which I totally recommend) and a diversity hackathon. I’d gotten a taste of what back-end programming languages could do, but I was hardly an expert in creative computing.

This meant that the first step of NaNoGenMo, for me, was to explore creative possibilities. I wanted to see what novels had been generated in previous years, and work out what I could achieve with my new Python skills.

Fortunately, there’s plenty of creative computing inspiration out there! Here are some of my favourite NaNoGenMo examples:

Revolution Recipe by Zach Whalen — a NaNoGenMo YouTube tutorial that combines the communist manifesto with the contents of a recipe book (more on this later).

50,000 Meows by Hugo — this is pretty much what it says on the tin. It even stretches out meows to match the word length of the text it’s based on!

In Dialogue by Leonard Richardson— this code replaces dialogue in one text with dialogue from another. The example output, Alice’s Adventures in the Whale, is a brilliant combination of prose from Alice in Wonderland and dialogue from Moby Dick.

My search for inspiration taught me that my novel didn’t have to make sense or be hard to code, but it did need to be based on a good idea. So long as you’re not infringing on copyright, you can do anything for NaNoGenMo!

A Warm Up Exercise

After several days of exploring NaNoGenMo examples, I decided it was time to exercise my coding chops. I began by following the Zach Whalen YouTube tutorial.

The tutorial introduced me to the Markovify library - a thing that allows you to generate random sentences based on a text file. The generated text output is completely dependent on the text file you feed your code, so I decided to feed it a combination of my 4 NaNoWriMo attempts.

I was excited to create a meta-novel of my own writing, but I hit a couple of snags on the way there.

Snag 1: Preparing the data

I had to sift through my 4 NaNoWriMo attempts to remove page numbers, chapter titles, asterisks and acknowledgements. I wanted my generative output to focus solely on the narrative body of text, as opposed to random numbers, section breaks, and thank yous.

Data preparation is a mundane frustration that I’m sure all data scientists have faced. In my case, it meant realising through trial and error that combining half a novel written in first person and 3 whole novels written in third person might not be a great idea. It did produce some amusing sentences though!

Snag 2: Word salad

As much as I loved the idea of Markovifying my previous writing, the result was essentially a huge block of nonsense sentences. The output wasn’t creative enough for my liking and, unfortunately, my research suggested that there was no quick fix for this text comprehension problem. Sure, my novel didn’t have to make sense, but I wanted it to be more readable than it was.

Some example text output. Just look at that word salad!

I quickly realised that, in order to make the text output less nonsensical, I would need to deepen my understanding of machine learning and sentiment analysis. But I was also working to the NaNoGenMo deadline. I didn’t have time to fully immerse myself in complicated computer science concepts alongside my life commitments outside of the project, especially speaking as someone who’s never officially studied computer science!

By this point, I was happy to dismiss my Markovify NaNoGenMo attempt as a warmup exercise. I’d met the 50k word count, but I’d also hit a creative dead end. I knew I could do something much more interesting!

Making Meta Poetry

Next, I decided to try making limericks out of the BBC website.

My Python programming course had already introduced me to API requests (a thing that allows you to access data from and communicate with other applications), so I looked into whether the BBC had an API that I could play with.

Sadly, it did not.

Snag 3: the BBC Developer Portal is for BBC employees only.

Luckily, one of the programming concepts I’d googled while gathering creative/technical inspiration, provided a potential workaround for this snag. My inspiration search led me to download a free web scraping app called ParseHub.

ParseHub essentially lets you trawl through a website of your choice, extract the data you want from it, and generate an API to access that data. I hadn’t thought any of this was relevant to what I was doing for NaNoGenMo initially, so I’d saved it for extra learning at a later date. Now however, it was ParseHub’s time to shine!

A Moment of Prioritisation

By this point, a significant amount of life stuff had interrupted the trajectory of my NaNoGenMo project. I paused to reappraise my coding priorities in the grand scheme of things and decided that I would continue to use my NaNoWriMo writing as a warmup dataset. I’d test my code on the NaNoWriMo text file before applying the code to data from the BBC.

The most important part of my NaNoGenMo project was getting the core functionality of the code (turning an input text file into limericks) to work, so I decided to come back to ParseHub and APIs once I’d successfully generated 50,000 word’s worth of limericks.

Snag 4: How to code a limerick

I did the requisite amount of googling and found a few interesting examples of how to approach limerick generation. However, most of the examples were written in programming languages that I was unfamiliar with.

I could only find one limerick generator written in Python and it was, strangely enough, too simplistic to meet my needs. I probably know enough about Python now to draw inspiration from that code and make the appropriate changes but, at the time, I didn’t have that experiential knowledge. Also, the NaNoGenMo deadline was rapidly approaching! I didn’t have time to search for more appropriate limerick generators.

Haiku Generation

I changed poetry tack and started searching for haiku generators that I’d seen online before. During this search I realised that haikus are well suited to this kind of project because of their short, abstract nature. Haikus don’t have to make much grammatical sense to strike a chord in the reader!

I quickly found a haiku generator tutorial written in Python. It seemed perfect for my purposes, except…

Snag 5: Downgrading Python

A lot of the tutorials that I’d skim-read recommended using the spaCy library to play around with words. I was reluctant to install this library because I would have to downgrade my version of Python. I was nervous about future code compatibility issues, but it soon became clear that I had no other option if I wanted to get anywhere with my NaNoGenMo project.

I downgraded my version of Python. The process was a lot more difficult than it had any right to be, but I was eventually able to install all the libraries I needed to achieve maximum haiku. It brought me one tantalising step closer to functional NaNoGenMo code!

Snag 6: Tutorial troubleshooting

I’m not entirely sure what went wrong here. I carefully followed all of the tutorial steps, googled error messages and, eventually, turned to my software engineer father for troubleshooting help (he has grown accustomed to my screenshots over the years).

For reasons I still don’t understand, the code worked much better when run directly through my text editor, instead of Jupyter Notebook. 🤷‍♀️ It just goes to show that sometimes the best troubleshooting method is to talk to someone who actually knows what they’re doing!

Finally Getting Somewhere

Once the tutorial code was working, it generated a haiku using random words from my NaNoWriMo dataset. It then gave me the option to create another haiku or exit the program.

From here on out, the process of writing my NaNoGenMo code was relatively smooth sailing. All I had to do was alter the tutorial code to better fit my 50k+ word count needs.

My code alterations included:

Adding a few lines of code to generate random haiku titles.

Capitalising my haiku titles (the all caps was an stylistic accident that I grew to love).

Editing the code structure so the program would continue to generate haikus until it met the 50k word count.

Saving the haiku output as a text file.

Converting the text file to a more presentable PDF file.

The end result was a fun compilation of my writing style in haiku format. You can check out the source code and output PDF on my Github.

What have I learned?

I’m proud of my NaNoGenMo project, even though I didn’t have time to test its application beyond my warmup dataset (to be fair, using text from the BBC website idea might have invoked copyright issues…). The project helped me get to grips with Python on a deeper level, it built up my coding confidence and, most importantly, it taught me that the only limit to my coding imagination is time.

If I want to generate stories via machine learning, I can do that, but first I must put aside enough time to allow for the inevitable learning curve — even supposedly simple code can take an unexpectedly long time to implement!

Future Improvements

NaNoGenMo may be over, but my enthusiasm is not. There’s definitely room for improvement in my code! Here are a few of the updates I want to make:

PDF refinement — the output PDF could be better formatted. It should include a title, page numbers and more page breaks (so haikus don’t get split across pages). Interactivity — I could ask the user to provide the input text file. Data experiments —I never got to test my code on data outside of my NaNoWriMo writing, but now I have all the time in the world to get to grips with APIs and try it!

My NaNoGenMo project has given me more than a few coding ideas. I might even try my hand at limerick generation all over again, now that I have some semblance of what I’m doing. If you’re interested in seeing that, stay tuned for more novice —friendly coding content! 😉

For the time being, I shall leave you with a few of my favourite haikus.