Markov.py
Gareth McCaughan
Gareth.McCaughan at pobox.com
Mon Jan 22 23:40:18 EST 2001
cpsoct at my-deja.com wrote:
> I found the following script in the mac standard distribution (see path
> below). It seems to be Guido's, i am not sure however, i am at a loss
> as to how to use this. I was unable to run it and there is not
> documentation. In otherwords, i put on my detective hat but was still
> unable to get the thing to run. Anyone use this and can explain to me
> how? The script is a bit to big and complex for my level of python kno-
> how, but how sweet would it be to be able to make a markov chain in
> python without having to re-invent the wheel. Particularly since i
> wouldn't be able to in this case!
It looks like a dissociated-press thing to me. You feed it
text; it uses the relative frequencies of tuples of characters,
or words, in the text to build a Markov chain, and then it
runs the Markov chain for a while to produce output text
that has a spooky resemblance to the input text but doesn't
actually make any sense. (Of course, some input texts appear
to be invariant under this treatment...)
Rather than storing successor states with their probabilities,
for each state it associates a list of all the successors that
have been seen. Then, to choose a successor in any given state,
it just picks a random element of the list. So the Markov class
doesn't represent a general Markov chain, just one with rational
probabilities -- and rational probabilities with large denominators
would be prohibitively expensive to implement. :-)
It looks like rather old code, from the fact that it defines
its own "tuple" function and uses string.splitfields instead
of string.split .
As for how to use it: you say something like
python markov.py -3 some-file-of-text
and it thinks for a bit and then starts spewing out text.
Here's some of what it produced when I fed it a chunk of the
Bible. (Readers of delicate sensibility are assured that
there is no blasphemy in what follows...)
1 "Eat you things of thy hard of you, or thing, and which was then the
raim, "David have morit of Kei'lainst go all you shall befor imale cond
with outside one freskin of the to their new whom to the contill deemen
head their fath, Nabouth to him, to younts uncle, sonsecred its by man by
David not can as and from to a dringlease, and hings. 14
That was character-by-character. Here's word-by-word (with the -w option).
1 So these three men ceased to answer Job, because he was too afraid
of his family and the men of the sons of Israel; and Aaron shall take
upon himself any guilt incurred in the holy city were two hundred
pomegranates, in two rows round about; and so with the other held his
weapon.
Does that make sense? (Well, of course the answer is "no", but
I was referring to what *I* wrote...)
--
Gareth McCaughan Gareth.McCaughan at pobox.com
sig under construc
More information about the Python-list
mailing list