我想构建一个简单的聊天机器人了解某些变量,名称等。例子:bot:你叫什么名字?用户:我叫约翰(用户名(Bot:很高兴认识您John(如您所见,它可以理解名称并在对话中使用(
我的问题是:Chatterbot是否提供此功能?如果是这样,在Chatterbot中使用什么来创建它?
如果您期待拥有了解变量的短语列表,则可以做到这一点:
if 'my name is ' in response.lower():
index=response.lower().find('my name is ')+11 #index of start of name
indexspace=response[index:].find(' ')+index #find index of space after name, indexspace gives result in terms of the index for response[index:], not response itself, so +index
if indexspace==-1:
name=response[index:].replace('.','') #handle 'My name is Joe.' without space at the end
else:
name=response[index:indexspace].replace('.','').replace(',','') #handle 'My name is Joe, nice to meet you!'
print('Nice to meet you, '+name)
else:
print('Hi, what is your name?')
您可以添加更多的替换条款,以增加标点符号和随机响应。您也可以使用不同的声明短语添加更多此类内容。
如果您期待做一些更通用的事情,而没有特殊案例来发表可变声明,我建议您查看聊天机器人的机器学习,这些聊天机器人需要聊天数据和分析以确定声明短语,或者更频繁地提供响应。