Aug 9, 2016

Teaching a Computer to Write Poetry

For decades, computers have been taking over tasks previously done by humans. Now a team of Berkeley I School researchers is taking that to the next level by designing a computer system that writes poetry.

“Poetry is the spontaneous overflow of powerful feelings: it takes its origin from emotion recollected in tranquillity,” wrote William Wordsworth. Can a computer — without feelings or emotion — really match a human poet? Can a computer independently write poetry that is comparable to human poetry? That is the challenge faced by the team: I School student Andrea Gagliano (MIMS ’17); alumna Emily Paul (MIMS ’16); Kyle Booten, a Ph.D. candidate in education; and I School professor Marti Hearst, an expert in automated natural language processing.

Passing the Turing Test

The discipline of Natural Language Processing traditionally aims to analyze and understand human language. Increasingly, however, the computers are learning to respond — to communicate back to humans using human language. But poetry presents unique challenges.

The Turing Test is a classic test of artificial intelligence named for pioneering computer scientist Alan Turing. The challenge is to devise a computer algorithm that is indistinguishable from a human when answering questions or engaging in conversation. In this case, the challenge is to program a computer to write poetry that is indistinguishable from human-written poetry.

The Neukom Institute for Computational Science at Dartmouth College sponsors Turing Test competitions in the creative arts — including a “PoetiX” competition for computer-written poetry. And not just any poems; the challenge requires the computers to compose sonnets, an especially constrained poetic form.

As the team prepared to enter the 2016 competition, the first challenge was to train the computer to follow the rigid poetic form of the sonnet.

What is a Sonnet?

A sonnet follows a strict scheme of rhyme and meter. A traditional sonnet is fourteen lines of rhymed iambic pentameter; Shakespearean sonnets follow the rhyme scheme ABAB CDCD EFEF GG, while Petrarchan (or Italian) sonnets us the rhyme scheme ABBAABBA CDECDE or ABBAABBA CDCDCD.

As the building blocks for automated sonnet construction, the team first needed a catalog of rhyming words. They started with the open-source Carnegie Mellon University Pronouncing Dictionary. They then analyzed a corpus of 1,871 sonnets by Shakespeare and other poets. Knowing the expected rhyme patterns, the algorithm was able to fill in the blanks and figure out the rhyming relationships among words used in the sonnets. This analysis generated a rhyme bank 4,336 words in 344 rhyming sets.

The algorithm then constructs poetic lines, starting with the the end rhyming word and working backward. It can construct both Shakespearean stanzas (ABAB) and Petrarchan stanzas (ABBA), and build them into a complete sonnet.

Understanding Metaphor

But poetry is more than meter and rhyme: the hallmark of poetic language is metaphor. Understanding metaphor is a tall order for a computer algorithm, and figuring out which metaphors actually make sense is even more challenging.

Poets often evoke classic metaphors without stating the comparison explicitly. The researchers use Emily Dickinson’s “Because I Could Not Stop for Death” as an example:

“Because I could not stop for Death –
He kindly stopped for me –
The Carriage held but just Ourselves –
And Immortality.”

Dickinson compares death to a journey or a departure — but without using the word. “She merely needs to mention the words ‘death’ and ‘carriage’ in the same set of stanzas for the reader to know that the carriage is not taking a spin around the block, but rather a one-way trip with no return,” the researchers explain.

To teach the computer to write metaphorical language, the team started by using “word vectors,” which calculate webs of relationships between different words. Comparing the vectors of two different words generates “connector words” that can draw the two concepts together and create figurative relationships.

The researchers input a collection of 2,860 American poems for the system to analyze, detecting common topics, language, and themes. The computer then selected a pair of different “anchor words” to use to create a metaphor, and then used the word vectors to calculate appropriate connector words for that particular metaphor.

Sample stanzas can be found in the article “The Pythonic Poet,” published this summer in the iSchool Review. The article also includes an interactive quiz, to test whether you can tell the difference between Shakespearean stanzas and computer-generated ones.


Computer-generated sonnets from Berkeley researchers’ algorithm placed second in the 2016 “PoetiX” competition. Two computer-generated sonnets from each team were mixed with human-written sonnets and presented to a three-judge panel. The judges were asked to identify which sonnets were written by humans and which by computers.

Although the sonnets followed the neccesary meter and rhyme scheme and used rich figurative language, none of the competitors passed the Turing Test. All three judges had perfect scores, perfectly separating the human and computer poetry.


“The Pythonic Poet,” Emily Paul and Andrea Gagliano, iSchool Review, 1:1. 2016

“Intersecting Word Vectors to Take Figurative Language to New Heights,” Andrea Gagliano, Emily Paul,Kyle Booten, & Marti A. Hearst. Presented at the Fifth Workshop on Computational Linguistics for Literature, June 16, 2016, San Diego, California.

In the News

Last updated:

November 15, 2016