A Markov Chain class and some fun stuff I did with it
Import the markov class with:
from markov import markov
The first thing you will need to do is create a markov object:
Markov = markov()
To train the markov chain simply use:
Markov.record( previous, current )
previous is the nth item in the sequence. current is the n+1th item in the sequence.
Once the object is trained on all of your data, you simply use:
Markov.predict( current )
current is the nth item in the sequence and this function will return the n+1th item in the sequence or None
if there is no data to base the decision off of.
word-learning.py is a sample program using the markov class designed to learn how to write based on a set of text that it reads. To run this program use python word-learning.py <filename>
where <filename> is the name of the file that contains the learning text. This program will also dump it's object as hidden files and load them back up on each run to save on computational time, if you change your text files make sure you delete the hidden files too.