mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-08 19:22:33 +01:00
Separate class MarkovDB.
This commit is contained in:
parent
0dc52d790a
commit
4352cfde5f
@ -52,75 +52,86 @@ import privmsgs
|
|||||||
import callbacks
|
import callbacks
|
||||||
|
|
||||||
|
|
||||||
class Markov(callbacks.Privmsg):
|
class MarkovDB(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
callbacks.Privmsg.__init__(self)
|
self.dbs = {}
|
||||||
self.dbCache = ircutils.IrcDict()
|
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
for db in self.dbCache:
|
for db in self.dbs.values():
|
||||||
try:
|
try:
|
||||||
db.close()
|
db.close()
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# FIXME: database independency? (All of these private functions)
|
|
||||||
def _getDb(self, channel):
|
def _getDb(self, channel):
|
||||||
channel = channel.lower()
|
channel = channel.lower()
|
||||||
if not channel in self.dbCache:
|
if channel not in self.dbs:
|
||||||
filename = '%s-Markov.db' % channel
|
filename = '%s-Markov.db' % channel
|
||||||
filename = os.path.join(conf.supybot.directories.data(), filename)
|
filename = os.path.join(conf.supybot.directories.data(), filename)
|
||||||
self.dbCache[channel] = anydbm.open(filename, 'c')
|
self.dbs[channel] = anydbm.open(filename, 'c')
|
||||||
return self.dbCache[channel]
|
return self.dbs[channel]
|
||||||
|
|
||||||
def _getNumberOfPairs(self, db):
|
def __getitem__(self, (channel, item)):
|
||||||
# Minus one, because we have a key storing the first pairs.
|
return self._getDb(channel)[item]
|
||||||
return len(db) - 1
|
|
||||||
|
|
||||||
def _getNumberOfFirstPairs(self, db):
|
def __setitem__(self, (channel, item), value):
|
||||||
|
self._getDb(channel)[item] = value
|
||||||
|
|
||||||
|
def getNumberOfPairs(self, channel):
|
||||||
try:
|
try:
|
||||||
pairs = db[''].split()
|
# Minus one, because we have a key storing the first pairs.
|
||||||
|
return len(self[channel.lower()]) - 1
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return 0
|
return 0
|
||||||
return len(pairs)
|
|
||||||
|
|
||||||
def _getFirstPair(self, db):
|
def getNumberOfFirstPairs(self, channel):
|
||||||
try:
|
try:
|
||||||
pairs = db[''].split()
|
return len(self[channel, ''].split())
|
||||||
|
except KeyError:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def getFirstPair(self, channel):
|
||||||
|
try:
|
||||||
|
pairs = self[channel, ''].split()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError('No starting pairs in the database.')
|
raise ValueError('No starting pairs in the database.')
|
||||||
pair = random.choice(pairs)
|
pair = random.choice(pairs)
|
||||||
return pair.split('\x00', 1)
|
return pair.split('\x00', 1)
|
||||||
|
|
||||||
def _getFollower(self, db, first, second):
|
def getFollower(self, channel, first, second):
|
||||||
pair = '%s %s' % (first, second)
|
pair = '%s %s' % (first, second)
|
||||||
try:
|
try:
|
||||||
followers = db[pair].split()
|
followers = self[channel, pair].split()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return '\x00'
|
return '\x00'
|
||||||
return random.choice(followers)
|
return random.choice(followers)
|
||||||
|
|
||||||
def _addFirstPair(self, db, first, second):
|
def addFirstPair(self, channel, first, second):
|
||||||
pair = '%s\x00%s' % (first, second)
|
pair = '%s\x00%s' % (first, second)
|
||||||
try:
|
try:
|
||||||
startingPairs = db['']
|
startingPairs = self[channel, '']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
startingPairs = ''
|
startingPairs = ''
|
||||||
db[''] = '%s%s ' % (startingPairs, pair)
|
self[channel, ''] = '%s%s ' % (startingPairs, pair)
|
||||||
|
|
||||||
def _addPair(self, db, first, second, follower):
|
def addPair(self, channel, first, second, follower):
|
||||||
pair = '%s %s' % (first, second)
|
pair = '%s %s' % (first, second)
|
||||||
try:
|
try:
|
||||||
followers = db[pair]
|
followers = self[channel, pair]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
followers = ''
|
followers = ''
|
||||||
db[pair] = '%s%s ' % (followers, follower)
|
self[channel, pair] = '%s%s ' % (followers, follower)
|
||||||
|
|
||||||
|
|
||||||
|
class Markov(callbacks.Privmsg):
|
||||||
|
def __init__(self):
|
||||||
|
callbacks.Privmsg.__init__(self)
|
||||||
|
self.db = MarkovDB()
|
||||||
|
|
||||||
def doPrivmsg(self, irc, msg):
|
def doPrivmsg(self, irc, msg):
|
||||||
if not ircutils.isChannel(msg.args[0]):
|
if not ircutils.isChannel(msg.args[0]):
|
||||||
return
|
return
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
db = self._getDb(channel)
|
|
||||||
if ircmsgs.isAction(msg):
|
if ircmsgs.isAction(msg):
|
||||||
words = ircmsgs.unAction(msg).split()
|
words = ircmsgs.unAction(msg).split()
|
||||||
words.insert(0, '\x00nick')
|
words.insert(0, '\x00nick')
|
||||||
@ -130,11 +141,11 @@ class Markov(callbacks.Privmsg):
|
|||||||
isFirst = True
|
isFirst = True
|
||||||
for (first, second, follower) in window(words, 3):
|
for (first, second, follower) in window(words, 3):
|
||||||
if isFirst:
|
if isFirst:
|
||||||
self._addFirstPair(db, first, second)
|
self.db.addFirstPair(channel, first, second)
|
||||||
isFirst = False
|
isFirst = False
|
||||||
self._addPair(db, first, second, follower)
|
self.db.addPair(channel, first, second, follower)
|
||||||
if not isFirst: # i.e., if the loop iterated at all.
|
if not isFirst: # i.e., if the loop iterated at all.
|
||||||
self._addPair(db, second, follower, '\x00')
|
self.db.addPair(channel, second, follower, '\x00')
|
||||||
|
|
||||||
_maxMarkovLength = 80
|
_maxMarkovLength = 80
|
||||||
_minMarkovLength = 7
|
_minMarkovLength = 7
|
||||||
@ -146,18 +157,17 @@ class Markov(callbacks.Privmsg):
|
|||||||
channel itself).
|
channel itself).
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
db = self._getDb(channel)
|
|
||||||
try:
|
try:
|
||||||
pair = self._getFirstPair(db)
|
pair = self.db.getFirstPair(channel)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error('I have no records for this channel.')
|
irc.error('I have no records for this channel.')
|
||||||
return
|
return
|
||||||
words = [pair[0], pair[1]]
|
words = [pair[0], pair[1]]
|
||||||
while len(words) < self._maxMarkovLength:
|
while len(words) < self._maxMarkovLength:
|
||||||
follower = self._getFollower(db, words[-2], words[-1])
|
follower = self.db.getFollower(channel, words[-2], words[-1])
|
||||||
if follower == '\x00':
|
if follower == '\x00':
|
||||||
if len(words) < self._minMarkovLength:
|
if len(words) < self._minMarkovLength:
|
||||||
pair = self._getFirstPair(db)
|
pair = self.db.getFirstPair(channel)
|
||||||
words = [pair[0], pair[1]]
|
words = [pair[0], pair[1]]
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
@ -174,8 +184,7 @@ class Markov(callbacks.Privmsg):
|
|||||||
<channel>.
|
<channel>.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
db = self._getDb(channel)
|
n = self.db.getNumberOfPairs(channel)
|
||||||
n = self._getNumberOfPairs(db)
|
|
||||||
s = 'There are %s pairs in my Markov database for %s' % (n, channel)
|
s = 'There are %s pairs in my Markov database for %s' % (n, channel)
|
||||||
irc.reply(s)
|
irc.reply(s)
|
||||||
|
|
||||||
@ -186,8 +195,7 @@ class Markov(callbacks.Privmsg):
|
|||||||
<channel>.
|
<channel>.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
db = self._getDb(channel)
|
n = self.db.getNumberOfFirstPairs(channel)
|
||||||
n = self._getNumberOfFirstPairs(db)
|
|
||||||
s = 'There are %s first pairs in my Markov database for %s'%(n,channel)
|
s = 'There are %s first pairs in my Markov database for %s'%(n,channel)
|
||||||
irc.reply(s)
|
irc.reply(s)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user