Markov Chain Text Generation
Updated:
When you read about computers generating text, such as a chatbot or AI-generated book/research papers, you might have heard the term Markov Chains. Let’s dissect this concept and use some analogies to demystify it. There are many foreign terms surrounding that make the base concept much more complicated than it really is.
During a hackathon, I played around with the markovify python package and generated Dragon Ball Z subtitles. Before embarking on this project, I had little idea what markov chains were and I needed very little understanding to get this working.
What Is A Markov Chain
A markov chain is:
a stochastic model describing a sequence of possible events in which the probability of each event depends only on the state attained in the previous event
This is a long winded way of saying it’s a state machine that is random:
- it’s a FSM due to the sequencing of events aka moving between states
- it’s stochastic (random and time driven), rather than event-driven
Markov Chain Text Generation
How is this useful for text generation? Languages have grammar constructs that result in patterns.
I (subject) want (verb) salsa verde (direct object) with my tacos (indirect objects)
In English, this is subject-verb-object order. You can’t change this without sounding like Yoda. This means we can find many patterns of I leading to want, and want leading to salsa or tacos.
We can create a finite state machine, where each node represents a word and the next state leads to the next word. To move within the state machine, we do not use events but assign probabilities to each edge. The probability is the frequency of the next word. This is a markov chain text generator.
We can seed the chain by analyzing texts, called a corpus. The more words in the corpus, the more nodes and edges we will have. This will give more pathways when generating text and may produce more fluid sentences.
We can feed it a subset of related texts, to reinforce patterns among domain subjects. In my case, I feed it Dragon Ball Z dialogue, which we all know has a lot of common tropes. This resulted in very successful output and was realistically believable to be actual dialogue from the show.