markov startwords
This commit is contained in:
+19
-8
@@ -2,6 +2,7 @@ import asyncio
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import numpy as np
|
||||
import random
|
||||
|
||||
class Chatbot(object):
|
||||
"""Een heuse chatbot. Under construction..."""
|
||||
@@ -10,11 +11,12 @@ class Chatbot(object):
|
||||
self.bot = bot
|
||||
self.voice_states = {}
|
||||
self.END_OF_MESSAGE = "<EOM>"
|
||||
self.START_OF_MESSAGE = "<SOM>"
|
||||
try:
|
||||
np.load('markov_dict.npy')
|
||||
except FileNotFoundError:
|
||||
#If there's no initial file, make a new one:
|
||||
initFile = {'pairs':{'Hoi':{self.END_OF_MESSAGE : 1}}, 'trairs':{}}
|
||||
initFile = {'pairs':{self.START_OF_MESSAGE:{'Hoi':1},'Hoi':{self.END_OF_MESSAGE:1}}, 'trairs':{}}
|
||||
np.save("markov_dict.npy", initFile)
|
||||
except:
|
||||
print("ERROR! - Could not open or create Markov chat file")
|
||||
@@ -41,11 +43,10 @@ class Chatbot(object):
|
||||
newPairDict = wordDict['pairs']
|
||||
newTrairDict = wordDict['trairs']
|
||||
|
||||
message = message + " " + self.END_OF_MESSAGE
|
||||
message = self.START_OF_MESSAGE + " " + message + " " + self.END_OF_MESSAGE
|
||||
corpus = message.split()
|
||||
pairs = self.makePairs(corpus)
|
||||
trairs = self.makeTrairs(corpus)
|
||||
print(trairs)
|
||||
|
||||
#Add wordcounts to pair dict
|
||||
for word_1, word_2 in pairs:
|
||||
@@ -84,7 +85,8 @@ class Chatbot(object):
|
||||
iterator = channel.history(limit=nrOfMessages, before=None, after=None, reverse=False, around=None)
|
||||
for i in range(nrOfMessages):
|
||||
msg = yield from iterator.next()
|
||||
messageContent = msg.content.strip()
|
||||
messageContent = msg.content.replace('(', '').replace(')', '').replace('"', '')
|
||||
messageContent = messageContent.strip()
|
||||
if messageContent == "":
|
||||
pass #print("-- Bericht is leeg. Negeer.")
|
||||
elif msg.author.bot:
|
||||
@@ -124,11 +126,17 @@ class Chatbot(object):
|
||||
#print(wordDict)
|
||||
|
||||
#Pick a random first word (Which is not an end-of-message)
|
||||
#TODO pick a word which responds to the previous message in the channel
|
||||
first_word = np.random.choice(list(wordPairDict.keys()))
|
||||
while first_word == self.END_OF_MESSAGE: #or first_word.islower() ???
|
||||
#Usually, we start with a regular <SOM>, but sometimes we don't
|
||||
a = random.randint(1,10)
|
||||
if a == 1:
|
||||
first_word = np.random.choice(list(wordPairDict.keys()))
|
||||
while first_word == self.END_OF_MESSAGE:
|
||||
first_word = np.random.choice(list(wordPairDict.keys()))
|
||||
else:
|
||||
first_word = self.START_OF_MESSAGE
|
||||
chain = [first_word]
|
||||
#TODO pick a word which responds to the previous message in the channel
|
||||
|
||||
|
||||
#Go through the chain:
|
||||
for i in range(1, MAX_N_WORDS):
|
||||
@@ -151,7 +159,10 @@ class Chatbot(object):
|
||||
probs = weights / weights.sum()
|
||||
chain.append(np.random.choice(possible_words, p=probs))
|
||||
|
||||
chain = chain[:-1] #remove <EOM>
|
||||
if chain[0] == self.START_OF_MESSAGE:
|
||||
chain = chain[1:-1] #remove <SOM> and <EOM>
|
||||
else:
|
||||
chain = chain[:-1] #remove <EOM>
|
||||
|
||||
#print(' '.join(chain))
|
||||
yield from ctx.channel.send(' '.join(chain))
|
||||
|
||||
Reference in New Issue
Block a user