如何在 Python 中为聊天机器人创建对话?
How to create dialogue for a chat bot in Python?
我想编写一个快速简单的聊天机器人,可以与用户进行对话。我想知道如何创建允许无限数量的 输入 和 响应 的对话。现在我正在使用的代码不允许用户定义的输入。这是我现在正在使用的代码。
# Import the random module
import random
bot_template = "AGENT: {0}"
user_template = "USER: {0}"
# Define variables
name = "Greg"
weather = "cloudy"
# Define a dictionary containing a list of responses for each message
responses = {
"what's your name?": [
"my name is {0}".format(name),
"they call me {0}".format(name),
"I go by {0}".format(name)
],
"what's today's weather?": [
"the weather is {0}".format(weather),
"it's {0} today".format(weather)
],
"default": ["default message"]
}
# Use random.choice() to choose a matching response
def respond(message):
# Check if the message is in the responses
if message in responses:
# Return a random matching response
bot_message = random.choice(responses[message])
else:
# Return a random "default" response
bot_message = random.choice(responses["default"])
return bot_message
# Define a function that sends a message to the bot: send_message
def send_message(message):
# Print user_template including the user_message
print(user_template.format(message))
# Get the bot's response to the message
response = respond(message)
# Print the bot template including the bot's response.
print(bot_template.format(response))
# Send a message to the bot
send_message("what's today's weather?")
使用input() 命令可以获取用户定义的字符串。它 returns 一个字符串,包含用户在 shell 运行 程序中输入的行。
而不是:
# Send a message to the bot
send_message("what's today's weather?")
你可以这样写:
while True:
print('Write your message to the bot and press ENTER')
user_msg = input()
# Send a message to the bot
send_message(user_msg)
这会将用户输入的消息发送到机器人,直到您停止该程序。
我想编写一个快速简单的聊天机器人,可以与用户进行对话。我想知道如何创建允许无限数量的 输入 和 响应 的对话。现在我正在使用的代码不允许用户定义的输入。这是我现在正在使用的代码。
# Import the random module
import random
bot_template = "AGENT: {0}"
user_template = "USER: {0}"
# Define variables
name = "Greg"
weather = "cloudy"
# Define a dictionary containing a list of responses for each message
responses = {
"what's your name?": [
"my name is {0}".format(name),
"they call me {0}".format(name),
"I go by {0}".format(name)
],
"what's today's weather?": [
"the weather is {0}".format(weather),
"it's {0} today".format(weather)
],
"default": ["default message"]
}
# Use random.choice() to choose a matching response
def respond(message):
# Check if the message is in the responses
if message in responses:
# Return a random matching response
bot_message = random.choice(responses[message])
else:
# Return a random "default" response
bot_message = random.choice(responses["default"])
return bot_message
# Define a function that sends a message to the bot: send_message
def send_message(message):
# Print user_template including the user_message
print(user_template.format(message))
# Get the bot's response to the message
response = respond(message)
# Print the bot template including the bot's response.
print(bot_template.format(response))
# Send a message to the bot
send_message("what's today's weather?")
使用input() 命令可以获取用户定义的字符串。它 returns 一个字符串,包含用户在 shell 运行 程序中输入的行。
而不是:
# Send a message to the bot
send_message("what's today's weather?")
你可以这样写:
while True:
print('Write your message to the bot and press ENTER')
user_msg = input()
# Send a message to the bot
send_message(user_msg)
这会将用户输入的消息发送到机器人,直到您停止该程序。