Icon ASCII : A Love Letter


Icon My Neural Network isn't working! What should I do?


Icon Phase-Functioned Neural Networks for Character Control


Icon 17 Line Markov Chain


Icon 14 Character Random Number Generator


Icon Simple Two Joint IK


Icon Generating Icons with Pixel Sorting


Icon Neural Network Ambient Occlusion


Icon Three Short Stories about the East Coast Main Line


Icon The New Alphabet


Icon "The Color Munifni Exists"


Icon A Deep Learning Framework For Character Motion Synthesis and Editing


Icon The Halting Problem and The Moral Arbitrator


Icon The Witness


Icon Four Seasons Crisp Omelette


Icon At the Bottom of the Elevator


Icon Tracing Functions in Python


Icon Still Things and Moving Things


Icon water.cpp


Icon Making Poetry in Piet


Icon Learning Motion Manifolds with Convolutional Autoencoders


Icon Learning an Inverse Rig Mapping for Character Animation


Icon Infinity Doesn't Exist


Icon Polyconf


Icon Raleigh


Icon The Skagerrak


Icon Printing a Stack Trace with MinGW


Icon The Border Pines


Icon You could have invented Parser Combinators


Icon Ready for the Fight


Icon Earthbound


Icon Turing Drawings


Icon Lost Child Announcement


Icon Shelter


Icon Data Science, how hard can it be?


Icon Denki Furo


Icon In Defence of the Unitype


Icon Maya Velocity Node


Icon Sandy Denny


Icon What type of Machine is the C Preprocessor?


Icon Which AI is more human?


Icon Gone Home


Icon Thoughts on Japan


Icon Can Computers Think?


Icon Counting Sheep & Infinity


Icon How Nature Builds Computers


Icon Painkillers


Icon Correct Box Sphere Intersection


Icon Avoiding Shader Conditionals


Icon Writing Portable OpenGL


Icon The Only Cable Car in Ireland


Icon Is the C Preprocessor Turing Complete?


Icon The aesthetics of code


Icon Issues with SDL on iOS and Android


Icon How I learned to stop worrying and love statistics


Icon PyMark


Icon AutoC Tools


Icon Scripting xNormal with Python


Icon Six Myths About Ray Tracing


Icon The Web Giants Will Fall


Icon PyAutoC


Icon The Pirate Song


Icon Dear Esther


Icon Unsharp Anti Aliasing


Icon The First Boy


Icon Parallel programming isn't hard, optimisation is.


Icon Skyrim


Icon Recognizing a language is solving a problem


Icon Could an animal learn to program?




Icon Pure Depth SSAO


Icon Synchronized in Python


Icon 3d Printing


Icon Real Time Graphics is Virtual Reality


Icon Painting Style Renderer


Icon A very hard problem


Icon Indie Development vs Modding


Icon Corange


Icon 3ds Max PLY Exporter


Icon A Case for the Technical Artist


Icon Enums


Icon Scorpions have won evolution


Icon Dirt and Ashes


Icon Lazy Python


Icon Subdivision Modelling


Icon The Owl


Icon Mouse Traps


Icon Updated Art Reel


Icon Tech Reel


Icon Graphics Aren't the Enemy


Icon On Being A Games Artist


Icon The Bluebird


Icon Everything2


Icon Duck Engine


Icon Boarding Preview


Icon Sailing Preview


Icon Exodus Village Flyover


Icon Art Reel




Icon One Cat Just Leads To Another

17 Line Markov Chain

Created on March 14, 2017, 1:12 p.m.

In my most recent project ./code --poetry I created a code poem called irc.py that (as well as looking like an IRC chat itself) mimics IRC conversations when run. It looks something like this:

irc code poem

The output is randomized each time, but for a single instance, running the program looks something like this:


Essentially the program uses a simple Markov chain and a database of the IRC logs accumated from various IRC channels over the years to simulate the conversations. Now, reading that Wikipedia article, you would be forgiven for thinking that Markov chains are something limited to the realms of Mathematics graduates and PhDs - but actually Markov chains are pretty simple to implement in a language like Python, and can be coded up in just a few lines. Let us see what the Markov chain I implemented above looks like without all the poetry and decoration obfuscating it. But first, let us switch up the dataset a bit. Let us use this big database of poetry I collected a while back instead.

With the data downloaded, the first step is to read it in and separate it out into words. Newlines are important in poetry so for this data we'll use a regex to make sure we only split on spaces.

import re
with open('poetry.txt') as f:
    words = re.split(' +', f.read())

The essence of a Markov Chain is a transition function - a function which takes some previously generated words and produces the next one. To emulate this function we'll just use a lookup table. The input to the table will be the two previous words and the output will be a list of potential following words from which to choose from.

We can do this easily in Python using a defaultdict - a dictionary which has a default value for missing entries. In our case we'll use an empty list default. We'll loop over all the words in our database in groups of three and append the third word to the list of potential outputs produced from the first two words.

from collections import defaultdict

transition = defaultdict(list)

for w0, w1, w2 in zip(words[0:], words[1:], words[2:]):
    transition[w0, w1].append(w2)

We'll then pick some random starting location and find the three words located there:

from random import randint, choice
i = randint(0, len(words)-3)
w0, w1, w2 = words[i:i+3]

Then all we need to do is print out our current word w2 (followed by a space), update our previous word variables w0 and w1, and choose a next word from the list found via our transition function.

for _ in range(500):
    stdout.write(w2+' ')
    w0, w1, w2 = w1, w2, choice(transition[w1, w2])

That's it! Here is the whole thing.

import re
from sys import stdout
from random import randint, choice
from collections import defaultdict

with open('poetry.txt') as f:
    words = re.split(' +', f.read())

transition = defaultdict(list)
for w0, w1, w2 in zip(words[0:], words[1:], words[2:]):
    transition[w0, w1].append(w2)

i = randint(0, len(words)-3)
w0, w1, w2 = words[i:i+3]
for _ in range(500):
    stdout.write(w2+' ')
    w0, w1, w2 = w1, w2, choice(transition[w1, w2])

You can adjust the value 500 to produce shorter or longer poetry. Now you can produce some beautiful poetry like this!

Half hidden in helmet and shield, -
Point me the happiness that's mine;
That I am not
Going on with light!
Flowers she had, all she sees.
They cannot light the lakes,
When trumpets loose the string
And let them bring her.

In the long, long way and the jelly-bean
Play cockalorum-and-the-hen,
When the cool sound of sin ?
And him who would attack with terror seemed to shake.
There lyeth the body and my limbs gushed full of tunes that were born on the golden morrow
Beam'd upward from the world.

O contentment, make me cry on Willie, my boy is leading, calls his mate, ne any does not recede.
I foment them
With earnest eyes and I am full of thieves for thieves' use: give me room! give loneliness and fidgety revenge.
Nobody knew where he lay
A band of very great boon
To all God's utterance,
We hardly knew a dearer rate;
And took, but not a beggar, in fixed eyes,
Lifting distressful hands as in the painted leaves of the Nightingale.

Along the sprawled city's modern swarm and propagate the sure esteem
To cheated youth's midsummer dream,
When every friend is one and the gay, man,
Fal, la, la, &c.;

Here's a jest!
what word will come to the following two distichs of my heart,
Lord of my own breath. I'm alone and none other than him, so I am but a richer emerald glows:
Into each flower-cup
Her cool dews are round her name.

Bowl of gems
Transports me with your blocks?
Castles and palaces, temples and towers,
Cut shorter many a Nymph or Goddess of the hands of unknown shores
And laughed as he was, are we.
github twitter rss