Removed some duplicate code from chatbot
This commit is contained in:
+12
-12
@@ -264,6 +264,14 @@ class Chatbot(commands.Cog):
|
|||||||
response = self.getResponseForMessage(wordsToRespondTo)
|
response = self.getResponseForMessage(wordsToRespondTo)
|
||||||
yield from ctx.channel.send(response)
|
yield from ctx.channel.send(response)
|
||||||
|
|
||||||
|
# Calculate probabilities and pick a next word from a word pair dict:
|
||||||
|
# https://stackoverflow.com/questions/835092/python-dictionary-are-keys-and-values-always-the-same-order
|
||||||
|
def pickNextWord(self, pairPossibilities):
|
||||||
|
possible_words = list(pairPossibilities.keys())
|
||||||
|
weights = np.array(list(pairPossibilities.values()))
|
||||||
|
probs = weights / weights.sum()
|
||||||
|
return np.random.choice(possible_words, p=probs)
|
||||||
|
|
||||||
#Respond to a message.
|
#Respond to a message.
|
||||||
#Assumes the message isn't a command
|
#Assumes the message isn't a command
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@@ -315,12 +323,8 @@ class Chatbot(commands.Cog):
|
|||||||
if word2 in trairPossibilities.keys():
|
if word2 in trairPossibilities.keys():
|
||||||
pairPossibilities[word2] += trairPossibilities[word2]
|
pairPossibilities[word2] += trairPossibilities[word2]
|
||||||
|
|
||||||
#Calculate probabilities and pick a next word:
|
#Pick a next word:
|
||||||
#https://stackoverflow.com/questions/835092/python-dictionary-are-keys-and-values-always-the-same-order
|
chain.append(self.pickNextWord(pairPossibilities))
|
||||||
possible_words = list(pairPossibilities.keys())
|
|
||||||
weights = np.array(list(pairPossibilities.values()))
|
|
||||||
probs = weights / weights.sum()
|
|
||||||
chain.append(np.random.choice(possible_words, p=probs))
|
|
||||||
|
|
||||||
|
|
||||||
if chain[0] == self.START_OF_MESSAGE:
|
if chain[0] == self.START_OF_MESSAGE:
|
||||||
@@ -375,12 +379,8 @@ class Chatbot(commands.Cog):
|
|||||||
if len(pairPossibilities) == 0:
|
if len(pairPossibilities) == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#Calculate probabilities and pick a next word:
|
#Pick a next word:
|
||||||
possible_words = list(pairPossibilities.keys())
|
chain.append(self.pickNextWord(pairPossibilities))
|
||||||
weights = np.array(list(pairPossibilities.values()))
|
|
||||||
probs = weights / weights.sum()
|
|
||||||
chain.append(np.random.choice(possible_words, p=probs))
|
|
||||||
#TODO this is duplicate code. reuse the stuff from getResponseForMessage()
|
|
||||||
|
|
||||||
if self.countSyllables(chain[-1]) == nrOfSyl:
|
if self.countSyllables(chain[-1]) == nrOfSyl:
|
||||||
return chain
|
return chain
|
||||||
|
|||||||
Reference in New Issue
Block a user