我想针对自定义Alexa技能,问答项目在Python中将响应随机化

时间:2019-05-26 06:25:20

标签: python alexa-skill

我正在为Alexa创建技能,她将回答“谁是对的,我还是...”这个问题,我在这里遵循了一个教程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,谁是对的,我还是我的妻子?”时,她总是说:“她是” 我希望她每次都给我不同的答复,例如:他是或他或她,还是听你的妻子!或是您或其他任何答案。我尝试这样做:

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总是只选择最后一个回答:“当然是你的妻子。” 如何随机化其中许多响应?我不知道如何编码,但是如果我走了这么远,请在您的帮助下完成。我可以发布整个代码,只是这里的两倍。

1 个答案:

答案 0 :(得分:0)

Player_BIOGRAPHYdict,这意味着每个键只有一个值。如果您将其初始化为

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)