<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
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.
def chat(msg): ints = prediction(msg) print(ints[0]) // this here is what is being printed below res = response(ints, intents) return reschat("sd")
{'intent': 'greeting', 'probability': '0.91055113'}
{'intent': 'greeting', 'probability': '0.99985576'}
if probability < 0.91: print("Sorry I don't understand what you mean, please ask me something else") else: # continue ...
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")
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)