我在下面输入
inputs=["Hi","I want to know some information about the course"]
我在这里有回音
responses=["Hi,How can I help you?","Which course do you want to take?"]
我想基于输入长度创建一个聊天机器人,它应该给出相应的响应,即,如果用户输入"Hi"
,它必须比较输入列表中的长度和单词并从响应列表中给出相应的响应。
我尝试过如下
### splitting list elements as words
user_word_split=[word for line in inputs for word in str(line).split()]
### Asking user for enter their text
while True:
user=input('User:')
#### seeing if user text present in the existing list
for i in range(0,len(user_word_split)):
commonElements=[x for x in (user_word_split) if x in user]
#### comparing the length of the inputs and giving responses if condition meets.
if len(user)>=len(commonElements):
user=input(responses[0])
elif len(user)>=len(commonElements):
user=input(responses[1])
输出:
User:Hi
chatbot: Hi,How can i help you?
即使我输入"go"
作为用户文本,它也会给出响应。它不是比较输入列表中存在的单词。它只是比较长度。我如何实现这一点。如果user text length >= len(commonElements)
必须从响应列表中获取第一响应,依此类推。但是无论何时使用词比较,我每次都仅从响应列表中获取第一响应。
预期输出:
User: Hi
chatbot: Hi,How can I help you?
User: I want to know some information aout the course
chatbot: Which course do you want to take?