Limnoria/sandbox/Markov.py

234 lines
7.8 KiB
Python
Raw Normal View History

2004-04-17 15:48:39 +02:00
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# 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.
###
"""
Silently listens to a channel, building a 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.
"""
__revision__ = "$Id$"
import plugins
import anydbm
import random
import os.path
import conf
import world
import ircmsgs
import ircutils
import privmsgs
import callbacks
2004-07-23 12:25:53 +02:00
class MarkovDB(object):
2004-04-17 15:48:39 +02:00
def __init__(self):
2004-07-23 12:25:53 +02:00
self.dbs = {}
2004-04-17 15:48:39 +02:00
def die(self):
2004-07-23 12:25:53 +02:00
for db in self.dbs.values():
2004-04-17 15:48:39 +02:00
try:
db.close()
except:
continue
def _getDb(self, channel):
channel = channel.lower()
2004-07-23 12:25:53 +02:00
if channel not in self.dbs:
2004-04-17 15:48:39 +02:00
filename = '%s-Markov.db' % channel
filename = os.path.join(conf.supybot.directories.data(), filename)
2004-07-23 12:25:53 +02:00
self.dbs[channel] = anydbm.open(filename, 'c')
return self.dbs[channel]
def __getitem__(self, (channel, item)):
return self._getDb(channel)[item]
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def __setitem__(self, (channel, item), value):
self._getDb(channel)[item] = value
def getNumberOfPairs(self, channel):
try:
# Minus one, because we have a key storing the first pairs.
return len(self[channel.lower()]) - 1
except KeyError:
return 0
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def getNumberOfFirstPairs(self, channel):
2004-04-17 15:48:39 +02:00
try:
2004-07-23 12:25:53 +02:00
return len(self[channel, ''].split())
2004-04-17 15:48:39 +02:00
except KeyError:
return 0
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def getFirstPair(self, channel):
2004-04-17 15:48:39 +02:00
try:
2004-07-23 12:25:53 +02:00
pairs = self[channel, ''].split()
2004-04-17 15:48:39 +02:00
except KeyError:
raise ValueError('No starting pairs in the database.')
pair = random.choice(pairs)
return pair.split('\x00', 1)
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def getFollower(self, channel, first, second):
2004-04-17 15:48:39 +02:00
pair = '%s %s' % (first, second)
try:
2004-07-23 12:25:53 +02:00
followers = self[channel, pair].split()
2004-04-17 15:48:39 +02:00
except KeyError:
return '\x00'
return random.choice(followers)
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def addFirstPair(self, channel, first, second):
2004-04-17 15:48:39 +02:00
pair = '%s\x00%s' % (first, second)
try:
2004-07-23 12:25:53 +02:00
startingPairs = self[channel, '']
2004-04-17 15:48:39 +02:00
except KeyError:
startingPairs = ''
2004-07-23 12:25:53 +02:00
self[channel, ''] = '%s%s ' % (startingPairs, pair)
2004-07-21 21:36:35 +02:00
2004-07-23 12:25:53 +02:00
def addPair(self, channel, first, second, follower):
2004-04-17 15:48:39 +02:00
pair = '%s %s' % (first, second)
try:
2004-07-23 12:25:53 +02:00
followers = self[channel, pair]
2004-04-17 15:48:39 +02:00
except KeyError:
followers = ''
2004-07-23 12:25:53 +02:00
self[channel, pair] = '%s%s ' % (followers, follower)
class Markov(callbacks.Privmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
self.db = MarkovDB()
2004-04-17 15:48:39 +02:00
def doPrivmsg(self, irc, msg):
if not ircutils.isChannel(msg.args[0]):
return
channel = msg.args[0]
if ircmsgs.isAction(msg):
words = ircmsgs.unAction(msg).split()
words.insert(0, '\x00nick')
#words.insert(0, msg.nick)
else:
words = msg.args[1].split()
isFirst = True
for (first, second, follower) in window(words, 3):
if isFirst:
2004-07-23 12:25:53 +02:00
self.db.addFirstPair(channel, first, second)
2004-04-17 15:48:39 +02:00
isFirst = False
2004-07-23 12:25:53 +02:00
self.db.addPair(channel, first, second, follower)
2004-04-17 15:48:39 +02:00
if not isFirst: # i.e., if the loop iterated at all.
2004-07-23 12:25:53 +02:00
self.db.addPair(channel, second, follower, '\x00')
2004-04-17 15:48:39 +02:00
_maxMarkovLength = 80
_minMarkovLength = 7
def markov(self, irc, msg, args):
"""[<channel>]
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).
"""
channel = privmsgs.getChannel(msg, args)
try:
2004-07-23 12:25:53 +02:00
pair = self.db.getFirstPair(channel)
2004-04-17 15:48:39 +02:00
except ValueError:
irc.error('I have no records for this channel.')
return
words = [pair[0], pair[1]]
while len(words) < self._maxMarkovLength:
2004-07-23 12:25:53 +02:00
follower = self.db.getFollower(channel, words[-2], words[-1])
2004-04-17 15:48:39 +02:00
if follower == '\x00':
if len(words) < self._minMarkovLength:
2004-07-23 12:25:53 +02:00
pair = self.db.getFirstPair(channel)
2004-04-17 15:48:39 +02:00
words = [pair[0], pair[1]]
else:
break
else:
words.append(follower)
if words[0] == '\x00nick':
words[0] = choice(irc.state.channels[channel].users)
irc.reply(' '.join(words))
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)
2004-07-23 12:25:53 +02:00
n = self.db.getNumberOfPairs(channel)
2004-04-17 15:48:39 +02:00
s = 'There are %s pairs in my Markov database for %s' % (n, channel)
irc.reply(s)
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)
2004-07-23 12:25:53 +02:00
n = self.db.getNumberOfFirstPairs(channel)
2004-04-17 15:48:39 +02:00
s = 'There are %s first pairs in my Markov database for %s'%(n,channel)
irc.reply(s)
# 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)
# db = self._getDb(channel)
# cursor = db.cursor()
# cursor.execute("""SELECT COUNT(*) FROM follows""")
# n = int(cursor.fetchone()[0])
# s = 'There are %s follows in my Markov database for %s' % (n, channel)
# irc.reply(s)
# 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)
# db = self._getDb(channel)
# cursor = db.cursor()
# cursor.execute("""SELECT COUNT(*) FROM follows WHERE word ISNULL""")
# n = int(cursor.fetchone()[0])
# s = 'There are %s lasts in my Markov database for %s' % (n, channel)
# irc.reply(s)
Class = Markov
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: