如何处理 Alexa 意图中的同义词?
How to handle synonyms in intents in Alexa?
在下面的例子中:
如果用户说太妃糖,难道不应该翻译成糖果吗?我问是因为传递给我意图的值是 'toffee'。所以不确定我说错了什么。
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"
}
]
}
我们需要在您的后端代码中处理实体解析。
更多可以参考这里:
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;
}
现在当您的用户输入太妃糖时,它会被翻译成糖果。
一般响应 JSON 结构如下,因此要在一行中提取它,请使用
source =
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"
}
}
}
}
希望这会简单且有用。
在下面的例子中: 如果用户说太妃糖,难道不应该翻译成糖果吗?我问是因为传递给我意图的值是 'toffee'。所以不确定我说错了什么。
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"
}
]
}
我们需要在您的后端代码中处理实体解析。 更多可以参考这里: 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;
}
现在当您的用户输入太妃糖时,它会被翻译成糖果。
一般响应 JSON 结构如下,因此要在一行中提取它,请使用
source = 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"
}
}
}
}
希望这会简单且有用。