【问题标题】:How to handle synonyms in intents in Alexa?如何在 Alexa 中处理意图中的同义词?
【发布时间】:2018-07-16 05:46:48
【问题描述】:

在以下示例中: 如果用户说太妃糖,那不应该翻译成糖果吗?我问是因为传递给我的意图的价值是“太妃糖”。所以不确定我有什么不正确的。

types":[  
   {  
      "name":"SHOPPING_LIST",
      "values":[  
         {  
            "id":null,
            "name":{  
               "value":"candy",
               "synonyms":[  
                  "toffee"
               ]
            }
         },
         {  
            "name":"GetPrice",
            "samples":[  
               "Get me the price of {item}",
               "tell me the price of {item}",

            ],
            "slots":[  
               {  
                  "name":"item",
                  "type":"SHOPPING_LIST"
               }
            ]
         }

【问题讨论】:

    标签: aws-lambda alexa alexa-skills-kit alexa-skill alexa-voice-service


    【解决方案1】:

    我们需要在您的后端代码中处理实体解析。 更多可以参考这里: https://developer.amazon.com/blogs/alexa/post/5de2b24d-d932-4c6f-950d-d09d8ffdf4d4/entity-resolution-and-slot-validation

    您可以在代码中添加,

    this.attributes.item = slotValue(this.event.request.intent.slots.item);
    

    另外,将它添加到您的处理函数之外,

    function slotValue(slot, useId){
        let value = slot.value;
        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'){
            let resolutionValue = resolution.values[0].value;
            value = resolutionValue.id && useId ? resolutionValue.id : resolutionValue.name;
        }
        return value;
    }
    

    现在当您的用户输入太妃糖时,它会被翻译成糖果。

    【讨论】:

    • 感谢您提供的出色功能,但令人震惊的是,这不是为我们内置的。
    • 确实如此。我已经搞砸了很多试图找出是否有内置的东西。不幸的是,还没有。
    • @Caltor 实际上响应将是 JSON,因此要提取您只需要按照树进行操作,请参阅下面的直接提取答案,无需将任何内容传递给任何函数
    【解决方案2】:

    一般的响应 JSON 结构如下,因此提取它在一行中使用

    来源 = intent.slots.source.resolutions.resolutionsPerAuthority[0].values[0].value.name;

    "request": {
        "type": "IntentRequest",
        "requestId": "amzn1.ech****************************************************************************************",
        "timestamp": "2019-03-13T08:34:46Z",
        "locale": "en-US",
        "intent": {
            "name": "STMDStreamStartIntent",
            "confirmationStatus": "NONE",
            "slots": {
                "source": {
                    "name": "source",
                    "value": "source 1",
                    "resolutions": {
                        "resolutionsPerAuthority": [
                            {
                                "authority": "amzn1.er-authority.e************************************************",
                                "status": {
                                    "code": "ER_SUCCESS_MATCH"
                                },
                                "values": [
                                    {
                                        "value": {
                                            "name": "source1",
                                            "id": "SOURCE_ONE"
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    "confirmationStatus": "NONE",
                    "source": "USER"
                }
            }
        }
    }
    

    希望这会简单而有用。

    【讨论】:

    • 如果它不存在,这将引发错误。这就是@thedreamsaver 使用 if 语句来查看它是否存在的原因。他也是一个函数,因此它可以在整个技能中重复使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多