Markov: preliminary support for the bot to conditionally shut it self up (and recover from that)

This commit is contained in:
Brian S. Stephan 2011-04-30 15:43:59 -05:00
parent 42d414a0a4
commit 14f2a027fe
1 changed files with 26 additions and 11 deletions

View File

@ -39,6 +39,10 @@ class Markov(Module):
http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/
"""
def timer_interval(self):
"""Do various conversation scoring and infinite reply checks."""
return 30
def __init__(self, irc, config, server):
"""Create the Markov chainer, and learn text from a file if available."""
@ -57,6 +61,8 @@ class Markov(Module):
self.learnre = re.compile(learnpattern)
self.replyre = re.compile(replypattern)
self.shut_up = False
Module.__init__(self, irc, config, server)
def db_init(self):
@ -173,19 +179,20 @@ class Markov(Module):
return self.reply(connection, event, self.markov_train(connection, event, nick, userhost, what, admin_unlocked))
elif self.learnre.search(what):
return self.reply(connection, event, self.markov_learn(connection, event, nick, userhost, what, admin_unlocked))
elif self.replyre.search(what):
elif self.replyre.search(what) and not self.shut_up:
return self.reply(connection, event, self.markov_reply(connection, event, nick, userhost, what, admin_unlocked))
# not a command, so see if i'm being mentioned
if re.search(connection.get_nickname(), what, re.IGNORECASE) is not None:
addressed_pattern = '^' + connection.get_nickname() + '[:,]\s+(.*)'
addressed_re = re.compile(addressed_pattern)
if addressed_re.match(what):
# i was addressed directly, so respond, addressing the speaker
return self.reply(connection, event, '{0:s}: {1:s}'.format(nick, self._generate_line(line=addressed_re.match(what).group(1))))
else:
# i wasn't addressed directly, so just respond
return self.reply(connection, event, '{0:s}'.format(self._generate_line(line=what)))
if not self.shut_up:
# not a command, so see if i'm being mentioned
if re.search(connection.get_nickname(), what, re.IGNORECASE) is not None:
addressed_pattern = '^' + connection.get_nickname() + '[:,]\s+(.*)'
addressed_re = re.compile(addressed_pattern)
if addressed_re.match(what):
# i was addressed directly, so respond, addressing the speaker
return self.reply(connection, event, '{0:s}: {1:s}'.format(nick, self._generate_line(line=addressed_re.match(what).group(1))))
else:
# i wasn't addressed directly, so just respond
return self.reply(connection, event, '{0:s}'.format(self._generate_line(line=what)))
def markov_train(self, connection, event, nick, userhost, what, admin_unlocked):
"""Learn lines from a file. Good for initializing a brain."""
@ -233,6 +240,14 @@ class Markov(Module):
else:
return self._generate_line(min_size=min_size, max_size=max_size)
def timer_do(self):
"""Do various things.
* Check to see if we've been talking too much, and shut up if so.
"""
self.shut_up = True
def _learn_line(self, line, target=None):
"""Create Markov chains from the provided line."""