简体   繁体   English

我如何实现 NodeJS 的“你的意思是”功能?

[英]How do I implement NodeJS “Did you mean” feature?

How can I implement a "Did you mean" feature in NodeJS?如何在 NodeJS 中实现“您的意思是”功能? I searched the internet and found a tutorial that uses a webserver but I will be using this for a Discord bot, so I can't really use tutorials that involve web servers, etc.我在互联网上搜索并找到了一个使用网络服务器的教程,但我将把它用于 Discord 机器人,所以我不能真正使用涉及 web 服务器等的教程。

For example:例如:

User enters command: restrat
Actual command: restart
Bot Message: Command not found, did you mean "restart"?

Is this possible using NodeJS?这可能使用NodeJS吗? If so, may I know how I can implement this?如果是这样,我可以知道如何实现吗?

Any help will be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Whether in NodeJS or the browser, some code is needed to find a command that most closely resembles the text entered by the user.无论是在 NodeJS 中还是在浏览器中,都需要一些代码来找到与用户输入的文本最相似的命令。

This code uses the Levenshtein distance calculation to return the closest match.此代码使用Levenshtein 距离计算来返回最接近的匹配。

const jsLevenshtein = require("js-levenshtein");

function closestCommand (userText, commands) {
  let minDistance = Infinity;
  return commands.reduce((closest, cmd) => {
    const cmdDistance = jsLevenshtein(userText, cmd);
    if (cmdDistance < minDistance) {
      minDistance = cmdDistance;
      return cmd;
    }
    return closest;
  }, '');
}

const myCommands = ['quit', 'login', 'logout', 'restart', 'refresh'];
const userCommand = 'restrat';

console.log(`closest command is '${closestCommand(userCommand, myCommands)}'`);
// closest command is 'restart'

Working code in RunKit. RunKit 中的工作代码。

It should be straightforward to add this to a NodeJS app.将它添加到 NodeJS 应用程序应该很简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ArangoJS(ArangoDB)为NodeJS应用实现类似于搜索的功能 - How Do I Implement Search Like Feature For NodeJS App Which Using ArangoJS(ArangoDB) 如何在 nodejs 中执行此异步功能 - How can I do this asyn feature in nodejs 我如何使用keycloak在NodeJs中实现注销功能 - How can i implement logout feature in NodeJs with keycloak 如何在没有 Nodejs 的情况下在 javascript 中实现 WebSocket 服务器? - How do I implement a WebSocket server in javascript without Nodejs? 如何有效地在 MEAN 网络应用程序上实现类似功能? - How to implement a like feature on a MEAN web app efficiently? 如何使用Elasticsearch NPM在Node.js中实现ElasticSearch脚本 - How do i implement elasticsearch scripts in nodejs using elasticsearch npm nodejs mongoose 错误 TS2551:“文档”类型上不存在属性“isDeleted”。 你的意思是'$isDeleted' - nodejs mongoose error TS2551: Property 'isDeleted' does not exist on type 'Document'. Did you mean '$isDeleted' 我如何修复消息“你真的不认为你可以通过npm安装Ruby,是吗?” - How do I fix the message “You didn't really think you could install Ruby through npm, did you?” 如何设置 mongoDB 的新 CSFLE 功能,使用 nodejs 进行显式加密隐式解密? - How do I set up mongoDB's new CSFLE feature, with explicit encryption implicit decryption using nodejs? 无论如何要在 Java 脚本中模拟“你的意思是”吗? - Is there anyway to simulate a “Did you mean” in Java Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM