首页 > 解决方案 > 无法使用 luis 响应中的 json 路径提取意图和得分

问题描述

嗨,伙计们,
我正在努力从 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": {}
  }
}

标签: jsonrestapixpathazure-language-understanding

解决方案


您只需要按他们的分数对意图列表进行排序。这是一个 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
  }
]

推荐阅读