Close Mobile Menu

The Bot Versus the Bard: Researchers Teach a Computer to Write Poetry

August 30, 2016
by Katia Savchuk
Image of Shakespeare using a laptop

What’s in the brain that ink may character
Which hath not figured to thee my true spirit

King of hell no quarrel have I left thee
No lovely maid who gleaned in fields or skies

One pair of lines above is the work of Shakespeare. The other was written by a computer. Can you tell which is which?

If you’re having a hard time, that’s just what a group of UC Berkeley researchers intended (read on for the answer). A four-person team from the School of Information and the Graduate School of Education designed a computer algorithm that spits out sonnets. Dubbed the Pythonic Poet (after the computer programming language), it competed in Dartmouth College’s spring PoetiX competition, in which sonnet generators are judged according to how “human” their poems seem.

Image of robot hands typing on a computer

I-School graduate students Andrea Gagliano and Emily Paul, who completed her master’s degree in May, kicked off the project last fall in a class on natural language processing. They continued polishing their verse-generating code in independent study, working with Professor Marti Hearst of the I-School and Kyle Booten, a Ph.D student in education.

The goal was to design an algorithm (a set of rules for the computer to follow) that could create true sonnets, 14-line poems traditionally written in iambic pentameter. Per the contest rules, rhyme schemes could be Shakespearean (a-b-a-b) or Petrarchan (a-b-b-a).  

“The algorithm didn’t need to do the same process as a human, but it needed to get the same result,” Paul says. “Working on the algorithm helped us think about the creative process of humans and understand it better, and to see the limitations of computers.”

Poetry generation is a small but growing part of computational linguistics. Some algorithms rely on complex machine learning, while others fit words into strict templates. This team took a different approach. Starting with a set of 1,871 sonnets from the Bard and other poets, they identified pairs of rhyming words using the open-source Carnegie Mellon University Pronouncing Dictionary, creating a sort of  “rhyme bank” that eventually grew to 8,265 words with 344 rhyme sounds.

Using this trove of rhyming words, the Pythonic Poet writes sonnets backwards, starting each line with the last word, according to the rhyme scheme. It then adds the preceding word, basing its selection on the probability that the new word would appear before the one that now follows it in the original group of sonnets. This process continues, with the computer considering up to three words at a time, until the line reaches 10 syllables.

To help the sonnets feel more cohesive, the team taught the algorithm to write metaphors. First, they fed it 2,860 American poems and had it identify common themes. When a user kickstarts the algorithm by inputting a noun like “heart,” the algorithm picks a theme, choosing one that isn’t too closely related—like “sadness,” instead of the more predictable “love.” Paul and Gagliano say their biggest challenge was creating poems that flowed and had a theme, without being too obvious. To gauge the closeness of the relationship, the computer uses “word vectors,” which represent words in numerical terms. As the algorithm writes the sonnet, it favors words that fall mathematically somewhere between the original noun (heart) and the theme (sadness).

In May, the group’s efforts earned second place in Dartmouth’s first-ever PoetiX competition, which evaluates sonnet generators using the Turing test, a classic measure of artificial intelligence. Judges read six sonnets written by humans and four produced by machines (two from each of the two contestants). Although all the sonnets submitted met the technical parameters, none of the judges were fooled by the machine-created verses.

Cartoon of a robot holding a large pen

“To me, it reinforced that there’s a lot of nuance in poetry. We don’t yet understand creativity to the degree that we can reproduce it,” Paul says.

Computers are already pretty good at some creative pursuits, such as writing classical music. And of course, they’ve long bested humans at chess. There’s little doubt that machines will get better at the formal requirements of crafting poetry, as well, says Brian Christian, a poet and author who wrote about the Turing test in his 2012 book The Most Human Human. Whether they’ll ever be truly great—or even just passably good—is another question.

“It’s easier to replicate the technique and harder to give a machine something to say,” Christian says. “Every human has had a different life experience, and that’s why we have something to say to other people. That’s where great art comes from.”  

Matthew Zapruder, a poet and Berkeley alum who currently edits The New York Times Magazine’s poetry column, is also skeptical that computers could ever compose worthwhile poems. “You can teach a computer to be a bad poet, doing things you expect to be done. But a good poet breaks the rules and makes comparisons you didn’t know could be made,” he says. “If computers started writing really good poems, I’d be really scared because they would definitely be sentient.”

The researchers say projects like theirs could lead to creativity aids or spawn new forms of art through a back-and-forth collaboration between human and machine. They could also produce insights that help computers interact with people in less structured ways.

For now, the Berkeley team is continuing to refine the Pythonic Poet, fixing punctuation and grammar problems and trying to improve cohesion. They plan to enter the PoetiX competition again next year.

Christian, for one, thinks it’s fully possible that computers will one day be able to write with intent, becoming true artists. In the meantime, increasingly proficient machine poets could lead to a healthy rivalry: “For human poets to have something nipping at their heels is a real incentive to raise the level of the game,” he says.

So which of those lines did Shakespeare write?

He wrote the first pair in Sonnet 108, part of the “Fair Youth” sequence, first published in 1609.

The computer composed the second pair just last week. Let the rivalry begin.

Share this article