---
abstract: 'We provide an approach for generating beautiful poetry. Our sonnet-generation algorithm includes several novel elements that improve over the state of the art, leading to metrical, rhyming poetry with many human-like qualities. These novel elements include in-line punctuation, part of speech restrictions, and more appropriate training corpora. Our work is the winner of the 2018 PoetiX Literary Turing Test Award for computer-generated poetry.'
author:
- |
    **John Benhardt,$^{1\dagger}$ Peter Hase,$^{1,2,\dagger}$ Liuyi Zhu,$^{1,\dagger}$ Cynthia Rudin$^1$** [^1]\
    ^1^Duke University\
    ^2^UNC Chapel Hill\
    <span>`gutierrezanthony@example.net, gutierrezanthony@example.net, gutierrezanthony@example.net, gutierrezanthony@example.net`</span>
bibliography:
- 'acl2019.bib'
nocite: '[@endymion; @whitman; @hungerGames1; @hungerGames2; @hungerGames3]'
title: 'Shall I Compare Thee to a Machine-Written Sonnet? An Approach to Algorithmic Sonnet Generation'
---

Introduction
============

Automated poetry generation is an extremely difficult task; currently no algorithm is able to capture the complexity of language well enough to generate truly meaningful poetry with all the nuances expected from a human poet @deep-speare. In the leading paradigm of poetry generation algorithms, which are founded in statistical learning of human poetry, humans must strike a fine balance between granting an algorithm expressive freedom and constraining it to follow poetic rules. This process requires finding good inductive biases for the learning of grammar, rhyme, meter, poetic style, and semantic content, as well as enforcing the sampling constraints needed to satisfy the relevant poetic rules.

One particular form of poetry, the Shakespearean sonnet, provides a special challenge for algorithms. Each line in these poems must fit the stress pattern of iambic pentameter and the poem as a whole must follow the rhyming scheme of *ABABCDCDEFEFGG*. These constraints, in addition to standard rules of grammar, must be obeyed when generating poems.

In this paper we present a new approach for generating Shakespearean sonnets. By combining neural language models and expert rules, we demonstrate how certain inductive biases and sampling constraints can greatly improve poetry generation. Poetry generated by our algorithm is shown in Table \[lead\].[^2]

\[lead\]

Comparing our algorithm to past work, we show that poetry quality is noticeably improved by the addition of in-line punctuation, part-of-speech-based sampling constraints, and careful selection of language model training corpora. As poetry is ideally judged by humans, we discuss an extrinsic evaluation of our results.[^3]

Related Work
============

Poetry generation techniques rely on two general methods: rule-based expert systems and statistical learning methods. Expert systems have made use of grammatical, metrical, and rhyming templates, drawing words from source texts of varying sizes @oliveira2009 [@oliveira2012; @gervas2000; @colton2012] or knowledge bases built from larger corpora @netzer2009. @greene provided an initial use case of a statistical language model, and since then neural language models have been used widely in production of English, Spanish, and Chinese poetry @topical_poetry [@hafez; @rhythmic_verse; @deep-speare; @yi2018]. The core of these approaches is to train a neural language model on poetry and then sample from the model with constraints designed to satisfy poetic rules.

Our approach is most similar to those of @topical_poetry and @deep-speare; both of these approaches use constrained sampling from a neural language model to generate Shakespearean sonnets.

Relative to the work of @topical_poetry, we improve poetry generation by means of adding in-line punctuation, adding part-of-speech-based grammatical constraints, and using training corpora more appropriate to the sonnet format.

Our work was performed concurrently with that of @deep-speare, and the conceptual differences between our approaches are two-fold: First, as we sample from the language model, we follow hard constraints on meter and rhyme, whereas they enforce soft constraints suggested by meter and rhyme models. Second, we select lines via a beam search to approximately optimize for line likelihood under the model, while they generate lines by sampling word by word from the language model using a low-temperature softmax. Consequently, our poetry follows the metrical and rhyme constraints of sonnets more precisely and each line will tend to have a higher likelihood than the lines in their sonnets, as computed by their respective language models.

Approach
========

The base of our approach is shared in common with the Hafez algorithm of @topical_poetry. At a high level, the scheme is to first pick the rhyme words and then use a word-level RNN to build each line backwards from the rhyme words.

Base of Approach
----------------

Critical to the Hafez approach was the Carnegie Mellon University Pronouncing Dictionary,[^4] which contains phoneme and stress patterns for over 134,000 words. Our approach uses two key pieces of information from the dictionary: the phoneme structure for each word and the stress pattern for each word. While the CMU dictionary includes three stress patterns, we simplify the dictionary to include only unstressed ($0$) and stressed ($1$) syllables. We use this dictionary to ensure that all generated lines conform to iambic pentameter and follow the necessary rhyme pattern.

Generation begins with a user supplied prompt. The first step is to pick rhyme words. To do so, we compute a similarity score between the GloVe vector representation of the user-supplied topic and all metrically valid rhyme pairs @pennington2014glove. This similarity score is the maximum cosine similarity between the topic representation and each of the two words in the rhyme pair. If the user-supplied topic consists of several distinct tokens, we take the average of their word embeddings as the topic representation. We then transform the rhyme pair similarities into a probability distribution and sample rhyme pairs from the distribution, favoring rhyme pairs with high similarities. To conform with the sonnet rhyme scheme, each time a pair is drawn, all other pairs with that rhyme phoneme are discarded from the distribution and the probabilities are renormalized.

Given the rhyme words, we begin constructing each of the fourteen lines of the sonnet independently. We use a word-level RNN with LSTM units to evaluate the likelihoods of metrically valid sequences @lstm.[^5] In doing so, we employ a beam search, selecting for the highest likelihood paths. We sample each line from the 10 highest likelihood lines returned by the beam search. For search widths under 20 words, poems can be generated within 10 minutes on a standard laptop CPU.

Novel Elements in Generation
----------------------------

Here we describe a few improvements over the previous state of the art represented in @topical_poetry, and we corroborate the usefulness of some techniques that appear in the concurrent work of @deep-speare.

### Adding In-line Punctuation

We have found that in-line punctuation makes poems far more representative of actual human poetry, which often reads more slowly than prose due to comma placement.

We add commas in a post-processing step, after all of the words for a poem have been chosen. Our approach is motivated by an issue inherent to selecting for punctuation during the construction of the poem. Punctuation does not affect the meter, which means that in order for a beam search to search over lines including punctuation, possible next words would always need to be considered with and without punctuation preceding them. This would increase the search cost by a factor of roughly $2^{d}$, where $d$ is the search depth.

We circumvent this cost by approximately maximizing the line likelihoods, conditioned on the poem having some number $n$ commas. Once the words for a poem have been selected, our algorithm samples a number $n$ of commas to place into the poem. Then it evaluates the likelihood of a comma in every possible position, before placing commas into the $n$ most likely positions. We choose a distribution over the number of commas that produces a desirable amount of punctuation, in our judgment. This method only approximately optimizes the line likelihoods, since the ranking of the most likely positions could change after every comma placed into the poem. In practice, we observe that the approach tends to place commas in appropriate locations.

Thus we achieve the presence of punctuation as @deep-speare do, but unlike in their work, we still optimize for line likelihood.

### Part of Speech Restrictions

We observe that across competitive approaches to poetry generation, part-of-speech (PoS) errors occur that could be easily detected by a human (see Section \[comparison\]). Using the Python NLTK package's PoS tags, we reviewed poems generated by an earlier version of our program and recorded the tag sequences that produced ungrammatical phrases. We then used this knowledge to restrict the possible PoS paths in generated lines to ensure that the ungrammatical PoS sequences would not appear. For example, a pronoun typically does not directly precede a pronoun in English (e.g. "he it"), so we add a restriction to prevent this from occurring. We created rules in this form for the 35 parts of speech that occur in the NLTK package.

### Suitable Corpora

Relative to the work of @topical_poetry, which used a training corpus of contemporary song lyrics, we rely on corpora more suited to sonnet generation, namely <span>*Endymion*</span> by John Keats, collected works of Walt Whitman, and the <span>*Hunger Games*</span> trilogy by Suzanne Collins.[^6] We found that, compared to using a combination of texts from a wide variety of authors, using works from a single author provided a more coherent voice to our generated poems. Meanwhile, the texts have their individual fits to the meter and cadence that sonnets require. <span>*Endymion*</span> is written in iambic pentameter, the meter used in sonnets, while Walt Whitman's style is more traditionally poetic than the song lyric corpus. When generating a poem, we randomly pick a language model trained exclusively on one author's works.

@deep-speare use a corpus of sonnets. This is certainly suitable for the gutierrezanthony@example.net. However, without hard sampling constraints on meter and rhyme, their method is incapable of producing sonnets if they were to train on texts like the Hunger Games. Consequently, they cannot write sonnets in the voice of a prose writer, as our approach and that of @topical_poetry can.

Evaluation
==========

In this section we compare our poetry to that of @topical_poetry[^7] and @deep-speare.[^8] We also discuss an extrinsic evaluation of our work.

Poem Comparisons {#comparison}
----------------

In Tables \[ourPoems\] and \[otherPoems\], we show quatrains from our approach as well as that of @topical_poetry and @deep-speare. Through our PoS sampling constraints, our poetry avoids grammatical errors seen in Quatrains 3 and 5. By prohibiting directly adjacent nouns, our approach avoids such phrases as "human beings procreation" in 3, and a constraint on adjacent comparative adjectives and verbs would prevent "greater be" in 5.

Meanwhile, Quatrains 5 and 6 fails to adhere to an acceptable rhyme scheme. @deep-speare report an F1 score of .91 when evaluating against the CMU dictionary, but even a single mistake in the rhyme can give the poem away as being machine-generated. By directly constraining based on the CMU dictionary, our approach always avoids these kinds of mistakes.

Extrinsic Evaluation
--------------------

At present, poems' meter and rhyme can be automatically evaluated, but qualities like theme, imagery, and other important poetic flourishes must still be evaluated by human readers. Since humans sometimes bend formal poetic rules to achieve certain literary effects @deep-speare, an overall assessment of a poem is best given by a human reader who can holistically evaluate its quality.

Poetry from our algorithm was assessed by a team of trained readers organized by Dartmouth's Neukom Insitute for the purpose of judging the 2018 PoetiX Literary Turing Test. In Turing test fashion, sonnets generated by our approach were mixed with human-generated sonnets and presented to the judges for them to distinguish between. The judges were able to successfully separate the human poems from ours, with a majority of judges labeling every case correctly. Yet while our poems failed to fool the judges, they did receive first place in the competition.

It is important to note that these judges were trained readers of sonnets, some with backgrounds in computer science and some in the humanities. @deep-speare find that "meter is largely ignored by lay persons in poetry evaluation," and so a thorough evaluation requires expert readership.

Our method succeeds that of @topical_poetry as a winner of the Neukom Institute's competition.

Conclusion
==========

We present an approach for algorithmic sonnet generation that capitalizes on inductive biases and expert rules to produce sonnets with many human-like qualities. While our poems are generated by approximate likelihood maximization, they also contain punctuation, always follow the metrical and rhyme-related rules of sonnets, and reflect the voice of the writer whose works they were trained on. Our approach was evaluated by expert readers in the Neukom Institute's 2018 Literary Turing Test, where it received first prize.

[^1]:   $\dagger$ denotes equal contribution.

[^2]: All examples from our approach shown in this paper are drawn from the same set of 50 generated sonnets. More examples are available in the supplement.

[^3]: The code for our work is available at .

[^4]: http://www.speech.cs.cmu.edu/cgi-bin/cmudict

[^5]: Hyperparameters are given in the supplement.

[^6]: These texts are all publicly available.

[^7]: Obtained through the interface provided by @hafez

[^8]: Poems were generated from the `LM`$^{**}$+`PM`+`RM` model.

{'source': 'arXiv_out/1811'}