版权声明:转载请注明出处。 https://blog.csdn.net/Xin_101/article/details/82467041
本文介绍聊天机器人原理及实现傻瓜式聊天机器人。
1 聊天机器人简介
维基百科
聊天机器人(也称为聊天机器,聊天机器人,Bot,IM bot,交互代理或人工对话实体)是通过语音或文本进行对话的计算机程序或人工智能。
百度百科
聊天机器人是一个用来模拟人类对话或聊天的程序。
2 聊天机器人分类
2.1 基于检索匹配模型的机器人
- 根据输入和内容,结合知识库的算法得到相应的回复
- 从固定的数据集或知识库中搜索回复内容
- 检索和匹配的方式较多
- 数据和匹配方式对回复结果的质量影响较大
2.2 基于生成模型的机器人
- 典型的案例有seq2seq+Attention
- 生成的回复结果需要考虑语意及语句通顺程度
3 初级应用
源码1--制定规则,回复指定内容
#-*-coding:utf-8-*-
import random
greetings = ['你好','嗨','嘿','哈喽','您好','好久不见','Hi','Hello','Hey']
random_greeting = random.choice(greetings)
questions = ["最近过得如何?","最近工作怎么样呀?","工作还顺利吗?"]
responses = ['最近过得不错!','又涨工资了,嘿嘿!','很忙,很忙,但是很开心!你呢?','你最近怎么样呀?']
random_response = random.choice(responses)
while True:
question = input('测试爸爸说>')
if question in greetings:
print(random_greeting)
elif question in questions:
print(random_response)
elif question == '再见':
break
else:
print("别逼逼了,我不知道你在说啥!!!!")
源码2--提取关键词,回复指定内容
import nltk
import jieba
import pynlpir
from nltk import word_tokenize
import random
#下载分词器punkt
# nltk.download('punkt')
greetings = ['你好','嗨','嘿','哈喽','您好','好久不见','Hi','Hello','Hey']
random_greeting = random.choice(greetings)
questions = ["如何","怎么样","顺利","工作"]
responses = ['最近过得不错!','又涨工资了,嘿嘿!','很忙,很忙,但是很开心!你呢?','你最近怎么样呀?']
random_response = random.choice(responses)
while True:
question = input('测试爸爸说>')
#jieba分词
cut = jieba.cut(question,cut_all=True)
#分词结果转成字符串格式,generator to string
cut_result = " ".join(cut)
# 字符串转成列表,string to list
cut_cut = cut_result.split()
#提取关键词,英文,不适合中文
# cleaned_input = word_tokenize(question)
# print(type(cleaned_input))
# print(cleaned_input)
#比对关键词,进行回复
if not set(cut_cut).isdisjoint(greetings):
print(random_greeting)
elif not set(cut_cut).isdisjoint(questions):
print(random_response)
elif question == '再见':
break
else:
print("别逼逼了,我不知道你在说啥!!!!")