使用关键字在 JSON 文件中找到最佳匹配
Find the best match in JSON file with keywords
我不知道怎么解释,我不知道这是否可能,因为我在互联网上找不到我想要的东西。
例如我有这个JSON命令
{
"gif": [
"gif","show","bot"
],
"doc": [
"show","doc","bot"
],
"weather": [
"show","actual","weather"
]
}
用户输入此消息:
"Hi bot can you show me the actual weather ?"
我如何编写代码,我的函数将 return 命令("weather" 在这种情况下)与用户消息中的最佳关键字匹配?
希望我的问题比较清楚
谢谢
您可以使用 Object.keys
遍历您的 JSON,然后使用 array#every
使用 string#includes
检查字符串中数组中的每个单词是否存在。
var keyWords = { "gif": [ "gif","show","bot" ], "doc": [ "show","doc","bot" ], "weather": [ "show","actual","weather" ] };
var message = "Hi bot can you show me the actual weather ?";
var result = Object.keys(keyWords).find(k =>
keyWords[k].every(word => message.includes(word)));
console.log(result);
我不知道怎么解释,我不知道这是否可能,因为我在互联网上找不到我想要的东西。
例如我有这个JSON命令
{
"gif": [
"gif","show","bot"
],
"doc": [
"show","doc","bot"
],
"weather": [
"show","actual","weather"
]
}
用户输入此消息:
"Hi bot can you show me the actual weather ?"
我如何编写代码,我的函数将 return 命令("weather" 在这种情况下)与用户消息中的最佳关键字匹配?
希望我的问题比较清楚
谢谢
您可以使用 Object.keys
遍历您的 JSON,然后使用 array#every
使用 string#includes
检查字符串中数组中的每个单词是否存在。
var keyWords = { "gif": [ "gif","show","bot" ], "doc": [ "show","doc","bot" ], "weather": [ "show","actual","weather" ] };
var message = "Hi bot can you show me the actual weather ?";
var result = Object.keys(keyWords).find(k =>
keyWords[k].every(word => message.includes(word)));
console.log(result);