1
0
mirror of https://github.com/MrDetonia/Maki.git synced 2024-11-22 03:44:18 -05:00

Added KeyError handling when not enough text available, v0.10.1

This commit is contained in:
Zac Herd 2016-04-03 16:25:35 +01:00
parent 360705a04c
commit 0ede83abff
2 changed files with 12 additions and 8 deletions

15
bot.py
View File

@ -24,7 +24,7 @@ from secret import email,pwd
name = "Maki"
# bot version
version = "v0.10.0"
version = "v0.10.1"
# text shown by .help command
helptext = """I am a bot written in Python by MrDetonia
@ -147,8 +147,6 @@ def on_message(message):
# print messages to terminal for info
print(message.author.name + ': ' + message.content)
# ensure we store this user's ID
if message.author.name not in users:
users[message.author.name] = message.author.id
@ -258,10 +256,13 @@ def on_message(message):
elif message.content.startswith('.markov'):
# generate a markov chain sentence based on the user's chat history
tmp = message.content[8:].split(' ',1)
if os.path.isfile('./markovs/' + users[tmp[0]]):
mc = markov.Markov(open('./markovs/' + users[tmp[0]]))
yield from client.send_message(message.channel, mc.generate_text())
tmp = message.content[8:]
if os.path.isfile('./markovs/' + users[tmp]):
mc = markov.Markov(open('./markovs/' + users[tmp]))
try:
yield from client.send_message(message.channel, mc.generate_text())
except KeyError:
yield from client.send_message(message.channel, 'Something went wrong :( Maybe you haven\'t spoken enough yet?')
else:
yield from client.send_message(message.channel, 'I haven\'t seen that user speak yet!')

View File

@ -37,6 +37,9 @@ class Markov(object):
gen_words = []
for i in range(size):
gen_words.append(w1)
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
try:
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
except KeyError:
break
gen_words.append(w2)
return ' '.join(gen_words)