我正在尝试通过在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;
}
}
答案 0 :(得分:0)
如果您想使用同义词,则必须使用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()) {
[...]