## quarta-feira, março 01, 2017

### Markov Chains and Hamlet

Lately I've been feeling adventurous and that got me thinking programming-wise. Is it possible to write a play like Hamlet by using Markov Chains?

Yes! There's a cheating way of doing that by using Markov Chain text generator.

It works more or less like this:

1. Take some text as input (e.g., the complete works of Shakespeare).

2. For every distinct word in it, determine what words follow it, and with what frequency.

3. Pick a word to start with -- e.g., choose one at random from all the words that start sentences.

4. Randomly choose a word to follow it, using the frequencies found in step 2.

5. Randomly choose a word to follow that, again using the frequencies from step 2.

6. Carry on in this vein until you reach a predetermined length.

The result can be surprisingly convincing, but can also include utter gibberish.

You get better chances of getting intelligible output by

- increasing the amount of input text;

- in step 2, determining what words follow each distinct pair (or triplet, etc.) of words.

--- The Library is total and ... its shelves contain all the possible combinations of the 20-odd orthographic symbols ... that is, everything which can be expressed, in all languages.” ---

This is exactly what the Internet of the future will be, once the extraterrestrials all over the universe and our, and their, computers fill it up with every conceivable thought, nearer and nearer to what we could call a God mind -- every conceivable novel theory, nonsense, mathematical, chemical or other formula. In short, the intellectual reflection of the universe. And, best if all, with a kick-arse "find" function. We will now it all just by willing to know it.

The digital expansion of pi is conjectured to contain all possible sequences. If so, given some suitable alphanumeric coding (e.g. 01 = A, 02 = B, etc.) pi will contain all possible statements of any length, just like the (infinite) Library of Babel. So it would contain the full text of Hamlet, the full text of Hamlet with the word "Hamlet" replaced throughout by "Larry Grayson", the proof (in French) of the Riemann Hypothesis, a recipe for "Filhoses" (a Portuguese delicatessen) using toenail clippings and earwax, and this comment followed by a thousand other comments saying how marvellous a person I must be for mentioning this. Infinity is a great place to massage ones ego.

And low and behold on wall 3, shelf three, volume 22, page 71, end of line 16 in Hex: