从零开始升级基于RuleBased的聊天机器人
这里记录从最基础的基于规则的聊天机器人,升级到基于逻辑的机器人,再升级到调用Google提供的API来让机器人能说、会听普通话。
最基本的完全基于规则式的问答:问什么就答什么,幼儿园水平。
import random
# 打招呼greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']# 回复打招呼random_greeting = random.choice(greetings)
# 对于“你怎么样?”这个问题的回复question = ['How are you?','How are you doing?']# “我很好”responses = ['Okay',"I'm fine"]# 随机选一个回random_response = random.choice(responses)
# 机器人跑起来while True: userInput = input(">>> ") if userInput in greetings: print(random_greeting) elif userInput in question: print(random_response) # 除非你说“拜拜” elif userInput == 'bye': break else: print("I did not understand what you said")}
>>> hi hey >>> how are u I did not understand what you said >>> how are you I did not understand what you said >>> how are you? I did not understand what you said >>> How are you? I'm fine >>> bye
完全基于规则的问答会使召回率极低,因为难以列举的问法太多。
升级I: 现在,我们使用关键词匹配来升级我们的机器人。透过关键词来判断这句话的意图是什么(intents)。
from nltk import word_tokenizeimport random
# 打招呼greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']# 回复打招呼random_greeting = random.choice(greetings)
# 对于“假期”的话题关键词question = ['break','holiday','vacation','weekend']# 回复假期话题responses = ['It was nice! I went to Paris',"Sadly, I just stayed at home"]# 随机选一个回random_response = random.choice(responses)
# 机器人跑起来while True: userInput = input(">>> ") # 清理一下输入,看看都有哪些词 cleaned_input = word_tokenize(userInput) # 这里,我们比较一下关键词,确定他属于哪个问题 if not set(cleaned_input).isdisjoint(greetings): print(random_greeting) elif not set(cleaned_input).isdisjoint(question): print(random_response) # 除非你说“拜拜” elif userInput == 'bye': break else: print("I did not understand what you said")
>>> hi hey >>> how was your holiday? It was nice! I went to Paris >>> wow, amazing! I did not understand what you said >>> bye
大概能发现,这依旧是文字层面的“精准对应”。
现在主流的研究方向,是做到语义层面的对应。
比如,“肚子好饿哦”, “饭点到了”都应该表示的是要吃饭了的意思。
在这个层面就需要用到word vector之类的embedding方法,这些内容将是本博客不断更新的重头戏。
升级II : 建立一个简易的知识图谱来存储“知识体系”
# 建立一个基于目标行业的database# 比如 这里我们用python自带的graphgraph = {'上海': ['苏州', '常州'], '苏州': ['常州', '镇江'], '常州': ['镇江'], '镇江': ['常州'], '盐城': ['南通'], '南通': ['常州']}
# 明确如何找到从A到B的路径def find_path(start, end, path=[]): path = path + [start] if start == end: return path if start not in graph: return None for node in graph[start]: if node not in path: newpath = find_path(node, end, path) if newpath: return newpath return Noneprint(find_path('上海', "镇江"))
['上海', '苏州', '常州', '镇江']
使用python版本的prolog:PyKE,它可以构建一种复杂的逻辑网络,让你方便提取信息,而不至于需要你亲手code所有的信息:
son_of(bruce, thomas, norma)
son_of(fred_a, thomas, norma)
son_of(tim, thomas, norma)
daughter_of(vicki, thomas, norma)
daughter_of(jill, thomas, norma)
升级III: 利用Google的API(需FQ)实现文字合成语音,以及语音识别。
from gtts import gTTSimport ostts = gTTS(text='您好,我是您的私人助手,我叫小飞侠', lang='zh-tw')tts.save("hello.mp3")os.system("mpg321 hello.mp3")
同理,除了语音识别,调用Google的接口还可实现语音识别(注意:这里需要你的机器安装几个库 SpeechRecognition, PyAudio 和 PySpeech)
import speech_recognition as srfrom time import ctimeimport timeimport osfrom gtts import gTTSimport sys
# 讲出来AI的话def speak(audioString): print(audioString) tts = gTTS(text=audioString, lang='en') tts.save("audio.mp3") os.system("mpg321 audio.mp3")
# 录下来你讲的话def recordAudio(): # 用麦克风记录下你的话 r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source)
# 用Google API转化音频 data = "" try: data = r.recognize_google(audio) print("You said: " + data) except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError as e: print("Could not request results from Google Speech Recognition service; {0}".format(e))
return data
# 自带的对话技能(rules)def jarvis():
while True:
data = recordAudio()
if "how are you" in data: speak("I am fine")
if "what time is it" in data: speak(ctime())
if "where is" in data: data = data.split(" ") location = data[2] speak("Hold on Tony, I will show you where " + location + " is.") os.system("open -a Safari https://www.google.com/maps/place/" + location + "/&")
if "bye" in data: speak("bye bye") break
# 初始化time.sleep(2)speak("Hi Tony, what can I do for you?")
# 跑起jarvis()
Hi Tony, what can I do for you? You said: how are you I am fine You said: what time is it now Mon Oct 15 18:16:54 2018 You said: where is London Hold on Tony, I will show you where London is. You said: ok bye bye bye bye
转载于:https://www.cnblogs.com/wangliman/p/9795083.html
从零开始升级基于RuleBased的聊天机器人相关推荐
- 从零开始造一个“智障”聊天机器人
腾讯DeepOcean原创文章:dopro.io/nlp_seq2seq- 智能机器人在生活中随处可见:iPhone里会说话的siri.会下棋的阿法狗.调皮可爱的微软小冰--她们都具有一定的智能,能够 ...
- Python基于AIML智能聊天机器人实战(4)- 机器人自学习能力集成
一.前言 本文是<Python基于AIML智能聊天机器人实战>第四篇:AIML自学习能力集成: AIML是智能对话机器人具有里程碑意义的开源项目,曾斩获多项国际大奖,是基于检索技术的闲聊式 ...
- 2Python全栈之路系列之基于socket实现聊天机器人
Python全栈之路系列之基于socket实现聊天机器人 通过socket实现局域网内的聊天工具. service.py文件如下: #!/usr/bin/env python # _*_ coding ...
- 基于“机器学习”智能聊天机器人---python实现(2)
本博文上接上一篇博文"基于"机器学习"智能聊天机器人---python实现(1)" 博文链接:https://blog.csdn.net/DALEONE/art ...
- 基于tensorflow的聊天机器人
** 基于tensorflow的聊天机器人 ** 基于Tensorflow的聊天机器人,主要基于机器深度学习,采用seq2seq+Attention模型,先由jieba中文分词框架对汉字文本语句分词再 ...
- NoneBot2,基于Python的聊天机器人
NoneBot2,基于Python的聊天机器人 第一步,配置python 第二步,配置[go-cqhttp](https://github.com/Mrs4s/go-cqhttp) 第三步,配置[No ...
- Python基于AIML智能聊天机器人实战视频教程-张子良-专题视频课程
Python基于AIML智能聊天机器人实战视频教程-244人已学习 课程介绍 Python基于AIML的智能聊天机器人开发实战课程,包括典型案例.基础理论.功能拓展.源码框架解读四个方 ...
- linux智能聊天机器人,基于bluemix智能聊天机器人开发过程(一)
基于bluemix智能聊天机器人开发过程(一)--入门及地址部署 前期准备工作: 创建bluemix账号 GIT(可选) node.js cf(cloud foundry) bluemix Cli t ...
- 基于pytorch的聊天机器人项目
写一个闲聊式的聊天机器人,类似微软小冰 代码见 https://github.com/skyerhxx/Chatbot 环境 pytorch1.3 python3.7 cuda10.1 注意pytor ...
最新文章
- 冠状病毒如何杀灭最高效?这里有一份几十年的实验汇总
- 皮一皮:P没P图?傻傻分不清...
- Python基础教程(008)--第一个Python程序
- DataGirdView 编辑项时的验证
- 76岁“爷爷考生”第5次备战研究生考试
- linux opencv gtk 没窗口,OpenCV GTK+2.x error
- 删除所有的.svn 文件
- 揪出造成失败用户登录的应用主机名、数据库用户信息
- linux之iptables详解及配置
- HDU 6043 Balala Power! 思维 + 码力
- 创作gtk源码级vim帮助文档 tags
- 一人有限责任公司章程范本
- excel两个字符串相减_Excel小技巧|三种方法计算算式字符串
- 5814:无根树(技巧:树的直径BFS)
- java 实现邮箱验证注册
- 实验验证二项分布(Binomial)公式正确性
- 十七、面相对象的进阶
- C# 调用微信接口上传素材和发送图文消息
- JavaScript中BOM和DOM(持续更新)
- 转——解决游戏王DL的steam版锁区问题