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