5.2.6. GenerateText

Many modules need text that is no pre-canned, but not truly random either. Markov chains provide a useful way forward for that. Each word is generated based on how likely it is given the word(s) that came before it. Since many modules will need this functionality this module provides a class to do it.

class GenerateText.GenerateText(infile)

Use a MarkovChooser on the given file to generate strings of text based on the transition matrix in it.

Parameters:

  • infile: the source file containing the Markov transition matrix

clear()

Clear the generated string so a call to get_string would return an empty string.

generate(nwords)

Generate a string of the next nwords markov words. This does not return the string. Instead call get_string after. Calling this will overwrite any previous strings.

Parameters:

  • nwords: the number of words to include in the generated string

get_string()

Get the most recently generated string