gpt4 book ai didi

javascript - 如何通过多个数组过滤字符串并选择最相似的一个

转载 作者:行者123 更新时间:2023-12-03 03:05:17 26 4
gpt4 key购买 nike

我不是要求代码示例,只是要求尽可能的最佳方法

因此,我目前正在创建一个机器人应答的帮助台,当它识别出特定单词时(例如,如果用户输入“stuur mail”,它会说“Als je een mailtje wil”),它会回答特定的答案(例如联系地址)。 Sturen kan dit naar help@wordquest.nl')

它适用于数组,所以有一个数组

mailText['stuur', 'mail', 'verzend'];

还有一个函数 (checkMail()),用于检查数组中有多少给定文本(来自输入字段)。因此,如果用户输入“hoe Stuur ik een mail”,它会识别“stuur”和“mail”,并知道使用了 mailText 数组中 66% 的单词。我目前有两个这样的数组。它可以工作,有 4 个 if 语句来检查哪一个最适合用户输入,但我真的不想编写数百个 if 语句来比较数组并为每个数组创建一个函数。

当前的JS是

window.onkeydown = function (event) {
if (event.keyCode == 13) {
checkQuestions(document.getElementsByClassName('inputField')[0].value);

document.getElementsByClassName('inputField')[0].value = '';

if (event.preventDefault) event.preventDefault(); // This should fix it
return false; // Just a workaround for old browsers
}
}

var mail = ['stuur', 'mail', 'verzend'];
var verzenden = ['kost', 'verzend', 'pakket'];

var errorText = '<h1>An error has occured...</h1><br>There are no answers with the current search query...';

var mailText = 'Als je een mailtje wil sturen kan dit naar <a
href="mailto:help@wordquest.nl">help@wordquest.nl</a>';
var sendText = 'Verzendkosten verschillen per pakket';

var answers = [];

function checkQuestions(quest) {
console.log('');
answer = 0;
finalText = errorText;
answers = [];

mailNum = checkMail(quest);
sendNum = checkSend(quest);

if (mailNum > sendNum) {
answers.push(mailText);
} else if (sendNum > mailNum) {
answers.push(sendText);
} else if (sendNum == mailNum && sendNum != 0) {
answers.push(mailText);
answers.push(sendText);
} else {
answers.push(errorText);
}

setAnswer();
}

function checkMail(question) {
chance = 0;
indent = 100 / mail.length;
for (i = 0; i < mail.length; i++) {
if (question.includes(mail[i])) {
chance = chance + indent;
}
}
console.log('mail is ' + chance + '%');
return chance;
}

function checkSend(question) {
chance = 0;
indent = 100 / verzenden.length;
for (i = 0; i < verzenden.length; i++) {
if (question.includes(verzenden[i])) {
chance = chance + indent;
}
}
console.log('verzenden is ' + chance + '%');
return chance;
}

var finalAns = [];

function setAnswer() {
finalAns = [];

if (answers[0] != errorText) {
finalAns.push('<b>Er zijn ' + answers.length + ' antwoorden gevonden!</b>');
}

for (i = 0; i < answers.length; i++) {
finalAns.push('<p>' + answers[i] + '</p>');
}

document.getElementsByClassName('answers')[0].innerHTML = finalAns.join('');
}

当前页面(HTML):https://codepen.io/timyboy12345/pen/YEpmZz

我知道这可能不是最好的方法,但这只是一个爱好项目......

那么,是否有人知道如何创建一个函数,将用户输入与所有数组的答案组合进行比较(最终可能+50),而无需为每个数组创建函数,并为用户提供正确的答案文本(我目前正在使用预定义变量)以及他们的问题的答案。

最佳答案

您可以使用嵌套数组并使用计数器来跟踪匹配的单词数。然后计算准确率百分比并使用最终数组来了解哪个数组的答案是正确的。

循环的伪代码:

for i = 0; i < arraysToMatch; i++
nbMatched = 0
foreach answer in answerArray[i]
if answer is in arraysToMatch[i] then
++nbMatched
end if
end foreach

// Here calculate the accuracy and if good use i

end for

这样你就不需要每个数组都有一个函数。但是,您需要将匹配数组与答案数组配对。

关于javascript - 如何通过多个数组过滤字符串并选择最相似的一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com