Markov-chain text generator

This is an example taken from The Practice of Programming, by Brian W. Kernighan and Rob Pike.

Basically we treat a text as a string of words generated from a Markov chain. We then learn the rules and try to generate random sentenses from them. The authors already presented several implementations (beautifully coded) in C/C++/Java/Awk/Perl.

Javascript demonstration

Copy and paste the learning text to the box below.

words. Markov chain order:

Output:

Some example text:

† The script can handle Asian text as well.

Source Code

The source code of the above Javascript markov.js.

A similar Python script markov.py.