Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm currently making a chatbot using tensorflow and keras. What i want is to send the user a message if the pattern cannot be found within the intents.json file.

I've tried to implement an if-else statement in the results variable, but its not letting me. Does anyone have any suggestions? Any help would be much appreciated!


<pre>def bag_of_words(sentence):
    # tokenize the pattern
    sentence_words = clean_sentence(sentence)
    # bag of words - matrix of N words, vocabulary matrix
    bag = [0] * len(words)
    for s in sentence_words:
        for i, word in enumerate(words):
            if word == s:
                bag[i] = 1
    return np.array(bag)


def prediction(sentence):
    # filter out predictions below a threshold
    bow = bag_of_words(sentence)
    res = model.predict(np.array([bow]))[0]
    ERROR_THRESHOLD = 0.75
    results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]

    results.sort(key=lambda x: x[1], reverse=True)
    
    return_list = []
    for r in results:
        return_list.append({"intent": classes[r[0]], 'probability': str(r[1])})
    return return_list


def response(intents_list, intents_json):
    tag = intents_list[0]['intent']
    list_of_intents = intents_json['intents']
    
    for i in list_of_intents:
        if i['tag'] == tag:
            result = random.choice(i['responses'])
            break  
    return result


def chat(msg):
    ints = prediction(msg)
    
    print(ints[0])

    res = response(ints, intents)
    return res


What I have tried:

I've tried to implement an if-else statement in the results variable, so that i can try code an output to r < ERROR THRESHOLD but its giving me an error.
Posted
Updated 20-Jul-21 5:56am
v4
Comments
Richard MacCutchan 20-Jul-21 7:28am    
Do you think it might be helpful if you explained exactly where you are implementing if/else, what are the actual values you are trying to compare, and what results you get? And if you get an error, please tell us what error and where it occurs. Leaving us to guess what happens when you build or run your code leaves us very much in the dark.
Socks92 20-Jul-21 11:20am    
Hi Richard, apologies. Right so im testing it all at the moment, i have a chat function (can be seen below)


Copy Code
def chat(msg):     ints = prediction(msg)        print(ints[0])    // this here is what is being printed below    res = response(ints, intents)    return reschat("sd")



here when typing "sd" into the function which is NOT in the JSON responses its giving me the following output:

Copy Code
{'intent': 'greeting', 'probability': '0.91055113'}



when I input a word that IS within the responses e.g "hello" it gives me this output:

Copy Code
{'intent': 'greeting', 'probability': '0.99985576'}


so here we can see the probability is higher when typing a response within my JSON file.

What i need to do is basically say if the 'probability' is less than 0.91' then send a message back to the user saying "Sorry i don't understand what you mean, please ask me something else" however if the probability is higher than 0.91 then the chatbot can respond from the intents provided.


I'm just really unsure about how i would implement it
Richard MacCutchan 20-Jul-21 11:37am    
Something like ...
if probability < 0.91:
    print("Sorry I don't understand what you mean, please ask me something else")
else:
    # continue ...
Socks92 20-Jul-21 11:53am    
Thank you for the prompt reply. I know that i need to code an if statement - im guessing in the response function. I'm just not entirely sure on how to access the 'probability' value inside of the ints variable:

{'intent': 'greeting', 'probability': '0.99985576'}

EDIT: no worries i figured it out :)
Richard MacCutchan 20-Jul-21 12:00pm    
How about:
    probability = float(ints['probability'])
    if probability < 0.91:
        print("Sorry I don't understand what you mean, please ask me something else")
    else:
        print("A good question")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900