Added an optional pair to start the chain, tests, and caught some errors.

This commit is contained in:
James Vega 2004-08-27 06:34:48 +00:00
parent 22c333b536
commit 29a6c92b76
2 changed files with 34 additions and 13 deletions

View File

@ -122,7 +122,6 @@ class DbmMarkovDB(object):
db = self._getDb(channel) db = self._getDb(channel)
firsts = db['\n \n'].split() firsts = db['\n \n'].split()
if firsts: if firsts:
firsts.pop() # Empty line.
if firsts: if firsts:
return ('\n', random.choice(firsts)) return ('\n', random.choice(firsts))
else: else:
@ -218,28 +217,45 @@ class Markov(callbacks.Privmsg):
self.q.enqueue(doPrivmsg) self.q.enqueue(doPrivmsg)
def markov(self, irc, msg, args): def markov(self, irc, msg, args):
"""[<channel>] """[<channel>] [word1 word2]
Returns a randomly-generated Markov Chain generated sentence from the Returns a randomly-generated Markov Chain generated sentence from the
data kept on <channel> (which is only necessary if not sent in the data kept on <channel> (which is only necessary if not sent in the
channel itself). channel itself). If word1 and word2 are specified, they will be used
to start the Markov chain.
""" """
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
(word1, word2) = privmsgs.getArgs(args, required=0, optional=2)
def markov(db): def markov(db):
try: if word1 and word2:
words = list(db.getFirstPair(channel)) givenArgs = True
except KeyError: words = [word1, word2]
irc.error('I don\'t have any first pairs for %s.' % channel) else:
return givenArgs = False
# words[-2:] is of the form ('\r', word) try:
# words is of the form ['\r', word]
words = list(db.getFirstPair(channel))
except KeyError:
irc.error('I don\'t have any first pairs for %s.' %channel)
return
follower = words[-1] follower = words[-1]
last = False last = False
resp = [] resp = []
while not last: while not last:
resp.append(follower) resp.append(follower)
(follower,last) = db.getFollower(channel, words[-2], words[-1]) try:
(follower,last) = db.getFollower(channel, words[-2],
words[-1])
except KeyError:
irc.error('I found a broken link in the Markov chain. '
'Maybe I received two bad links to start the '
'chain.')
return
words.append(follower) words.append(follower)
irc.reply(' '.join(resp)) if givenArgs:
irc.reply(' '.join(words))
else:
irc.reply(' '.join(resp))
self.q.enqueue(markov) self.q.enqueue(markov)
def firsts(self, irc, msg, args): def firsts(self, irc, msg, args):

View File

@ -37,9 +37,14 @@ except ImportError:
sqlite = None sqlite = None
if sqlite is not None: if sqlite is not None:
class MarkovTestCase(PluginTestCase, PluginDocumentation): class MarkovTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('Markov',) plugins = ('Markov',)
pass # Put actual tests here. def testMarkov(self):
self.assertSnarfNoResponse('Feed the db some text')
self.assertNotError('markov')
self.assertNotError('markov Feed the')
self.assertNotError('markov Feed')
self.assertError('markov foo bar')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: