Automated text generator using Markov Chain

See this step by step guide on how the algorithm works with reference code provided.

Photo by Thomas Lefebvre on Unsplash

Making computer generated text mimic human speech using Markov Chain is a fascinating and actually not that difficult for an effect that is sometimes convincing, but certainly entertaining. Markov Chains work by generating sentences based on recombination of elements of history of known sentences to generate meaningful sentences.

What’s really interesting, is that you can combine texts from two different people and get a mixed “voice”.

I played around this with texts of speeches from two great presidents:

Image of courtesy of

What my text generator came up with from Obama speeches and Bartlet scripts, is as follows:

  • ‘Can I burn my mother in North Carolina for giving us a great night planned.’
  • ‘And so going forward, I believe that we can build a bomb into their church.’
  • ‘’Charlie, my father had grown up in the Situation Room every time I came in.’’
  • ‘This campaign must be ballistic.’,

Further details and the python source code

You can find a detailed explanation of how it works and also how to create one yourself at site: How to code a Markov Text Generator.

Otherwise, you can find the source code here.

Thanks for reading! If you like what you read, give a clap below so that others may find this (you can also find me on Twitter )

More by Pubs Abayasiri

Topics of interest

More Related Stories