从非直接输入预测JavaScript字符串
我目前正在编程一个Discord机器人,想知道如果输入不正确,是否有可能预测想要的命令 例如,我有一个单词列表: ['help'、'meme'、'ping'],从非直接输入预测JavaScript字符串,javascript,algorithm,bots,discord.js,prediction,Javascript,Algorithm,Bots,Discord.js,Prediction,我目前正在编程一个Discord机器人,想知道如果输入不正确,是否有可能预测想要的命令 例如,我有一个单词列表: ['help'、'meme'、'ping'], 如果用户输入hepl,是否有可能猜测他们想要键入帮助?一个选项是查找一个命令,其输入值为2或更少: // https://gist.github.com/andrei-m/982927 const getEditDistance=functiont,n{if0==t.lengthreturn n.length;if0==n.lengt
如果用户输入hepl,是否有可能猜测他们想要键入帮助?一个选项是查找一个命令,其输入值为2或更少: // https://gist.github.com/andrei-m/982927
const getEditDistance=functiont,n{if0==t.lengthreturn n.length;if0==n.lengthreturn t.length;var e,h,r=[];fore=0;e您可以在建议中找到匹配项并显示多个单词。如果您想要相同的匹配项,可以用于显示大多数匹配项 const words=[help,meme,ping]; const getHits=word,wordToMatch,hits=0=>{ if!word.length | |!wordToMatch.length返回命中数; 设charW=word.0,1; 让index=wordToMatch.indexOfcharW; 如果索引!==-1{ 返回getHits word.1, StringwordToMatch.slice0,索引+wordToMatch.substrindex+1, 命中率+1 ; } 返回gethitswird.slice1,wordToMatch,hits; }; 常量getMatch=mword=>{ 返回words.reducem,word=>{ m[word]=getHitsmword,word; 返回m; }, {}; }; 常量排序=obj=>{ return Object.entriesobj.sort [[uuu1,value1],[uuu2,value2]=>value2-value1 ; }; console.loggetMatchhelp; console.logsortgetMatchhelp; console.loggetMatchme; console.logsortgetMatchme; 。作为控制台行{color:blue!important},并应用相同的概念,进一步扩展。