【问题标题】:Slot synonyms not working as expected插槽同义词未按预期工作
【发布时间】:2018-02-16 00:06:25
【问题描述】:

我正在尝试通过在 Alexa 技能中使用插槽同义词来处理实体解析。我从亚马逊提供的测验游戏模板开始我的技能,该模板包含城市名称、州缩写和大写字母的数据数组。我修改它以使用 NFL 球队名称。在测验交互期间,作为示例,用户可能会被问到“费城哪支 NFL 橄榄球队踢球?”。用户可以回答“Eagles”或“Philadelphia Eagles”,这两者都应该是可接受的以获得正确分数。短语“Philadelphia Eagles”是在我的 lambda 函数中的数据数组中定义的。在交互模型中,在我的 AnswerIntent 中,我有一个定义为 TeamName 的槽。我尝试在同义词中为“Philadelphia Eagles”和“Eagles”添加值。我使用 BIRDS 作为同义词 ID,Eagles 作为值,Philadelphia Eagles 作为同义词值。但是当我用“鹰”回答这个问题时,我得到了错误的答案。

我该如何纠正这个问题?

这是我在 Lambda 中的 AnswerIntent 函数:

"AnswerIntent": function() {
    let response = "";
    let speechOutput = "";
    let item = this.attributes["quizitem"];
    let property = this.attributes["quizproperty"];

    let correct = compareSlots(this.event.request.intent.slots, item[property]);

    if (correct)
    {
        response = getSpeechCon(true);
        this.attributes["quizscore"]++;
    }
    else
    {
        response = getSpeechCon(false);
    }

    response += getAnswer(property, item);

    if (this.attributes["counter"] < 10)
    {
        response += getCurrentScore(this.attributes["quizscore"], this.attributes["counter"]);
        this.attributes["response"] = response;
        this.emitWithState("AskQuestion");
    }
    else
    {
        response += getFinalScore(this.attributes["quizscore"], this.attributes["counter"]);
        speechOutput = response + " " + EXIT_SKILL_MESSAGE;

        this.response.speak(speechOutput);
        this.emit(":responseReady");
    }
},

这里是 compareSlot 函数:

function compareSlots(slots, value)

for (let slot in slots)
{
    if (slots[slot].value != undefined)
    {
        if (slots[slot].value.toString().toLowerCase() == value.toString().toLowerCase())
        {
            return true;
        }
    }
}
return false;

更新:compareSlots 函数已修改为:

    function compareSlots(slots, value)
{
    let slotId = slot.value; // fallback if you don't have resolutions
    let resolution = (slot.resolutions && slot.resolutions.resolutionsPerAuthority && slot.resolutions.resolutionsPerAuthority.length > 0) ? slot.resolutions.resolutionsPerAuthority[0] : null;

    if (resolution && resolution.status.code === 'ER_SUCCESS_MATCH') {

        if (resolution.values && resolution.values.length > 0) {
             slotId = resolution.values[0].value.id;
        }
    }

    if (slotId.toString().toLowerCase() == value.toString().toLowerCase()) {
        return true;
            }
}

【问题讨论】:

    标签: aws-lambda alexa alexa-skills-kit


    【解决方案1】:

    如果你想使用同义词,你必须使用entity resolutions。在那里,您可以检查多个同义词的一个 ID。

    因此,您的 compareSlots 函数应如下所示:

    [...]
    let slotId = slot.value; // fallback if you don't have resolutions
    let resolution = (slot.resolutions && slot.resolutions.resolutionsPerAuthority && slot.resolutions.resolutionsPerAuthority.length > 0) ? slot.resolutions.resolutionsPerAuthority[0] : null;
    
    if (resolution && resolution.status.code === 'ER_SUCCESS_MATCH') {
    
        if (resolution.values && resolution.values.length > 0) {
             slotId = resolution.values[0].value.id;
        }
    }
    
    if (slotId.toString().toLowerCase() == value.toString().toLowerCase()) {
    [...]
    

    【讨论】:

    • 我相应地修改了 compareSlots 函数,但执行时出现错误“Unexpected exeception 'ReferenceError: slot is not defined':.
    • 在您的 compareSlots 函数中,您使用“插槽”而不是单个“插槽”:
    • 您有两个选择:您已经在您的 AnswerIntent 中选择了正确的插槽并使用此插槽调用函数。 function compareSlots(slot, value) { 或者你把你的 for 循环放在 compareSlots 函数中并使用插槽:function compareSlots(slots, value) { for (let slot of slots) { let slotId = slot.value; // fallback if you don't have resolutions [...] 也很好读:difference between ( for… in ) and ( for… of ) in javascript
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多