简体   繁体   English

Chatterbot对话中的变量

[英]Variables in Chatterbot conversations

I would like to build a simple chatbot understanding some variables, names etc. Example: Bot: What is your name? 我想构建一个了解某些变量,名称等的简单聊天机器人。示例:Bot:您叫什么名字? User: My name is John(user's name) Bot: Nice to meet you John(as you can see it understands the name and uses it in conversation) 用户:我叫约翰(用户名)机器人:很高兴认识你约翰(如您所见,它理解名字并在对话中使用它)

My question is: Does the chatterbot provides this feature? 我的问题是:chatterbot是否提供此功能? If so, what to use in chatterbot to create it? 如果是这样,在chatterbot中使用什么来创建它?

If you are looking forward to having a list of phrases that understands variables, you could do this: 如果您希望有一个可以理解变量的短语列表,可以这样做:

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?')

You could add more clauses with replace for more punctuation, and random responses. 您可以添加更多子句,用replace替换更多标点和随机响应。 You could also add more of these with different declaration phrases. 您也可以使用不同的声明短语添加更多这些内容。

If you are looking forward to doing something more general, without special cases for variable declaration I suggest looking at Machine Learning for chatbots, which require chatting data and analysis to determine the declaration phrases, or more often, simply gives an response. 如果您希望做一些更通用的事情,而没有特殊情况下进行变量声明,那么我建议您使用机器学习的聊天机器人,这需要聊天数据和分析来确定声明短语,或更经常地,只是给出响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM