This experiment generates poetry with a first-order Markov chain: count which words follow one another, then sample a next word from those counts. I wanted to make those transitions visible, so a reader could follow the path from an input poem to a generated phrase.
Crafting verses with code#
The Poetry Generator takes a poem and counts which words follow each other. The application uses Markov chains1 to sample text from the word transitions in the supplied poem. A graph, a matrix and a heatmap let the reader inspect those transitions.

So, what are Markov chains?#
Markov chains are mathematical models that predict the next state based on the current state. In the context of text generation, each word represents a state, and the transition to the next word depends on the current word. Here’s how it works:
- Corpus Analysis: The application analyzes the provided poem to extract the words and their relationships. For example, given the phrase “the quick brown fox,” the words “the” and “quick” are connected, as are “quick” and “brown,” and so on.
- Transition Matrix: A transition matrix is created, where each cell represents the probability of transitioning from one word to another. This way, the model learns the likelihood of each word following another.
- Text Generation: Starting with an initial random word, the Markov chain generates the next word based on the transition probabilities. This process continues until the poem reaches a specified length.
By repeating this process, the Poetry Generator can reuse sequences from the original poem or combine them differently.
Looking at the transitions#
The project provides the user with three visualizations for the transition probabilities between words: a heatmap, a network graph, and a relationship matrix. Each shows the same word transitions in a different form.
Relationship Matrix#
This section displays the transition counts stored for each word. Each entry lists the words observed after it and how often each occurred. Those counts can then be normalized into probabilities for sampling.
For example, for the following input poem:
The sun sets in the west
And birds return to their nest
After lowercasing the text, the transition counts would look like this:
{
"the": {
"sun": 1,
"west": 1
},
"sun": {
"sets": 1
},
"sets": {
"in": 1
},
"in": {
"the": 1
},
"west": {
"and": 1
},
"and": {
"birds": 1
},
"birds": {
"return": 1
},
"return": {
"to": 1
},
"to": {
"their": 1
},
"their": {
"nest": 1
}
}
This object stores counts. To sample a next word, divide each outgoing count
by the sum for that word. Here, the is followed once by sun and once by
west, so each has probability 1/2. A word with no outgoing transition,
such as the final nest, needs a stopping or restart rule; the counts alone
do not define that behavior.
Heatmap#
The heatmap visualizes the transition probabilities between words, with each cell representing the likelihood of transitioning from one word to another. The color intensity indicates the strength of the relationship, allowing users to identify patterns and connections within the text.

Network Graph#
The network graph illustrates the words as nodes and the transition probabilities as edges between them. This visualization provides a clear representation of the connections between words, making it easier to identify clusters and relationships within the text. It’s also interactive, allowing users to explore the network, drag nodes, and zoom in for a closer look.

What to look for#
Try a short poem and inspect the transitions before generating another one. With very little input, some words have only one possible successor. At those points, sampling has no choice to make. Words with several successors are where the text can branch.
That makes the visualizations useful: you can compare the generated phrase with the edges that allowed it. The result may sound like a verse, repeat itself or stop abruptly. Those outcomes are part of exploring the model’s limits.
Footnotes
-
Markov chains are mathematical models that predict the next state based on the current state. In the context of text generation, each word represents a state, and the transition to the next word depends on the current word. If you want to learn more, check out the Wikipedia page on Markov chains. ↩