Exquisite cadavers — generating lines of poetry using a Monte Carlo engine.

Charles Ivia
3 min readApr 19, 2021

--

Image: Getty Images/iStock

I recently discovered the thrilling world of Monte Carlo engines upon reading Nassim Taleb’s book — Fooled by Randomness. I was particularly intrigued by the idea of creating poetic sentences from a random combination of words.

In the book, Nassim tells the story of “exquisite cadavers” where interesting and poetic sentences are randomly constructed. According to him, If you throw enough words together, some “unusual and magical sounding metaphor is bound to emerge according to the laws of combinatorics”.

In this article, I will show you how to create a Monte Carlo poetry generator using python.

Why “exquisite cadavers”?

After the First World War, several surrealist poets which included Andre Breton and Paul Eluard got together in cafes and attempted the following exercise. The poets would fold a piece of paper and each of them would write a predetermined part of a sentence without knowing the other’s choice. The first would pick an adjective, the second a noun, the third a verb, the fourth an adjective and the fifth a noun. One of these exercises yielded the following poetic sentence:

The exquisite cadavers shall drink the new wine.
(Les cadavres exquis boiront le vin nouveau.)

It’s all about words words and more words

To build a good Monte Carlo poetry generator we need plenty of words- adjectives, verbs and nouns. And to get a good number of adjectives, I could not find a better resource than the paperrater.com website which we are going to scrap. First, we import the requests and BeautifulSoup libraries to help with the scrapping. Next, we create an empty list to hold the adjectives.

Lastly, we define the get_adjectives function which sends a get request to paperrater.com and returns a response object called page. The function also uses beautiful soup to parse the response object and get adjectives from the “li” tags. We then append the adjectives to the empty list created earlier and return it. To scrap nouns and verbs, we follow the same procedure as above but with a few modifications as shown in the code snippets below.

Next, we need verbs to show action, event or state of being. Similarly, the verbs list required some scrapping, this time from from englishstudyhere.com.

Next, we scrap hundreds of nouns from englishstudyhere.com using the code below.

Finally, we put together the three snippets above and name the file- scrapper.py. This file becomes our module from which we can import the necessary functions.

Putting the engine together

From our scrapper module we import the necessary functions to fetch verbs, adjectives and nouns. We also import the random module to help us shuffle the words. Note that we create a list of linking verbs to tie the words into poetic sentences.

Now that we have everything we need, we create the construct_poetry function to randomly select words from the shuffled lists and construct a sentence. Finally, a “for” loop runs the function hundred times and outputs the poetic lines with the first letter capitalized.

Combining the last two snippets into a script and running it will generate 100 lines of poetry. As you read though them, you will find a few that are ugly and meaningless, others that are funny and others that will awe you with their beauty and depth. Below are some of the interesting lines that were formed.

You can clone the repository from here. Thanks to Neville Omangi and Doreen Muthoni for reading drafts of this.

--

--

Charles Ivia
Charles Ivia

No responses yet