Text & Lyric Generation with Markov Chains
Find the text generator here, and the lyric generator here.
It fascinates me how simple weighted probabilities can produce semi-comprehensible text. (Playing with text generation, and then writing fiction, I became aware of how my mind also operates like the Markov chain.)
Based off an old college assignment, this project uses basic natural language processing to generate semi-plausible text.
Markov Chains
The algorithm divides the input text into k-grams, and for each k-gram, forms a frequency table for the following character.
When generating text, the algorithm looks at the current k-gram and selects a random character based off the weighted distribution of the frequency table.
The algorithm incorporates the character into the next k-gram and repeats the process.
This algorithm is called a Markov chain and produces long-strings of semi-believable text.
Try it out for yourself with text or lyrics.
Project Design
I coded the Markov chain in Python and hosted it on Heroku with the Flask micro web framework. I wrote a CSS/HTML/JavaScript GUI to mimic the effect of the Terminal when originally testing the algorithm. For the animation I used the Typed.js library.
Because the output was especially amusing for song lyrics, I connected a version to the RapGenius API.
For Lyric Generator, you enter the name of an artist. The algorithm scrapes the artist’s 10 most popular songs from RapGenius and produces a pseudo-random song.
The GitHub repos for the text and lyric generators are here and here.
Leave a comment