无法从 luis 响应中使用 json 路径提取意图和分数
Failing to extract intent and score with json path from luis response
大家好,
我正在努力从 LUIS api 响应及其相应分数中提取前 2 个得分意图。
来自响应下面我需要提取 4 个值:
{
"query": "turn on all lights",
"prediction": {
"topIntent": "NAME_INFO",
"intents": {
"NAME_INFO": {
"score": 0.0462775342
},
"MONTHLY_HOUSING_INFO": {
"score": 0.0363982953
},
"WHAT_NEXT_INFO": {
"score": 0.03436338
},
"ADDRESS_INFO": {
"score": 0.0306101535
},
"SOCIAL_SECURITY_INFO": {
"score": 0.0280603524
},
"SECURITY_DEPOSIT_RETURN": {
"score": 0.0137537634
},
"None": {
"score": 0.003310648
},
"SECURITY_DEPOSIT_INFO": {
"score": 0.00294959615
}
},
"entities": {}
}
}
您只需按分数对意向列表进行排序。这是一个 JavaScript 示例,假设您的 JSON 响应保存在 result
:
// Convert result to an array of intents
const intentsArray = Object.entries(result.prediction.intents).map(([k, v]) => ({ intent: k, score: v.score }));
// Sort the array, descending
const sorted = intentsArray.sort((a, b) => b.score - a.score);
// Pull out the top two entries
const top2 = sorted.slice(0, 2);
// Show the result
console.log(JSON.stringify(top2, null, 2));
这导致:
[
{
"intent": "NAME_INFO",
"score": 0.0462775342
},
{
"intent": "MONTHLY_HOUSING_INFO",
"score": 0.0363982953
}
]
大家好,
我正在努力从 LUIS api 响应及其相应分数中提取前 2 个得分意图。
来自响应下面我需要提取 4 个值:
{
"query": "turn on all lights",
"prediction": {
"topIntent": "NAME_INFO",
"intents": {
"NAME_INFO": {
"score": 0.0462775342
},
"MONTHLY_HOUSING_INFO": {
"score": 0.0363982953
},
"WHAT_NEXT_INFO": {
"score": 0.03436338
},
"ADDRESS_INFO": {
"score": 0.0306101535
},
"SOCIAL_SECURITY_INFO": {
"score": 0.0280603524
},
"SECURITY_DEPOSIT_RETURN": {
"score": 0.0137537634
},
"None": {
"score": 0.003310648
},
"SECURITY_DEPOSIT_INFO": {
"score": 0.00294959615
}
},
"entities": {}
}
}
您只需按分数对意向列表进行排序。这是一个 JavaScript 示例,假设您的 JSON 响应保存在 result
:
// Convert result to an array of intents
const intentsArray = Object.entries(result.prediction.intents).map(([k, v]) => ({ intent: k, score: v.score }));
// Sort the array, descending
const sorted = intentsArray.sort((a, b) => b.score - a.score);
// Pull out the top two entries
const top2 = sorted.slice(0, 2);
// Show the result
console.log(JSON.stringify(top2, null, 2));
这导致:
[
{
"intent": "NAME_INFO",
"score": 0.0462775342
},
{
"intent": "MONTHLY_HOUSING_INFO",
"score": 0.0363982953
}
]