Limnoria/plugins/Markov.py

293 lines
9.5 KiB
Python
Raw Normal View History

2003-04-11 08:56:37 +02:00
#!/usr/bin/env python
###
# Copyright (c) 2002-2004, Jeremiah Fincher
2003-04-11 08:56:37 +02:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
2003-08-23 14:40:35 +02:00
Silently listens to a channel, building an SQL database of Markov Chains for
later hijinks. To read more about Markov Chains, check out
<http://www.cs.bell-labs.com/cm/cs/pearls/sec153.html>. When the database is
large enough, you can have it make fun little random messages from it.
2003-04-11 08:56:37 +02:00
"""
2003-11-25 09:23:47 +01:00
__revision__ = "$Id$"
2004-07-24 07:18:26 +02:00
import supybot.plugins as plugins
2003-04-11 08:56:37 +02:00
import sets
import Queue
import anydbm
import random
2003-04-11 08:56:37 +02:00
import os.path
import threading
2003-04-11 08:56:37 +02:00
2004-08-25 20:15:09 +02:00
import supybot.world as world
2004-07-24 07:18:26 +02:00
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs
import supybot.callbacks as callbacks
2003-04-11 08:56:37 +02:00
class MarkovDBInterface(object):
def close(self):
pass
2004-08-25 20:15:09 +02:00
def addPair(self, channel, first, second, follower,
isFirst=False, isLast=False):
pass
def getFirstPair(self, channel):
pass
2004-08-25 20:15:09 +02:00
def getPair(self, channel, first, second):
# Returns (follower, last) tuple.
pass
class SqliteMarkovDB(object):
def addPair(self, channel, first, second, follower,
isFirst=False, isLast=False):
pass
def getFirstPair(self, channel):
pass
2004-08-25 20:15:09 +02:00
def getFollower(self, channel, first, second):
# Returns (follower, last) tuple.
pass
2004-08-25 20:15:09 +02:00
class DbmMarkovDB(object):
def __init__(self):
self.dbs = ircutils.IrcDict()
2004-08-25 20:15:09 +02:00
def close(self):
for db in self.dbs.values():
db.close()
def _getDb(self, channel):
if channel not in self.dbs:
# Stupid anydbm seems to append .db to the end of this.
2004-08-17 18:18:59 +02:00
filename = plugins.makeChannelFilename(channel, 'DbmMarkovDB')
# To keep the code simpler for addPair, I decided not to make
# self.dbs[channel]['firsts'] and ['lasts']. Instead, we'll pad
# the words list being sent to addPair such that ['\n \n'] will be
# ['firsts'] and ['\n'] will be ['lasts']. This also means isFirst
# and isLast aren't necessary, but they'll be left alone in case
# one of the other Db formats uses them or someone decides that I
# was wrong and changes my code.
2004-08-17 18:18:59 +02:00
self.dbs[channel] = anydbm.open(filename, 'c')
return self.dbs[channel]
def addPair(self, channel, first, second, follower,
isFirst=False, isLast=False):
db = self._getDb(channel)
combined = self._combine(first, second)
2004-08-25 20:15:09 +02:00
if combined in db: # EW!
db[combined] = ' '.join([db[combined], follower])
else:
2004-08-25 20:15:09 +02:00
db[combined] = follower
if follower == '\n':
if '\n' in db:
db['\n'] = ' '.join([db['\n'], second])
else:
2004-08-25 20:15:09 +02:00
db['\n'] = second
db.sync()
def getFirstPair(self, channel):
db = self._getDb(channel)
firsts = db['\n \n'].split()
if firsts:
firsts.pop() # Empty line.
if firsts:
return ('\n', random.choice(firsts))
else:
raise KeyError, 'No firsts for %s.' % channel
else:
raise KeyError, 'No firsts for %s.' % channel
def _combine(self, first, second):
return '%s %s' % (first, second)
2003-04-11 08:56:37 +02:00
def getFollower(self, channel, first, second):
db = self._getDb(channel)
followers = db[self._combine(first, second)]
follower = random.choice(followers.split())
2004-08-25 20:15:09 +02:00
return (follower, follower == '\n')
def firsts(self, channel):
db = self._getDb(channel)
if '\n \n' in db:
return len(sets.Set(db['\n \n'].split()))
else:
2004-08-25 20:15:09 +02:00
return 0
def lasts(self, channel):
db = self._getDb(channel)
if '\n' in db:
return len(sets.Set(db['\n'].split()))
2004-08-25 20:15:09 +02:00
else:
return 0
def pairs(self, channel):
db = self._getDb(channel)
pairs = [k for k in db.keys() if '\n' not in k]
return len(pairs)
def follows(self, channel):
db = self._getDb(channel)
follows = [len(v.split()) for (k,v) in db.iteritems() if '\n' not in k]
return sum(follows)
def MarkovDB():
return DbmMarkovDB()
class MarkovWorkQueue(threading.Thread):
def __init__(self, *args, **kwargs):
2004-08-18 20:43:03 +02:00
name = 'Thread #%s (MarkovWorkQueue)' % world.threadsSpawned
world.threadsSpawned += 1
threading.Thread.__init__(self, name=name)
self.db = MarkovDB(*args, **kwargs)
self.q = Queue.Queue()
self.killed = False
2004-08-02 02:38:39 +02:00
self.setDaemon(True)
self.start()
def die(self):
self.killed = True
2004-08-17 18:18:59 +02:00
self.q.put(None)
def enqueue(self, f):
self.q.put(f)
def run(self):
while not self.killed:
f = self.q.get()
2004-08-17 18:18:59 +02:00
if f is not None:
f(self.db)
self.db.close()
2004-08-25 20:15:09 +02:00
class Markov(callbacks.Privmsg):
2003-04-11 08:56:37 +02:00
def __init__(self):
self.q = MarkovWorkQueue()
2003-04-11 08:56:37 +02:00
callbacks.Privmsg.__init__(self)
2004-08-25 20:15:09 +02:00
def die(self):
self.q.die()
2004-08-25 20:15:09 +02:00
def tokenize(self, s):
# XXX: Should this be smarter?
return s.split()
2003-04-11 08:56:37 +02:00
def doPrivmsg(self, irc, msg):
channel = msg.args[0]
if ircutils.isChannel(channel):
words = self.tokenize(msg.args[1])
words.insert(0, '\n')
words.insert(0, '\n')
words.append('\n')
2004-08-25 20:15:09 +02:00
def doPrivmsg(db):
for (first, second, follower) in window(words, 3):
db.addPair(channel, first, second, follower)
self.q.enqueue(doPrivmsg)
2003-04-11 08:56:37 +02:00
def markov(self, irc, msg, args):
"""[<channel>]
2003-04-11 08:56:37 +02:00
Returns a randomly-generated Markov Chain generated sentence from the
data kept on <channel> (which is only necessary if not sent in the
channel itself).
2003-04-11 08:56:37 +02:00
"""
channel = privmsgs.getChannel(msg, args)
def markov(db):
try:
words = list(db.getFirstPair(channel))
except KeyError:
irc.error('I don\'t have any first pairs for %s.' % channel)
return
2004-08-25 20:15:09 +02:00
# words[-2:] is of the form ('\r', word)
follower = words[-1]
last = False
2004-08-25 20:15:09 +02:00
resp = []
while not last:
2004-08-25 20:15:09 +02:00
resp.append(follower)
(follower,last) = db.getFollower(channel, words[-2], words[-1])
words.append(follower)
2004-08-25 20:15:09 +02:00
irc.reply(' '.join(resp))
self.q.enqueue(markov)
2003-04-11 23:31:43 +02:00
2004-08-25 20:15:09 +02:00
def firsts(self, irc, msg, args):
"""[<channel>]
Returns the number of Markov's first links in the database for
<channel>.
"""
channel = privmsgs.getChannel(msg, args)
def firsts(db):
s = 'There are %s firsts in my Markov database for %s.'
irc.reply(s % (db.firsts(channel), channel))
self.q.enqueue(firsts)
def lasts(self, irc, msg, args):
"""[<channel>]
Returns the number of Markov's last links in the database for
<channel>.
"""
channel = privmsgs.getChannel(msg, args)
def lasts(db):
s = 'There are %s lasts in my Markov database for %s.'
irc.reply(s % (db.lasts(channel), channel))
self.q.enqueue(lasts)
def pairs(self, irc, msg, args):
"""[<channel>]
Returns the number of Markov's chain links in the database for
<channel>.
"""
channel = privmsgs.getChannel(msg, args)
def pairs(db):
s = 'There are %s pairs in my Markov database for %s.'
irc.reply(s % (db.pairs(channel), channel))
self.q.enqueue(pairs)
def follows(self, irc, msg, args):
"""[<channel>]
Returns the number of Markov's third links in the database for
<channel>.
"""
channel = privmsgs.getChannel(msg, args)
def follows(db):
s = 'There are %s follows in my Markov database for %s.'
irc.reply(s % (db.follows(channel), channel))
self.q.enqueue(follows)
2003-04-11 08:56:37 +02:00
Class = Markov