I'm trying to make a chat bot, and my main issue is that I can't figure out a way to output a specific response after the program figures out which would be the most suitable.
My initial idea was this:
Check the words in the users input, and compare them against the pre-made responses. The words are split into 2 categorys for this- requiredWords, which if all aren't present in the users input that response is automatically ignored, and possibleWords, which are words that could come up in the users response but don't all have to be there.
For the responses that's required words are all in the users input, calculate the percentage of their possible words also in the users input, and append that as an integer into a list called responses.
Link the integer that is calculated each time (the percentage of possible words present) to the function it corresponds to, then take the integer out of responses which has the biggest value, and output the function it is linked to.
The last point is the part I am struggling to achieve, as I am not even sure if it is possible. I was thinking maybe to use a dictionary to retrieve it, but don't know if they work with functions or integers that change.
This is the code I have made:
import random
import time
import re
responses = []
possibleWords = []
requiredWords = []
recognisedResponse = True
fw = 0 #fownd words
rw = 0 #required words
nw = 0 #needed words
pw = 0 #possible words
userInput = input('You: ')
converted = re.split(r'\s+|[,;?!.-]\s*', userInput.lower())
def checkingInput():
for word in converted:
if word in possibleWords:
fw = fw +1
else:
fw = fw
for word in converted:
if word in requiredWords:
rw = rw +1
else:
rw = rw
RwCalc = int(rw / nw)*100
PwCalc = int(fw / pw)*100
if RwCalc == 100:
recognisedResponse = True
responses.append(PwCalc)
else:
recognisedResponse = False
def howAreYou():
possibleWords = ['how','are','you']
requiredWords = ['how','you']
nw = 2
pw = 3
checkingInput()
print('Bot: I\'m good, thanks for asking!')
time.sleep(1)
print('Bot: How are you?')
def makeAcake():
possibleWords = ['how','do','you','make','a','cake']
requiredWords = ['how','you']
nw = 2
pw = 6
checkingInput()
print('Bot: I have no idea!')
time.sleep(1)
print('Bot: Maybe ask Google instead?')
output = max(responses)
if recognisedResponse == False:
time.sleep(1)
print('Bot: I\'m sorry, I don\'t understand.')
time.sleep(1)
print('Bot: Could you re-phrase that?')
else:
print(output)
Because I don't know how to link the responses to the integer yet, it is only currently supposed to output the integer, however instead I get this error:
line 61, in <module>
output = max(responses)
ValueError: max() arg is an empty sequence
It did this when I first ran it when I'd only made one response, so I made a second response (makeAcake) which used the same required words as the first to see if that was the issue, but it still comes back with the same error.