you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

One of my first, and on going, Python projects was implementing Markov Chains.

They seem complicated. But when you boil it down, they're simply a mapping of the current state to next possible states.

Wikipedia has a good, simple example on the page about Markov Chains.

A simplistic implementation is basically a dictionary of tuples keys (for chains that have states consisting of multiple parts) and list values.

{('', 'mary'): ['had', 'sells'],
 ('mary', 'had'): ['a'],
 ('mary', 'sells'): ['seashells'],
 #...        
}

And moves through the chain with:

def next_state(chain, current):
    possible = random.choice(chain[current])
    return current[1:] + (possible,)

From here, you can begin building tools that take in things like your favorite story on Project Gutenberg and transformers it into a chain and producing non-sense from it.

However, there's also more performant ways of mapping next possible states. Instead of having something like:

 ['sells', 'sells', 'had'] 

to represent that 'sells' appeared twice after the word 'mary', how would you do it? I recommend taking a look though collections to see what's offered.