json - 无法使用 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": {}
}
}
解决方案
您只需要按他们的分数对意图列表进行排序。这是一个 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
}
]
推荐阅读
- shopware - ShopWare 中 Article.availableFrom 的更改无效
- javascript - 使用 JavaScript 缩放浏览器内容
- jquery - jquery datatable sotring 在 IE 中无法使用这种格式“MMM-YYYY”在 chrome 中可以正常工作
- python - 如何显示所有数据并将其保存在文本文件中?
- node.js - Node.js + Express 站点的最佳服务器
- excel - 合并2个excel代码
- typescript - 如何在 Visual Studio Code 中的项目之间共享通用(和积极修改的)TypeScript 文件?
- android - 如何实现 MediaStyle 通知以支持 Android Oreo?
- sql - 为无效的输入数据类型创建错误消息
- android - 使用带有 Camera2 API 的 MediaCodec 录制视频