Add plugins.Markov.minChainLength and plugins.Markov.maxAttempts which

should allow for interesting Markov chains (unless you like the one-worders).
This commit is contained in:
James Vega 2004-11-11 18:50:21 +00:00
parent ed2488c1f8
commit 611149aadc

View File

@ -78,6 +78,13 @@ conf.registerChannelValue(conf.supybot.plugins.Markov.randomSpeaking,
conf.registerChannelValue(conf.supybot.plugins.Markov.randomSpeaking, conf.registerChannelValue(conf.supybot.plugins.Markov.randomSpeaking,
'throttleTime', registry.PositiveInteger(300, """Determines the minimum 'throttleTime', registry.PositiveInteger(300, """Determines the minimum
number of seconds between the bot randomly speaking.""")) number of seconds between the bot randomly speaking."""))
conf.registerChannelValue(conf.supybot.plugins.Markov, 'minChainLength',
registry.PositiveInteger(1, """Determines the length of the smallest chain
which the markov command will generate."""))
conf.registerChannelValue(conf.supybot.plugins.Markov, 'maxAttempts',
registry.PositiveInteger(1, """Determines the maximum number of times the
bot will attempt to generate a chain that meets or exceeds the size set in
minChainLength."""))
class MarkovDBInterface(object): class MarkovDBInterface(object):
def close(self): def close(self):
@ -323,35 +330,50 @@ class Markov(callbacks.Privmsg):
def _markov(self, channel, irc, word1=None, word2=None, **kwargs): def _markov(self, channel, irc, word1=None, word2=None, **kwargs):
def f(db): def f(db):
if word1 and word2: minLength = self.registryValue('minChainLength', channel)
givenArgs = True maxTries = self.registryValue('maxAttempts', channel)
words = [word1, word2] while maxTries > 0:
else: maxTries -= 1;
givenArgs = False if word1 and word2:
try: givenArgs = True
# words is of the form ['\r', word] words = [word1, word2]
words = list(db.getFirstPair(channel)) else:
except KeyError: givenArgs = False
irc.error('I don\'t have any first pairs for %s.' %channel) try:
return # words is of the form ['\r', word]
follower = words[-1] words = list(db.getFirstPair(channel))
last = False except KeyError:
resp = [] irc.error('I don\'t have any first pairs for %s.' %
while not last: channel)
resp.append(follower) return
try: follower = words[-1]
(follower,last) = db.getFollower(channel, words[-2], last = False
words[-1]) resp = []
except KeyError: while not last:
irc.error('I found a broken link in the Markov chain. ' resp.append(follower)
'Maybe I received two bad links to start the ' try:
'chain.') (follower,last) = db.getFollower(channel, words[-2],
return words[-1])
words.append(follower) except KeyError:
if givenArgs: irc.error('I found a broken link in the Markov chain. '
irc.reply(' '.join(words[:-1]), **kwargs) ' Maybe I received two bad links to start '
else: 'the chain.')
irc.reply(' '.join(resp), **kwargs) return
words.append(follower)
if givenArgs:
if len(words[:-1]) >= minLength:
irc.reply(' '.join(words[:-1]), **kwargs)
return
else:
continue
else:
if len(resp) >= minLength:
irc.reply(' '.join(resp), **kwargs)
return
else:
continue
irc.error('I was unable to generate a Markov chain at least %s '
'long.' % utils.nItems('word', minLength))
return f return f
def markov(self, irc, msg, args, channel, word1, word2): def markov(self, irc, msg, args, channel, word1, word2):