Maak een praatje met Stroop. Als je eenzaam bent ofzo.

This commit is contained in:
JP
2018-07-07 18:44:46 +02:00
parent db3a87e568
commit 1a69062640
+48 -12
View File
@@ -20,10 +20,32 @@ class Chatbot(object):
np.save("markov_dict.npy", initFile) np.save("markov_dict.npy", initFile)
except: except:
print("ERROR! - Could not open or create Markov chat file") print("ERROR! - Could not open or create Markov chat file")
#Do stuff when a message comes in:
@self.bot.event
@asyncio.coroutine
def on_message(message):
if message.author.bot:
return
if message.content.strip()[0] != '!': #don't respond to a command with a chat
if message.content.lower() == "nihao":
yield from message.channel.send("kankerlauw")
return
if "DMChannel" in type(message.channel).__name__: #respond to messages in PM channels
yield from self.respondToMessage(message)
else:
pass #TODO respond to other channels at random
#process commands:
yield from self.bot.process_commands(message)
def __check(self, ctx): def __check(self, ctx):
return True return True
#TODO laat Stroop reageren op persoonlijke berichten
#Make word pairs: #Make word pairs:
def makePairs(self, corpus): def makePairs(self, corpus):
@@ -110,7 +132,6 @@ class Chatbot(object):
@commands.command(pass_context=True, hidden=True) @commands.command(pass_context=True, hidden=True)
@asyncio.coroutine @asyncio.coroutine
def trainmarkovchat(self, ctx, channelId=409751773595566081, nrOfMessages=50): def trainmarkovchat(self, ctx, channelId=409751773595566081, nrOfMessages=50):
@@ -176,15 +197,6 @@ class Chatbot(object):
Stroop leert van onze gesprekken. Hij kan zich nu voordoen als een van ons. Soort van. Stroop leert van onze gesprekken. Hij kan zich nu voordoen als een van ons. Soort van.
""" """
MAX_N_WORDS = 100 #Don't use more words than this
#Get dictionary/Markov chain
wordDict = np.load('markov_dict.npy').item()
wordPairDict = wordDict['pairs']
wordTrairDict = wordDict['trairs']
responsePairDict = wordDict['responsePairs']
#TODO store in global variable to minimise IO?
#Get previous message to respond to #Get previous message to respond to
messageIterator = ctx.channel.history(limit=3, before=None, after=None, reverse=False, around=None) messageIterator = ctx.channel.history(limit=3, before=None, after=None, reverse=False, around=None)
messageToRespondTo = yield from messageIterator.next() #this will be the !reageer command. skip messageToRespondTo = yield from messageIterator.next() #this will be the !reageer command. skip
@@ -203,6 +215,29 @@ class Chatbot(object):
except: except:
pass pass
response = self.getResponseForMessage(wordsToRespondTo)
yield from ctx.channel.send(response)
#Respond to a message.
#Assumes the message isn't a command
def respondToMessage(self, message):
wordsToRespondTo = message.content.strip().split()
response = self.getResponseForMessage(wordsToRespondTo)
yield from message.channel.send(response)
#Gets a response
def getResponseForMessage(self, wordsToRespondTo):
MAX_N_WORDS = 100 #Don't use more words than this
#Get dictionary/Markov chain
wordDict = np.load('markov_dict.npy').item()
wordPairDict = wordDict['pairs']
wordTrairDict = wordDict['trairs']
responsePairDict = wordDict['responsePairs']
#TODO store in global variable to minimise IO?
#Wtart with <SOM> #Wtart with <SOM>
chain = [self.START_OF_MESSAGE] chain = [self.START_OF_MESSAGE]
@@ -250,5 +285,6 @@ class Chatbot(object):
chain[n] = "@jemoeder" chain[n] = "@jemoeder"
#print(' '.join(chain)) #print(' '.join(chain))
yield from ctx.channel.send(' '.join(chain)) #yield from ctx.channel.send(' '.join(chain))
return ' '.join(chain)