# Of generating random text using a Markov model

Generating text using a markov model is a very old idea. In A mathematical theory of communication (1948), Claude Shannon gives different samples of text generated from offers the same Markov recipe.

The different sample differ by what is considered a token (letters or words) and by the number of precedent tokens taken in account when computing the next token.

1. Letters as token, Markov state of 1 letter

ON IE ANTSOUTINYS ARE T INCTORE ST BE S DEAMY ACHIN D ILONASIVE TUCOOWE AT TEASONARE FUSO TIZIN ANDY TOBE SEACE CTISBE.

2. Letters as token, Markov state of 2 letters

IN NO IST LAT WHEY CRATICT FROURE BIRS GROCID PONDENOME OF DEMONSTURES OF THE REPTAGIN IS REGOACTIONA OF CRE.

3. Word as token, Markov of state of 1 word

THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT THE CHARACTER OF THIS POINT IS THEREFORE ANOTHER METHOD FOR THE LETTERS THAT THE TIME OF WHO EVER TOLD THE PROBLEM FOR AN UNEXPECTED.

1948, just think how cool a hacker he was !

I won’t explain what Markov model are in this post. Claude Shannon actually manages to explain how he generated these text with very simple words. Since computers weren’t exactly ready for text processing at this time, these pieces have actually been generated by manually looking up word iteration in a book. For instance to generate the first sample …

One opens a book at random and selects a letter at random on the page. This letter is recorded. The book is then opened to another page and one reads until this letter is encountered. The succeeding letter is then recorded. Turning to another page this second letter is searched for and the succeeding letter recorded, etc.

To generate sample 2, one would do the same, but looking for consecutive pairs of letters, and to generate sample 3 one would do that with words.

Unfortunately Shannon notes that :

It would be interesting if further approximations could be constructed, but the labor involved becomes enormous at the next stage.

With current computers, it is actually extremely easy to generate such text while considering the two precedent words. In this post I will show an implementation in CoffeeScript. Rather than linearly looking up for occurrence of the two precedent words, we are going to put all alternatives into an index. Writing that kind of code is a lot of fun and I will try to make the code as clear as possible, so that anyone can read it.

The project is available on github and works on both nodeJS and in the browser. It can make a funnier alternative to lorem ipsum for testing your design.

# Demo

In the end, given different famous books as a corpus, the following algorithm generated the following text :

### Pride and Prejudice, Jane Austeen.

I do not. It is very much at Pemberley. The idea of their all walking out. I must acknowledge to you. When I do not marry Mr Collins had promised herself. But you have the carriage might be copied. It would look odd to be proud. You are perfectly good. Elizabeth was at the sight of Miss Darcy was delighted. You have no objection to my charge. I know not. Lydia was urgent with the keenest of all. Mr Collins, you puzzle me exceedingly. But, my proposals will not go. To the rest. But, to much conversation, no traces of them.

### The republic, Plato.

And what do you say of men. Quite true, he replied. Most true, he replied. Yes, he said. Is not the noble Hesiod and the other falls. They will tell you. And not being ambitious they do. Then you see, except at the same. I fear that the ideal of modern thinkers. But this only shows that they may be more likely. For I must be elicited from within. Let us examine this: Is not the same nature. This, which is current among men. For there might be eradicated. Suppose now another form of servitude. Plato is the subject.

### The Hounds of the Baskervilles, Sir Arthur Conan Doyle.

When you know her by sight. No, no doubt, but he’s not a child. It’s a very extraordinary question. We have never in my inferences. Thank you ! Now, Cartwright. It had not gone today it is ? I am. Mortimer has read to us in the Grimpen Mire. Here are twenty-three shillings. It was during these early days to Sherlock Holmes. But, hark, what do you say, Watson. It is Selden, in case I want you again. You know that he confided to anyone. I could look straight through the clouds. Is it, could object.

### Demo time

You can test the algorithm by copy paste a long text below and click on the generate button. Don’t be shy with the length of the text. The longer the better.

Generate Text !

# Implementation

## The tokenizer

First we need a simple tokenizer. A tokenizer, is a function that split our text into a sequence of token. In our case we will want to work on words, so we will want to split on whitespaces and punctuation. We don’t want to get rid of punctuation though.

Regular expressions are the right tool to get a quick-and-dirty version running. A very simple version could be for instance :

The first line surrounds all of the punctuation by white spaces, and the second one splits the text on spaces.

## Python’s defaultdict and counter in CoffeeScript

We will store the transitions in a two level dictionary going

first word -> second word -> third word -> number of appearance


In order to build this dictionary we will recode two very handy structures available in python’s collection, called defaultdict and Counter.

A defaultdict makes it possible to automatically fill a dictionary with a default value when the key requested is not found.

It will make it possible for us to write things such as

Thus avoiding the clumsy check for presence of the key when we first encounter of a word.

## A distribution

We will then have an object representing a simple distribution over an enum.

## The language model

The language is the object in charge of holding the markov model and able to compute a random text. It works by drawing the first two tokens within the beginning two words of the text, and then iteratively adds words randomly until a period is reached. We reject sentences of less than 3 tokens or more than 12 tokens.

## Building up the model

Finally we need to build the model by computing both the trigram distribution and the starting bigram distribution expected by the LanguageModel constructor.

Voilà! Don’t hesitate to fork the project on github and have fun with it.