Markov: size -> max_size, since I'm going to try adding a min_size soon

This commit is contained in:
Brian S. Stephan 2011-01-19 18:35:01 -06:00
parent 176ca25c68
commit ac0429569e
1 changed files with 8 additions and 8 deletions

View File

@ -166,18 +166,18 @@ class Markov(Module):
# cap the end of the chain
self.brain.setdefault((w1, w2), []).append(self.stop)
def _reply(self, size=100):
def _reply(self, max_size=100):
"""Generate a totally random string from the chains, of specified limit of words."""
# if the limit is too low, there's nothing to do
if (size <= 3):
raise Exception("size is too small: %d" % size)
if (max_size <= 3):
raise Exception("max_size is too small: %d" % max_size)
# start with an empty chain, and work from there
gen_words = [self.start1, self.start2]
# walk a chain, randomly, building the list of words
while len(gen_words) < size + 2 and gen_words[-1] != self.stop:
while len(gen_words) < max_size + 2 and gen_words[-1] != self.stop:
gen_words.append(random.choice(self.brain[(gen_words[-2], gen_words[-1])]))
# chop off the seed data at the start
@ -189,12 +189,12 @@ class Markov(Module):
return ' '.join(gen_words)
def _reply_to_line(self, line, size=100):
def _reply_to_line(self, line, max_size=100):
"""Reply to a line, using some text in the line as a point in the chain."""
# if the limit is too low, there's nothing to do
if (size <= 3):
raise Exception("size is too small: %d" % size)
if (max_size <= 3):
raise Exception("max_size is too small: %d" % max_size)
# get a random word from the input
words = line.split()
@ -204,7 +204,7 @@ class Markov(Module):
gen_words = [self.start1, self.start2]
# walk a chain, randomly, building the list of words
while len(gen_words) < size + 2 and gen_words[-1] != self.stop:
while len(gen_words) < max_size + 2 and gen_words[-1] != self.stop:
# use the chain that includes the target word, if it is found
if target_word in self.brain[(gen_words[-2], gen_words[-1])]:
gen_words.append(target_word)