python - 在聊天机器人中实现问题建议
问题描述
我正在考虑在我的常见问题聊天机器人中添加自动问题建议功能。基本上,当用户提出问题时,它不仅会生成正确的响应,而且还会返回用户可能也有兴趣了解的密切相关问题的列表,我如何在当前的聊天机器人中实现该功能?甚至有可能吗?(也许大约 3 到 5 条建议就可以了)
现在,我的聊天机器人能够识别用户消息的“标签”以选择最可能的响应。我希望它能够提供与主题相似的问题列表,或者例如,如果聊天机器人无法识别用户的问题,因为它没有存储在数据库中,它可以建议具有预定义响应的类似问题给它。
我目前正在使用词袋模型来构建我的聊天机器人。
推理模型代码
def bag_of_words(text, vocab):
tokens = clean_text(text)
bow = [0] * len(vocab)
for w in tokens:
for idx, word in enumerate(vocab):
if word == w:
bow[idx] = 1
return np.array(bow)
def pred_class(text, vocab, labels):
bow = bag_of_words(text, vocab)
result = model.predict(np.array([bow]))[0]
error_threshold = 0.2
# filter out predictions below a threshold
y_pred = [[idx, res] for idx, res in enumerate(result) if res > error_threshold]
y_pred.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in y_pred:
return_list.append(labels[r[0]])
return return_list
def get_response(intents_list, intents_json):
if len(intents_list)==0:
result= "Sorry! I don't understand."
else:
tag = intents_list[0]
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 chatbot_response(msg):
stop_condition = False
try:
msg = spell_check(msg)
if msg.lower() == "quit":
stop_condition = True
else:
intents = pred_class(msg, words, classes)
result = get_response(intents, data)
return result
except (KeyboardInterrupt, EOFError, SystemExit):
stop_condition = True
怎么做?
解决方案
推荐阅读
- javascript - 自动分隔和分隔 Google 表格中的字符串
- java - “运行总计返回错误值”
- sql - 选择两个不同行之间的日期差异
- react-native - 当使用 redux 在 react-native 上更改状态时,connect 不会刷新
- java - 即使条件为真,为什么我的方法返回 false?
- javascript - 如何删除网页中的特定类?
- python - 如何检查多对多相关字段是否包含所有给定值
- css - 动态加载外部css资源模型端
- r - 数据有 101 个值 为什么结果有 303 个值?
- sapui5 - Sapui5 路由器在 Master Detail App 中第二次无法正常工作