问题描述
这是我在这里的第一个问题。我开始学习 Javascript,现在正在修改脚本。(开源) 在输入字段(html)上,我告诉一个问题,用“Enter”将其发送到脚本。在constants.js 我写了所有的问题和答案。 如何更改脚本以便代码仅考虑预先创建的问题中的某些关键字。 还要忽略拼写错误?看起来不容易,但我不会学。我在网上和我所有的书上搜索,在 Javascript 中找不到答案。
非常感谢您的帮助!
const prompts = [
["hi"],["wie geht es dir"],["auch gut"],["gut"],["sehr gut"],
Index.js 验证输入并搜索正确答案。
const replies = [
["hallo"],["mir geht es gut,und dir"],["das ist schön. was kann ich für dich tun"],["oh,das ist nicht schön. was soll ich tun für dich"],
Index.js:
document.addEventListener("DOMContentLoaded",() => {
const inputField = document.getElementById("input");
inputField.addEventListener("keydown",(e) => {
if (e.code === "Enter") {
let input = inputField.value;
inputField.value = "";
output(input);
}
});
});
function output(input) {
let product;
let text = input.toLowerCase().replace(/\u00e4/g,"ae").replace(/\u00f6/g,"oe").replace(/\u00fc/g,"ue").replace(/\u00df/g,"ss").trim();
text = text
.replace(/ a /g," ") // 'tell me a story' -> 'tell me story'
.replace(/i feel /g,"")
.replace(/whats/g,"what is")
.replace(/please /g,"")
.replace(/ please/g,"")
.replace(/r u/g,"are you");
if (compare(prompts,replies,text)) {
// Search for exact match in `prompts`
product = compare(prompts,text);
} else if (text.match(/danke/gi)) {
product = "Gerne geschehen!"
} else if (text.match(/(corona|covid|virus)/gi)) {
// If no match,check if message contains `coronavirus`
product = coronavirus[Math.floor(Math.random() * coronavirus.length)];
} else {
// If all else fails: random alternative
product = alternative[Math.floor(Math.random() * alternative.length)];
}
// Update DOM
addChat(input,product);
}
function compare(promptsArray,repliesArray,string) {
let reply;
let replyFound = false;
for (let x = 0; x < promptsArray.length; x++) {
for (let y = 0; y < promptsArray[x].length; y++) {
if (promptsArray[x][y] === string) {
let replies = repliesArray[x];
reply = replies[Math.floor(Math.random() * replies.length)];
replyFound = true;
// Stop inner loop when input value matches prompts
break;
}
}
if (replyFound) {
// Stop outer loop when reply is found instead of interating through the entire array
break;
}
}
return reply;
}
function addChat(input,product) {
const messagesContainer = document.getElementById("messages");
let userDiv = document.createElement("div");
userDiv.id = "user";
userDiv.className = "user response";
userDiv.innerHTML = `<span>${input}</span>`;
messagesContainer.appendChild(userDiv);
let botDiv = document.createElement("div");
let botImg = document.createElement("img");
let bottext = document.createElement("span");
botDiv.id = "bot";
botImg.src = "";
botImg.className = "avatar";
botDiv.className = "bot response";
bottext.innerText = "Moment,ich berechne die Antwort...";
botDiv.appendChild(bottext);
botDiv.appendChild(botImg);
messagesContainer.appendChild(botDiv);
// Keep messages at most recent
messagesContainer.scrollTop = messagesContainer.scrollHeight - messagesContainer.clientHeight;
// Fake delay to seem "real"
setTimeout(() => {
bottext.innerText = `${product}`;
textToSpeech(product)
},2000
)
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)