2011-01-18 22:30:59 -06:00
|
|
|
"""
|
|
|
|
Markov - Chatterbot via Markov chains for IRC
|
|
|
|
Copyright (C) 2010 Brian S. Stephan
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-02-07 23:51:41 -06:00
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
"""
|
|
|
|
|
2011-05-01 10:31:20 -05:00
|
|
|
from datetime import datetime
|
2011-01-18 22:30:59 -06:00
|
|
|
import random
|
|
|
|
import re
|
2011-06-20 21:18:55 -05:00
|
|
|
import thread
|
|
|
|
import time
|
2011-01-18 22:30:59 -06:00
|
|
|
|
2013-02-07 23:51:41 -06:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2014-04-05 15:55:33 -05:00
|
|
|
|
2015-05-15 08:36:17 -05:00
|
|
|
import markov.lib as markovlib
|
2014-04-05 15:55:33 -05:00
|
|
|
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
2012-07-27 02:18:01 -05:00
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
from extlib import irclib
|
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
|
|
|
class Markov(Module):
|
|
|
|
|
2013-02-07 23:51:41 -06:00
|
|
|
"""Create a chatterbot very similar to a MegaHAL, but simpler and
|
2011-01-18 22:30:59 -06:00
|
|
|
implemented in pure Python. Proof of concept code from Ape.
|
2011-01-20 14:15:10 -06:00
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
Ape wrote: based on this:
|
|
|
|
http://uswaretech.com/blog/2009/06/pseudo-random-text-markov-chains-python/
|
|
|
|
and this:
|
|
|
|
http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/
|
2013-02-07 23:51:41 -06:00
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
"""
|
2011-01-20 14:15:10 -06:00
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
def __init__(self, irc, config):
|
2013-02-07 23:51:41 -06:00
|
|
|
"""Create the Markov chainer, and learn text from a file if
|
|
|
|
available.
|
|
|
|
|
|
|
|
"""
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
# set up regexes, for replying to specific stuff
|
2011-01-19 10:20:20 -06:00
|
|
|
learnpattern = '^!markov\s+learn\s+(.*)$'
|
2011-01-25 20:25:15 -06:00
|
|
|
replypattern = '^!markov\s+reply(\s+min=(\d+))?(\s+max=(\d+))?(\s+(.*)$|$)'
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
self.learnre = re.compile(learnpattern)
|
|
|
|
self.replyre = re.compile(replypattern)
|
|
|
|
|
2011-04-30 15:43:59 -05:00
|
|
|
self.shut_up = False
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen = []
|
2011-04-30 15:43:59 -05:00
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
Module.__init__(self, irc, config)
|
2011-02-24 20:39:32 -06:00
|
|
|
|
2011-06-20 21:18:55 -05:00
|
|
|
self.next_shut_up_check = 0
|
2011-06-20 22:49:25 -05:00
|
|
|
self.next_chatter_check = 0
|
2011-06-20 21:18:55 -05:00
|
|
|
thread.start_new_thread(self.thread_do, ())
|
|
|
|
|
rewrite recursion/alias code for the 500th time.
more of a moving of the code, actually, it now exists in (an overridden)
_handle_event, so that recursions happen against irc events directly,
rather than an already partially interpreted object.
with this change, modules don't need to implement do() nor do we have a
need for the internal_bus, which was doing an additional walk of the
modules after the irc event was already handled and turned into text. now
the core event handler does the recursion scans.
to support this, we bring back the old replypath trick and use it again,
so we know when to send a privmsg reply and when to return text so that
it may be chained in recursion. this feels old hat by now, but if you
haven't been following along, you should really look at the diff.
that's the meat of the change. the rest is updating modules to use
self.reply() and reimplementing (un)register_handlers where appropriate
2011-02-17 01:08:45 -06:00
|
|
|
def register_handlers(self):
|
2011-01-18 22:30:59 -06:00
|
|
|
"""Handle pubmsg/privmsg, to learn and/or reply to IRC events."""
|
|
|
|
|
2013-02-07 23:51:41 -06:00
|
|
|
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg,
|
|
|
|
self.priority())
|
|
|
|
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg,
|
|
|
|
self.priority())
|
|
|
|
self.irc.server.add_global_handler('pubmsg',
|
|
|
|
self.learn_from_irc_event)
|
|
|
|
self.irc.server.add_global_handler('privmsg',
|
|
|
|
self.learn_from_irc_event)
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
def unregister_handlers(self):
|
2013-02-07 23:51:41 -06:00
|
|
|
self.irc.server.remove_global_handler('pubmsg',
|
|
|
|
self.on_pub_or_privmsg)
|
|
|
|
self.irc.server.remove_global_handler('privmsg',
|
|
|
|
self.on_pub_or_privmsg)
|
|
|
|
self.irc.server.remove_global_handler('pubmsg',
|
|
|
|
self.learn_from_irc_event)
|
|
|
|
self.irc.server.remove_global_handler('privmsg',
|
|
|
|
self.learn_from_irc_event)
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
def learn_from_irc_event(self, connection, event):
|
|
|
|
"""Learn from IRC events."""
|
|
|
|
|
|
|
|
what = ''.join(event.arguments()[0])
|
2011-04-22 19:40:36 -05:00
|
|
|
my_nick = connection.get_nickname()
|
|
|
|
what = re.sub('^' + my_nick + '[:,]\s+', '', what)
|
2011-04-23 16:07:32 -05:00
|
|
|
target = event.target()
|
2011-05-01 10:31:20 -05:00
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
|
2012-03-19 00:12:29 -05:00
|
|
|
if not irclib.is_channel(target):
|
|
|
|
target = nick
|
|
|
|
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen.append((nick, datetime.now()))
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
# don't learn from commands
|
2012-03-19 00:12:29 -05:00
|
|
|
if self.learnre.search(what) or self.replyre.search(what):
|
2011-01-18 22:30:59 -06:00
|
|
|
return
|
|
|
|
|
2014-04-05 15:55:33 -05:00
|
|
|
if not event._recursing:
|
2015-05-15 08:36:17 -05:00
|
|
|
context = markovlib.get_or_create_target_context(target)
|
|
|
|
markovlib.learn_line(what, context)
|
2011-01-18 22:30:59 -06:00
|
|
|
|
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
|
|
|
"""Handle commands and inputs."""
|
|
|
|
|
2011-06-14 22:10:57 -05:00
|
|
|
target = event.target()
|
|
|
|
|
2012-03-19 00:12:29 -05:00
|
|
|
if self.learnre.search(what):
|
2013-02-09 15:05:44 -06:00
|
|
|
return self.irc.reply(event, self.markov_learn(event,
|
2013-02-07 23:51:41 -06:00
|
|
|
nick, userhost, what, admin_unlocked))
|
2011-04-30 15:43:59 -05:00
|
|
|
elif self.replyre.search(what) and not self.shut_up:
|
2013-02-09 15:05:44 -06:00
|
|
|
return self.irc.reply(event, self.markov_reply(event,
|
2013-02-07 23:51:41 -06:00
|
|
|
nick, userhost, what, admin_unlocked))
|
2011-01-18 22:30:59 -06:00
|
|
|
|
2011-04-30 15:43:59 -05:00
|
|
|
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:
|
2015-05-15 08:36:17 -05:00
|
|
|
context = markovlib.get_or_create_target_context(target)
|
2014-04-05 15:55:33 -05:00
|
|
|
|
2011-04-30 15:43:59 -05:00
|
|
|
addressed_pattern = '^' + connection.get_nickname() + '[:,]\s+(.*)'
|
|
|
|
addressed_re = re.compile(addressed_pattern)
|
|
|
|
if addressed_re.match(what):
|
2013-02-07 23:51:41 -06:00
|
|
|
# i was addressed directly, so respond, addressing
|
|
|
|
# the speaker
|
2014-04-05 15:55:33 -05:00
|
|
|
topics = [x for x in addressed_re.match(what).group(1).split(' ') if len(x) >= 3]
|
|
|
|
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
2014-05-03 20:44:52 -05:00
|
|
|
return self.irc.reply(event, u"{0:s}: {1:s}".format(nick,
|
2015-05-15 08:36:17 -05:00
|
|
|
u" ".join(markovlib.generate_line(context, topics=topics, max_sentences=1))))
|
2011-04-30 15:43:59 -05:00
|
|
|
else:
|
|
|
|
# i wasn't addressed directly, so just respond
|
2014-04-05 15:55:33 -05:00
|
|
|
topics = [x for x in what.split(' ') if len(x) >= 3]
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
2014-04-05 15:55:33 -05:00
|
|
|
|
2015-05-15 08:36:17 -05:00
|
|
|
return self.irc.reply(event, u"{0:s}".format(u" ".join(markovlib.generate_line(context,
|
|
|
|
topics=topics,
|
|
|
|
max_sentences=1))))
|
2011-01-18 22:30:59 -06:00
|
|
|
|
2013-02-09 15:05:44 -06:00
|
|
|
def markov_learn(self, event, nick, userhost, what, admin_unlocked):
|
2011-01-18 22:30:59 -06:00
|
|
|
"""Learn one line, as provided to the command."""
|
|
|
|
|
2011-04-23 16:07:32 -05:00
|
|
|
target = event.target()
|
2013-05-03 16:02:07 -05:00
|
|
|
|
|
|
|
if not irclib.is_channel(target):
|
|
|
|
target = nick
|
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
match = self.learnre.search(what)
|
|
|
|
if match:
|
|
|
|
line = match.group(1)
|
2015-05-15 08:36:17 -05:00
|
|
|
context = markovlib.get_or_create_target_context(target)
|
|
|
|
markovlib.learn_line(line, context)
|
2011-01-18 22:30:59 -06:00
|
|
|
|
2011-01-24 16:51:05 -06:00
|
|
|
# return what was learned, for weird chaining purposes
|
|
|
|
return line
|
|
|
|
|
2013-02-09 15:05:44 -06:00
|
|
|
def markov_reply(self, event, nick, userhost, what, admin_unlocked):
|
2011-01-18 22:30:59 -06:00
|
|
|
"""Generate a reply to one line, without learning it."""
|
|
|
|
|
2011-06-14 22:10:57 -05:00
|
|
|
target = event.target()
|
2013-05-03 16:02:07 -05:00
|
|
|
|
|
|
|
if not irclib.is_channel(target):
|
|
|
|
target = nick
|
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
match = self.replyre.search(what)
|
|
|
|
if match:
|
2011-01-25 20:25:15 -06:00
|
|
|
min_size = 15
|
2013-02-09 14:44:45 -06:00
|
|
|
max_size = 30
|
2015-05-15 08:36:17 -05:00
|
|
|
context = markovlib.get_or_create_target_context(target)
|
2011-01-25 20:25:15 -06:00
|
|
|
|
2011-01-18 22:30:59 -06:00
|
|
|
if match.group(2):
|
2011-01-25 20:25:15 -06:00
|
|
|
min_size = int(match.group(2))
|
|
|
|
if match.group(4):
|
|
|
|
max_size = int(match.group(4))
|
|
|
|
|
|
|
|
if match.group(5) != '':
|
|
|
|
line = match.group(6)
|
2014-04-05 15:55:33 -05:00
|
|
|
topics = [x for x in line.split(' ') if len(x) >= 3]
|
|
|
|
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
2015-05-15 08:36:17 -05:00
|
|
|
return u" ".join(markovlib.generate_line(context, topics=topics,
|
|
|
|
min_words=min_size, max_words=max_size,
|
|
|
|
max_sentences=1))
|
2011-01-18 22:30:59 -06:00
|
|
|
else:
|
2011-05-01 10:31:20 -05:00
|
|
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
2015-05-15 08:36:17 -05:00
|
|
|
return u" ".join(markovlib.generate_line(context, min_words=min_size,
|
|
|
|
max_words=max_size,
|
|
|
|
max_sentences=1))
|
2011-01-18 22:30:59 -06:00
|
|
|
|
2011-06-20 21:18:55 -05:00
|
|
|
def thread_do(self):
|
2011-05-01 10:31:20 -05:00
|
|
|
"""Do various things."""
|
|
|
|
|
2011-06-20 21:18:55 -05:00
|
|
|
while not self.is_shutdown:
|
|
|
|
self._do_shut_up_checks()
|
2011-06-20 22:49:25 -05:00
|
|
|
self._do_random_chatter_check()
|
2011-06-20 21:18:55 -05:00
|
|
|
time.sleep(1)
|
2011-05-01 10:31:20 -05:00
|
|
|
|
2011-06-20 22:49:25 -05:00
|
|
|
def _do_random_chatter_check(self):
|
|
|
|
"""Randomly say something to a channel."""
|
|
|
|
|
2014-04-05 15:55:33 -05:00
|
|
|
# TODO: make this do stuff again
|
|
|
|
return
|
2011-06-20 22:49:25 -05:00
|
|
|
|
2011-05-01 10:31:20 -05:00
|
|
|
def _do_shut_up_checks(self):
|
|
|
|
"""Check to see if we've been talking too much, and shut up if so."""
|
|
|
|
|
2011-06-20 21:18:55 -05:00
|
|
|
if self.next_shut_up_check < time.time():
|
|
|
|
self.shut_up = False
|
|
|
|
self.next_shut_up_check = time.time() + 30
|
2011-05-01 10:31:20 -05:00
|
|
|
|
2011-06-20 21:18:55 -05:00
|
|
|
last_30_sec_lines = []
|
2011-04-30 15:43:59 -05:00
|
|
|
|
2013-02-07 23:51:41 -06:00
|
|
|
for (nick, then) in self.lines_seen:
|
2011-06-20 21:18:55 -05:00
|
|
|
rdelta = relativedelta(datetime.now(), then)
|
2012-02-28 23:23:14 -06:00
|
|
|
if (rdelta.years == 0 and rdelta.months == 0 and rdelta.days == 0 and
|
|
|
|
rdelta.hours == 0 and rdelta.minutes == 0 and rdelta.seconds <= 29):
|
2013-02-07 23:51:41 -06:00
|
|
|
last_30_sec_lines.append((nick, then))
|
2011-04-30 15:43:59 -05:00
|
|
|
|
2011-09-20 01:20:27 -05:00
|
|
|
if len(last_30_sec_lines) >= 8:
|
2013-02-07 23:51:41 -06:00
|
|
|
lines_i_said = len(filter(lambda (a, b): a == '.self.said.', last_30_sec_lines))
|
2011-06-20 21:18:55 -05:00
|
|
|
if lines_i_said >= 8:
|
|
|
|
self.shut_up = True
|
|
|
|
targets = self._get_chatter_targets()
|
|
|
|
for t in targets:
|
2013-02-09 15:11:38 -06:00
|
|
|
self.sendmsg(t['target'],
|
2012-02-28 23:23:14 -06:00
|
|
|
'shutting up for 30 seconds due to last 30 seconds of activity')
|