Hi Folks,我正在努力从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
}
]