1
0
mirror of https://github.com/MrDetonia/Maki.git synced 2024-11-25 20:47:57 -05:00

v0.17.5 markovs back to 3 word chains, respond to .markov Maki

This commit is contained in:
Zac Herd 2016-12-01 22:43:37 +00:00
parent 86f54bcbcb
commit 8c4b5cf0e9
2 changed files with 29 additions and 19 deletions

29
bot.py
View File

@ -31,7 +31,7 @@ from secret import token, lfmkey
# CONFIGURATION # CONFIGURATION
# bot version # bot version
version = "v0.17.4" version = "v0.17.5"
# text shown by .help command # text shown by .help command
helptext = """I am a Discord bot written in Python helptext = """I am a Discord bot written in Python
@ -240,20 +240,23 @@ def on_message(message):
tmp = message.content[8:] tmp = message.content[8:]
target = '' target = ''
# if no user provided, markov the author if tmp == 'Maki':
if tmp == '': response = "My markovs always say the same thing."
target = message.server.id + '-' + message.author.id
else: else:
try: # if no user provided, markov the author
target = message.server.id + '-' + message.server.get_member_named(tmp).id if tmp == '':
except AttributeError: target = message.server.id + '-' + message.author.id
response = "I can't find that user!" else:
try:
target = message.server.id + '-' + message.server.get_member_named(tmp).id
except AttributeError:
response = "I can't find that user!"
if os.path.isfile('./markovs/' + target) and target != '': if os.path.isfile('./markovs/' + target) and target != '':
mc = markov.Markov(open('./markovs/' + target)) mc = markov.Markov(open('./markovs/' + target))
response = mc.generate_text(random.randint(20,40)) response = mc.generate_text(random.randint(20,40))
elif target != '': elif target != '':
response = "I haven't seen them speak yet!" response = "I haven't seen them speak yet!"
elif message.content.startswith('.roll '): elif message.content.startswith('.roll '):
# DnD style dice roll # DnD style dice roll

View File

@ -22,23 +22,30 @@ class Markov(object):
for i in range(len(self.words) - 1): for i in range(len(self.words) - 1):
yield (self.words[i], self.words[i+1]) yield (self.words[i], self.words[i+1])
def triples(self):
if len(self.words) < 3:
return
for i in range(len(self.words) - 2):
yield (self.words[i], self.words[i+1], self.words[i+2])
def database(self): def database(self):
for w1, w2 in self.doubles(): for w1, w2, w3 in self.triples():
key = w1 key = (w1, w2)
if key in self.cache: if key in self.cache:
self.cache[key].append(w2) self.cache[key].append(w3)
else: else:
self.cache[key] = [w2] self.cache[key] = [w3]
def generate_text(self, size=25): def generate_text(self, size=25):
seed = random.randint(0, self.word_size - 2) seed = random.randint(0, self.word_size - 3)
seed_word, next_word = self.words[seed], self.words[seed+1] seed_word, next_word = self.words[seed], self.words[seed+1]
w1, w2 = seed_word, next_word w1, w2 = seed_word, next_word
gen_words = [] gen_words = []
for i in range(size): for i in range(size):
gen_words.append(w1) gen_words.append(w1)
try: try:
w1, w2 = w2, random.choice(self.cache[w2]) w1, w2 = w2, random.choice(self.cache[(w1,w2)])
except KeyError: except KeyError:
break break
gen_words.append(w1) gen_words.append(w1)