Before getting into how to make Markov Chains, lets quickly get a refresh on what a Markov Chain is. A Markov Chain is a set of states and state transition which are selected based on an assigned probability of occurring. The goal of a Markov chain is to model complex dependent statistical systems, for an in-depth introduction to Markov Chains see the post An Introduction to Markov Chains.

The goal of this project is to generate text that approximates the statistical properties of written English text, and to build the text using a Markov Chain working on individual character. For this project I will be using the Python programming language. If you are unfamiliar with Python you can checkout Python.org for more information.

The program will operate in three steps. First, load text to analyze, and perform some initial formatting. Second, step through the formatted text and enumerate the occurrences of each n-gram. Once all n-gram have been counted the frequencies are normalized. Third, an initial seed n-gram is used to start the Markov Chain, and a string of text is generated and returned.

The following pseudocode describes how the Markov Chains will be generated, and how the text is produced from them.

The full python source code is available at the GitHub repository Iprocess-Projects: TextGen v1.1.

Overall this first version is working. Currently the maximum value I can use for the order of the Markov Chain is 5, any thing larger and the algorithm tries to allocate more memory than is installed on my computer. But even with only a fifth order Markov Chain the program is able to produce text where the majority of the works are spelled correctly. For example I trained the algorithm on the book “The Mysterious Island” by Jules Verne, an excerpt from the output of the algorithm is as follows.

times the heart in inflated himself in pencroft nodded to passed that blockhouse pronounced and it. that was not a satisfactor to granite house. they would care oven found damp. the larged. think anything continued. they had nothing the safety. this matter whether ladder. the interval being built by the ruffian. never rested to the he handling on the more cover anything to abandoning several or navigated by a more sprang for this left thus subjectglassmaker side. he west was convey it was still it had extract on ayrton. in the engineer having and the explorers like a sort of new landing his simply for and inspect heights of granite house an appearing centrance which reflection of the nature it even days set of falls the could not have notto the roast time the island had received by a few crater hand for design was soon apparent. its privation. they had not exclaiming magnificent. yes and industry.

Although with low order chains there is almost no relation between the individual words, still the Markov Chain produces text with the same vocabulary as the original even though each character in the text is randomly chosen. And if the order of the Markov Chain could be increased then it could possibly produce legible sentences and readable text, but that would require a major reduction in memory usage. For now it is limited to producing words.