From 8c4b5cf0e99a484787d38351d064388ffc191402 Mon Sep 17 00:00:00 2001 From: Zac Herd Date: Thu, 1 Dec 2016 22:43:37 +0000 Subject: [PATCH] v0.17.5 markovs back to 3 word chains, respond to .markov Maki --- bot.py | 29 ++++++++++++++++------------- markov.py | 19 +++++++++++++------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/bot.py b/bot.py index f2b6b9f..4200b49 100644 --- a/bot.py +++ b/bot.py @@ -31,7 +31,7 @@ from secret import token, lfmkey # CONFIGURATION # bot version -version = "v0.17.4" +version = "v0.17.5" # text shown by .help command helptext = """I am a Discord bot written in Python @@ -240,20 +240,23 @@ def on_message(message): tmp = message.content[8:] target = '' - # if no user provided, markov the author - if tmp == '': - target = message.server.id + '-' + message.author.id + if tmp == 'Maki': + response = "My markovs always say the same thing." else: - try: - target = message.server.id + '-' + message.server.get_member_named(tmp).id - except AttributeError: - response = "I can't find that user!" + # if no user provided, markov the author + if tmp == '': + target = message.server.id + '-' + message.author.id + 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 != '': - mc = markov.Markov(open('./markovs/' + target)) - response = mc.generate_text(random.randint(20,40)) - elif target != '': - response = "I haven't seen them speak yet!" + if os.path.isfile('./markovs/' + target) and target != '': + mc = markov.Markov(open('./markovs/' + target)) + response = mc.generate_text(random.randint(20,40)) + elif target != '': + response = "I haven't seen them speak yet!" elif message.content.startswith('.roll '): # DnD style dice roll diff --git a/markov.py b/markov.py index a0f5fa1..0227d64 100644 --- a/markov.py +++ b/markov.py @@ -22,23 +22,30 @@ class Markov(object): for i in range(len(self.words) - 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): - for w1, w2 in self.doubles(): - key = w1 + for w1, w2, w3 in self.triples(): + key = (w1, w2) if key in self.cache: - self.cache[key].append(w2) + self.cache[key].append(w3) else: - self.cache[key] = [w2] + self.cache[key] = [w3] 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] w1, w2 = seed_word, next_word gen_words = [] for i in range(size): gen_words.append(w1) try: - w1, w2 = w2, random.choice(self.cache[w2]) + w1, w2 = w2, random.choice(self.cache[(w1,w2)]) except KeyError: break gen_words.append(w1)