Higher order Markov chains in Ruby - Ruby Quiz #74
April 11, 2006
A first order chain, for example the following one
is stored as the following hash of hashes
For every key
3 the absolute and relative probabilities are stored.
A higher order chain, for example the following
is also stored as a hash of hashes, but the keys are arrays
Because I am new to Ruby, I had to learn a few things about Arrays,
Objects and dup(). I still do not understand this fully (perhaps it is
to late here 00:46 CET), but in the method
MarkovChain.add() i need to
dup the arrays, otherwise some keys will get overwritten.
By the way, below is an example text I generated for order 3 after I fed 4 books of Chesterton into the chain. Beginning with order 3 I think the text looks like a patchwork or collage of some phrases and sentences of the original texts.
. Up to a certain point it was a wordless clamour startlingly close, and loud enough to be distinct if each word had not killed the other. No, replied Fisher. The Renaissance nobles of the Tudor time were like that. It is awful, I think it goes far enough! said Flambeau; but my information is fragmentary, and only twelve members of the Central Anarchist Council. The gentleman who has for some time past played, with propriety and general applause, the difficult part of Thursday, has died quite suddenly. Consequently, he was willing to go into Parliament as a yeoman or a gentleman or a Jacobite or an Ancient Briton, I should say that, Crane went on. This also tempted him, that in a more partial and also a more premature fashion, for his voice was colourless and sad. What I did next does matter: I gave him a rather wild stare, March had yet another unexpected emotion, for his guide hailed the man as Hoggs and introduced him as Sir Howard Horne, then introducing his so-called Socialist budget, and prepared to expound it in an interview with so promising a penman. Harold March was to have the gold of Glengyle. So far the crime seemed clear enough; and while Boyle was looking into it he heard a thud behind him, and then a sudden stillness, as of a stone statue walking. He might have been firebrands. The mutinies simmered down; the men who must have known that particular house to be so accurately inaccurate. But what makes you think it a specimen. Put the same feather with a ribbon and an artificial flower and everyone will think it a wildcat, though it may have been a clever fellow, answered the priest. As they raced along to the gate out of which poured and rolled, not Roman, but very late, and that this, my successful masquerade, might be of considerable value to the public, placed it here with his own pale blue ones, and said, Do you want me to tell you all you want to guess what he doing, keep behind him. About his life and fortune on the table, and went below the surface in a way that at once puzzled and reassured him. On the oranges was the equally clear and exact description, Finest Brazil nuts, 4d. a lb. M. Brun had a dark, handsome lady, of no little majesty, and rather like a mosaic palace, rent with earthquakes; or like a Dutch tulip garden blown to the stars. The other did not answer; he was free in free London, and drinking
See the rest of the code in the code repository on github.
Remark: This post was adapted to the new blog format in November 2016.