基于python的对话机器人bot项目开发(一)
什么是bot?
bot是代替人进行某种工作的程序。这个词来源于机器人(ROBOT)。bot可以做各种各样的工作,例如,为Google等搜索引擎搜集 页面信息的bot、在Twitter上发言的bot、进行股票买卖的bot等。这次制作的是与人对话的 bot,也被称为聊天机器人。它可以对其他人的发言自动做出某种回答。
对话bot是什么?
对话bot(chatbot)是近年来非常流行的技术。LINE、Facebook messenger等聊天服务提供了用于创建对适的API(提供可以从程序中使用的指令等)。并推动bot的开发。以本书中制作的对适bot为基础,还可以制作与LINE或Facebook messenger上的人对话的bot。比较有名的对话bot还有微软制作的“小冰”。不过,这次制作的bot程序并不是使用“临门一脚”那样的AI(人工智能),而是对特定的关键词进行应答的简单程序。
要点 :通过服务器传递消息(API)
智能手机上的消息应用程度和bot程序进行对话时,通常会通过服务器发送消息。
本书为了简化程序,不通过服务器,直接以人与bot程序交互的形式进行。实际上,bot程序要想与智能手机交互。据必须根据各种消息应用程序(LNE.Farahnnk meesenger等)等公开的API制作程序。
制作简单的对话bot
制作pybot
pybot.py
while True:
command = input('pybot> ')
print(command)
运行:
pybot> 你好
你好
制作一个简单问候的bot
基于in运算符的字符串部分匹配
pybot.py
while True:
command = input('pybot> ')
if '你好' in command:
print('我很好')
elif '谢谢' in command:
print('不客气')
elif '再见' in command:
print('拜拜')
break
else:
print('我不明白你在说什么')
运行:
pybot> 你好
我很好
pybot> 谢谢
不客气
pybot> 再见
拜拜
pybot> 牛排
我不明白你在说什么
把问候语改成便于编辑的字典数据
bot_dict = {
'你好': '我很好',
'谢谢': '不客气',
'再见': '拜拜',
}
while True:
command = input('pybot> ')
response = ''
for message in bot_dict:
if message in command:
response = bot_dict[message]
break
if not response:
response = '我不明白你在说什么'
print(response)
if '再见' in command:
break
运行:
pybot> 你好
我很好
pybot> 谢谢
不客气
pybot> 再见
拜拜
pybot> 牛排
我不明白你在说什么
从文件中读取问候数据
pybot.txt:
你好,我很好
谢谢,不客气
再见,拜拜
command_file = open('pybot.txt', encoding='utf-8')
raw_data = command_file.read()
command_file.close()
lines = raw_data.splitlines()
bot_dict = {}
for line in lines:
word_list = line.split(',')
key = word_list[0]
response = word_list[1]
bot_dict[key] = response
while True:
command = input('pybot> ')
response = ''
for message in bot_dict:
if message in command:
response = bot_dict[message]
break
if not response:
response = '不知道'
print(response)
if '停止' in command:
break
txt文件,与py文件放在同一目录下。
运行:
pybot> 你好
我很好
pybot> 谢谢
不客气
pybot> 再见
拜拜
pybot> 牛排
不知道
pybot> 停止
不知道
创建用于计算的命令
pybot.txt:
你好,我很好
谢谢,不客气
再见,拜拜
command_file = open('pybot.txt', encoding='utf-8')
raw_data = command_file.read()
command_file.close()
lines = raw_data.splitlines()
bot_dict = {}
for line in lines:
word_list = line.split(',')
key = word_list[0]
response = word_list[1]
bot_dict[key] = response
while True:
command = input('pybot> ')
response = ''
for message in bot_dict:
if message in command:
response = bot_dict[message]
break
if '和历' in command:
wareki, year_str = command.split()
year = int(year_str)
if year >= 2019:
reiwa = year - 2018
response = f'公元{year}年、令和{reiwa}年'
elif year >= 1989:
heisei = year - 1988
response = f'公元{year}年、平成{heisei}年'
else:
response = f'公元{year}年、平成前的时代'
if not response:
response = '我不知道你在说什么'
print(response)
if '再见' in command:
break
运行:
pybot> 和历 2020
公元2020年、令和2年
pybot> 和历 2000
公元2000年、平成12年
pybot> 和历 1980
公元1980年、平成前的时代
pybot> 再见
拜拜
将计算指令的处理操作集中在一起
pybot.txt:
你好,我很好
谢谢,不客气
再见,拜拜
def wareki_command(command):
wareki, year_str = command.split()
year = int(year_str)
if year >= 2019:
reiwa = year - 2018
response = f'公元{year}年、令和{reiwa}年'
elif year >= 1989:
heisei = year - 1988
response = f'公元{year}年、平成{heisei}年'
else:
response = f'公元{year}年、平成前的时代'
return response
command_file = open('pybot.txt', encoding='utf-8')
raw_data = command_file.read()
command_file.close()
lines = raw_data.splitlines()
bot_dict = {}
for line in lines:
word_list = line.split(',')
key = word_list[0]
response = word_list[1]
bot_dict[key] = response
while True:
command = input('pybot> ')
response = ''
for message in bot_dict:
if message in command:
response = bot_dict[message]
break
if '和历' in command:
response = wareki_command(command)
if not response:
response = '我不知道你在说什么'
print(response)
if '再见' in command:
break
运行:
pybot> 和历 2023
公元2023年、令和5年
pybot> 和历 1999
公元1999年、平成11年
pybot> 和历 1988
公元1988年、平成前的时代
pybot> 牛排
我不知道你在说什么
pybot> 再见
拜拜
学习内置函数
def len_command(command):
cmd, text = command.split()
length = len(text)
response = f'字符串的长度为{length} '
return response
def wareki_command(command):
wareki, year_str = command.split()
year = int(year_str)
if year >= 2019:
reiwa = year - 2018
response = f'公元{year}年、令和{reiwa}年'
elif year >= 1989:
heisei = year - 1988
response = f'公元{year}年、平成{heisei}年'
else:
response = f'公元{year}年、平成前的时代'
return response
command_file = open('pybot.txt', encoding='utf-8')
raw_data = command_file.read()
command_file.close()
lines = raw_data.splitlines()
bot_dict = {}
for line in lines:
word_list = line.split(',')
key = word_list[0]
response = word_list[1]
bot_dict[key] = response
while True:
command = input('pybot> ')
response = ''
for message in bot_dict:
if message in command:
response = bot_dict[message]
break
if '和历' in command:
response = wareki_command(command)
if '长度' in command:
response = len_command(command)
if not response:
response = '我比知道你在说什么'
print(response)
if '再见' in command:
break
运行:
pybot> 长度 cghjcfdfyjghcfsfgkhvfdsesfv
字符串的长度为27
pybot> 长度 干啥测高仪复合肥很舒服个
字符串的长度为12
pybot> 牛排
我比知道你在说什么
pybot> 再见
拜拜