mirror of
https://github.com/MrDetonia/Maki.git
synced 2024-11-22 03:44:18 -05:00
v0.17.5 markovs back to 3 word chains, respond to .markov Maki
This commit is contained in:
parent
86f54bcbcb
commit
8c4b5cf0e9
29
bot.py
29
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
|
||||
|
19
markov.py
19
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)
|
||||
|
Loading…
Reference in New Issue
Block a user