我想为自定义 Alexa 技能、问答项目随机化 Python 中的响应
I want to randomize responses in Python for a custom Alexa skill, question-answer project
我正在为 Alexa 创建一项技能,她可以在其中回答问题 "Who is right, me or ..." 我在这里学习了教程 https://medium.com/crowdbotics/how-to-build-a-custom-amazon-alexa-skill-step-by-step-my-favorite-chess-player-dcc0edae53fb
但我需要随机化 Alexa 响应
Player_LIST = ["me or my wife", "me or my husband"]
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my husband":"He is."}
#------------------------------Part3--------------------------------
# Here we define the Request handler functions
def on_start():
print("Session Started.")
def on_launch(event):
onlunch_MSG = "Hi, you could say, for example: who is right me or my husband?"
reprompt_MSG = "you can say, who is right, me or my wife?"
card_TEXT = "Who is right, me or... ?."
card_TITLE = "Choose your question."
return output_json_builder_with_reprompt_and_card(onlunch_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)
def on_end():
print("Session Ended.")
#---------------------------Part3.1.1-------------------------------
# Here we define the intent handler functions
def player_bio(event):
name=event['request']['intent']['slots']['player']['value']
player_list_lower=[w.lower() for w in Player_LIST]
if name.lower() in player_list_lower:
reprompt_MSG = ""
card_TEXT = "You've picked " + name.lower()
card_TITLE = "You've picked " + name.lower()
return output_json_builder_with_reprompt_and_card(Player_BIOGRAPHY[name.lower()], card_TEXT, card_TITLE, reprompt_MSG, False)
else:
wrongname_MSG = "Some questions may not yet be present in my database. Try to rephrase your sentence."
reprompt_MSG = "For example, who is right, me or my wife?"
card_TEXT = "Use the full question."
card_TITLE = "Wrong question."
return output_json_builder_with_reprompt_and_card(wrongname_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)
当我说"Alexa, who is right, me or my wife?"时,她总是说:"She is"
我希望她每次都给我不同的回答,比如:他是或他或她或听你妻子的!或者当然是你或任何其他答案。我尝试这样做:
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my wife":"you.",
"me or my wife":"Of course your wife",
"me or my husband":"He is.",
"me or my husband":"You are right.",
"me or my husband":"He is not right."}
但 Alexa 总是只选择最后的回复 "Of course your wife."
我怎样才能随机化这些响应中的许多?我不知道如何编码,但如果我已经走到这一步,我将能够在你的帮助下完成,拜托。我可以 post 整个代码它只是这里的两倍。
Player_BIOGRAPHY
是一个dict
,也就是说每个键只有一个值。如果你初始化为
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my wife":"you.",
"me or my wife":"Of course your wife",
"me or my husband":"He is.",
"me or my husband":"You are right.",
"me or my husband":"He is not right."}
实际打印输出为:
{'me or my wife': 'Of course your wife', 'me or my husband': 'He is not right.'}
您应该做的是列出每个键的可能响应,然后使用 random.choice
之类的内容从列表中进行选择。像这样,
Player_BIOGRAPHY = {"me or my wife": ["She is.","you.","Of course your wife"],
"me or my husband": ["He is.","You are right.","He is not right."]}
并进行随机选择(需要import random
)
random.choice(Player_BIOGRAPHY[name.lower()])
# This will select a random item from the list mapped to name.lower()
所以你的完整代码如下:
import random # this can be at the top of the file too
def player_bio(event):
name=event['request']['intent']['slots']['player']['value']
player_list_lower=[w.lower() for w in Player_LIST]
if name.lower() in player_list_lower:
reprompt_MSG = ""
card_TEXT = "You've picked " + name.lower()
card_TITLE = "You've picked " + name.lower()
return output_json_builder_with_reprompt_and_card(random.choice(Player_BIOGRAPHY[name.lower()]), card_TEXT, card_TITLE, reprompt_MSG, False)
else:
wrongname_MSG = "Some questions may not yet be present in my database. Try to rephrase your sentence."
reprompt_MSG = "For example, who is right, me or my wife?"
card_TEXT = "Use the full question."
card_TITLE = "Wrong question."
return output_json_builder_with_reprompt_and_card(wrongname_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)
我正在为 Alexa 创建一项技能,她可以在其中回答问题 "Who is right, me or ..." 我在这里学习了教程 https://medium.com/crowdbotics/how-to-build-a-custom-amazon-alexa-skill-step-by-step-my-favorite-chess-player-dcc0edae53fb 但我需要随机化 Alexa 响应
Player_LIST = ["me or my wife", "me or my husband"]
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my husband":"He is."}
#------------------------------Part3--------------------------------
# Here we define the Request handler functions
def on_start():
print("Session Started.")
def on_launch(event):
onlunch_MSG = "Hi, you could say, for example: who is right me or my husband?"
reprompt_MSG = "you can say, who is right, me or my wife?"
card_TEXT = "Who is right, me or... ?."
card_TITLE = "Choose your question."
return output_json_builder_with_reprompt_and_card(onlunch_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)
def on_end():
print("Session Ended.")
#---------------------------Part3.1.1-------------------------------
# Here we define the intent handler functions
def player_bio(event):
name=event['request']['intent']['slots']['player']['value']
player_list_lower=[w.lower() for w in Player_LIST]
if name.lower() in player_list_lower:
reprompt_MSG = ""
card_TEXT = "You've picked " + name.lower()
card_TITLE = "You've picked " + name.lower()
return output_json_builder_with_reprompt_and_card(Player_BIOGRAPHY[name.lower()], card_TEXT, card_TITLE, reprompt_MSG, False)
else:
wrongname_MSG = "Some questions may not yet be present in my database. Try to rephrase your sentence."
reprompt_MSG = "For example, who is right, me or my wife?"
card_TEXT = "Use the full question."
card_TITLE = "Wrong question."
return output_json_builder_with_reprompt_and_card(wrongname_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)
当我说"Alexa, who is right, me or my wife?"时,她总是说:"She is" 我希望她每次都给我不同的回答,比如:他是或他或她或听你妻子的!或者当然是你或任何其他答案。我尝试这样做:
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my wife":"you.",
"me or my wife":"Of course your wife",
"me or my husband":"He is.",
"me or my husband":"You are right.",
"me or my husband":"He is not right."}
但 Alexa 总是只选择最后的回复 "Of course your wife." 我怎样才能随机化这些响应中的许多?我不知道如何编码,但如果我已经走到这一步,我将能够在你的帮助下完成,拜托。我可以 post 整个代码它只是这里的两倍。
Player_BIOGRAPHY
是一个dict
,也就是说每个键只有一个值。如果你初始化为
Player_BIOGRAPHY = {"me or my wife":"She is.",
"me or my wife":"you.",
"me or my wife":"Of course your wife",
"me or my husband":"He is.",
"me or my husband":"You are right.",
"me or my husband":"He is not right."}
实际打印输出为:
{'me or my wife': 'Of course your wife', 'me or my husband': 'He is not right.'}
您应该做的是列出每个键的可能响应,然后使用 random.choice
之类的内容从列表中进行选择。像这样,
Player_BIOGRAPHY = {"me or my wife": ["She is.","you.","Of course your wife"],
"me or my husband": ["He is.","You are right.","He is not right."]}
并进行随机选择(需要import random
)
random.choice(Player_BIOGRAPHY[name.lower()])
# This will select a random item from the list mapped to name.lower()
所以你的完整代码如下:
import random # this can be at the top of the file too
def player_bio(event):
name=event['request']['intent']['slots']['player']['value']
player_list_lower=[w.lower() for w in Player_LIST]
if name.lower() in player_list_lower:
reprompt_MSG = ""
card_TEXT = "You've picked " + name.lower()
card_TITLE = "You've picked " + name.lower()
return output_json_builder_with_reprompt_and_card(random.choice(Player_BIOGRAPHY[name.lower()]), card_TEXT, card_TITLE, reprompt_MSG, False)
else:
wrongname_MSG = "Some questions may not yet be present in my database. Try to rephrase your sentence."
reprompt_MSG = "For example, who is right, me or my wife?"
card_TEXT = "Use the full question."
card_TITLE = "Wrong question."
return output_json_builder_with_reprompt_and_card(wrongname_MSG, card_TEXT, card_TITLE, reprompt_MSG, False)