0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jasper
  • 17
  • 5
  • There's no need for `else: fw = fw`. Assigning a variable to itself doesn't do anything. – Barmar Aug 10 '23 at 20:27
  • You never call any of the functions. Also, `recognizedResponse` is a local variable inside `checkingInput()`, you can't access it outside without a `global` declaration. – Barmar Aug 10 '23 at 20:29
  • I thought that because I first used it at the beginning of my code outside of any functions it wasn't local? And I know that fw = fw doesn't technically do anything, but didn't know what to put there instead to complete the if/else statement. – Jasper Aug 10 '23 at 20:41
  • No, that's not how variable scope works in Python. And you don't have to "complete" the `if/else`; you can just leave out the entire `else:` if it doesn't need to do anything. – Barmar Aug 10 '23 at 20:49
  • Where would I put the global declaration to make it work then? – Jasper Aug 12 '23 at 09:09
  • See https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Barmar Aug 12 '23 at 19:31

1 Answers1

0

This code defines some functions, but they are never called.

This line of code:

output = max(responses)

is not inside a function, so it runs immediately, and of course responses is still an empty list at that point because no functions have been called, and so you get the error.

Did you intend that code to be part of a function?

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I didn't intend puting it into a function, as in my head that was a part of the main code and the other parts are functions so that they can be called seperately when needed. Would puting it in a function be better? – Jasper Aug 10 '23 at 20:43
  • I thought that if I called the functions it would run all of the responses instead of just the one that matches the user input? How can I alter it to make it work without doing that? – Jasper Aug 10 '23 at 20:45