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,27 +217,44 @@ 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):
if word1 and word2:
givenArgs = True
words = [word1, word2]
else:
givenArgs = False
try: try:
# words is of the form ['\r', word]
words = list(db.getFirstPair(channel)) words = list(db.getFirstPair(channel))
except KeyError: except KeyError:
irc.error('I don\'t have any first pairs for %s.' % channel) irc.error('I don\'t have any first pairs for %s.' %channel)
return return
# words[-2:] is of the form ('\r', word)
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)
if givenArgs:
irc.reply(' '.join(words))
else:
irc.reply(' '.join(resp)) irc.reply(' '.join(resp))
self.q.enqueue(markov) self.q.enqueue(markov)

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: