def makeWordModel(filename): # creates a Marko model from words in filename infile = open(filename) model = Markov() for line in infile: words = line.split() for w in words: model.add(w) infile.close() # Add a sentinel at the end of the text model.add(None) model.reset() return model
def makeWordModel(filename): '''creates a Morkov model from the words in the file with filename. pre: The file with name filename exists. post: A Markov chain from the text in the file is returned. ''' # creates a Markov model from words in filename infile = open(filename) tmpmodel = Markov() for line in infile: words = line.split() for w in words: tmpmodel.add(w) infile.close() # Add a sentinel at the end of the text tmpmodel.add(None) tmpmodel.reset() return tmpmodel