Description
Hello, I am following this tutorial on chatbot with neural net. I would need help with reviewing a response code of "I dont know" response, when the bot is not sure about the answer. Its trained on json file called intents, which contains tags (each tag each conversation topic) like greeting, goodbye, items for sale, etc. Each tag contains questions customers can ask and responses the bot provides.
This is part of the code for the bot and responses (I hope its enough, otherwise i can upload entire code):
def predict_class(sentence, model):
p = bow(sentence, words,show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.6
results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
#sort by strength of probability
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 getResponse(ints, intents_json):
if not ints:
tag = "noanswer"
else:
tag = ints[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
In predict_class there is the ErrorThreshold set for 60%, anything below will not be used. Then in getResponse function I just put the code if not ints then the tag will be "noanswer" - this tag gives responses like "I dont understand" , "I dont know" etc.
Problem
The problem is, that it can filter out totally random questions like "Who is Elon Musk?" the response would be "I dont know", but if I type some gibberish like "fjlsbnljf" or "F*ck you" it still gives me responses. Would you have some idea how to code it in different way, so that if the bot is not sure about the response, or if I type some random letters it would also return to me the "noanswer" tag? I may increase the threshold but then I risk that it would also discard the correct answers