Abstract
Researchers at USC and the University of Washington developed Hafez, a system that generates sonnets on any user-provided topic. By combining Finite-State Acceptors (FSAs) to enforce rigid iambic pentameter with Recurrent Neural Networks (RNNs) to ensure linguistic fluency, the system produces poetry that is both formally perfect and topically relevant.
Introduction: The Challenge of Constrained Creativity
Writing poetry is a "constrained creativity" task. A Shakespearean sonnet requires a strict 14-line structure, a specific rhyme scheme (ABAB CDCD EFEF GG), and iambic pentameter—ten syllables per line with alternating stress.
Previous automated systems typically suffered from one of two issues:
- The "Rule-Based" Trap: Systems followed formal rules perfectly but produced repetitive or nonsensical text.
- The "Deep Learning" Trap: Models produced fluent, beautiful sentences that completely ignored meter and rhyme constraints.
The Hafez system bridges this gap, creating a framework that maintains topicality while adhering strictly to formal poetic constraints.
System Architecture: The Hafez Pipeline
The Hafez system operates through a multi-stage pipeline that integrates symbolic AI with modern deep learning techniques.
- Topical Expansion: Upon receiving a user topic (e.g., "wedding"), the system uses word2vec (trained on Wikipedia and news) to identify approximately 1,000 related words (e.g., "bride," "ceremony").
- Rhyme Selection: It selects pairs of these related words to serve as line endings. To increase flexibility, the authors developed a custom slant rhyme algorithm that allows for near-matches, mimicking the creative license of human poets.
- The FSA "Skeleton": The system constructs a massive Finite-State Acceptor (FSA). This directed graph represents every possible word sequence that perfectly obeys iambic pentameter. If a word violates the "da-DUM da-DUM" rhythm, it is excluded from the FSA.
- RNN Path Extraction: To navigate the billions of possible paths within the FSA, an RNN language model (trained on 32 million words of song lyrics) scores the paths to find the most natural-sounding English sequence.
- Reverse Generation: The system generates each line backwards, starting from the pre-selected rhyme word. This ensures the line always ends on the correct rhyme without "painting the model into a corner."
- The "Translation" Model: To further boost topicality, an encoder-decoder model treats the rhyme word as a "source" and the rest of the line as a "translation," forcing the entire line to relate to the ending word.
Datasets and Training
The system relies on several key linguistic resources:
- CMU Pronunciation Dictionary: Used to extract syllable counts and stress patterns for ~125,000 words.
- Google Gigaword & Wikipedia: Used to train word2vec for topical word associations.
- Song Lyric Corpus: A collection of 94,882 English songs used to train the RNN for fluency and the "translation" model.
- Spanish Resources: For multilingual testing, the authors utilized Spanish Wikipedia and the LyricWikia corpus.
Key Insights
- Constraints as a Filter: Instead of training an RNN to learn rhyming rules, the authors used the FSA to define the "search space" of legal moves. The RNN’s role is solely to select the most fluent path within those bounds.
- The Power of Slant Rhymes: Moving beyond perfect rhymes allows the system to use a wider variety of topical words, making the output feel more modern.
- Reverse Decoding: In constrained generation, the end of the sentence is the most restricted. Starting at the rhyme ensures formal requirements are met first.
Evaluation and Results
The system was evaluated through human judgment, focusing on topicality and fluency.
- Topicality: Judges preferred sonnets using the "topical encouragement" system 54% of the time, compared to 18% for those without it.
- The Translation Model: The encoder-decoder approach was highly successful; human judges preferred its sonnets 57% of the time over the standard RNN model.
- Plagiarism Check: The system was confirmed to be generative rather than derivative. On average, only 1.2 5-grams were copied from the training lyrics per sonnet.
- Multilingual Success: The architecture proved language-agnostic, successfully generating Spanish sonetas by adjusting the FSA to handle sinalefa (vowel merging).
Limitations and Future Work
Despite its success, Hafez faces challenges with global coherence. While individual lines are fluent, the poem as a whole often lacks a narrative arc. Additionally, minor grammar glitches occur (e.g., "a" vs. "an") because these depend on the subsequent word, which is difficult to manage during reverse generation. Future work aims to incorporate "discourse structure" to give poems a clearer beginning, middle, and end.
Reference: Ghazvininejad, M., Shi, X., Choi, Y., & Knight, K. (2016). Generating Topical Poetry. Proceedings of EMNLP 2016.
Summary updated