Javascript/Node AI – 如何使用match重复数组中单词

我真的需要在这方面得到一些帮助。我已经用Javascript和Node.js开发了一个AI。我已经硬编码了一系列问题和答案,并且还允许用户根据需要更改AI的回答。

大部分Javascript代码都是在服务器端编写的。

我需要让AI能够重复特定的单词。例如:

如果用户输入 – “你住在哪里?”,AI会返回”In middle earth my precious”,因为数组列表中有如下内容:

var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };

我需要实现的是,当用户输入”live”时,AI会重复”live”。我考虑使用match变量,但我不确定。希望能得到一些建议!

BestAnswer会在用户更改答案时返回新的答案,否则它会返回默认答案。以下是发送到客户端/HTML端的数组和代码:

var Question1 = { Question: "Hello", Response: "Hello precious" };var Question2 = { Question: "What is your name?", Response: "My name is " + aHobbit.name + " " + "precious" };var Question3 = { Question: "How old are you?", Response: "I'm " + aHobbit.age + " " + "my love" };var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };var Question5 = { Question: "What do you like?", Response: "We love the precious. Yummy food we like, raw fish, rabbits, all of them.<br> I like them raw and raddeling. Yes precious raw we like them" };var Question6 = { Question: "What don't you like?", Response: "Filthy orcsisses, stupid fat hobbitsses.<br> Yes precious..  but juicy and tender they are.." };var Question7 = { Question: "How are you?", Response: "We are so happy precious oh yees..<br> Up and down down and up.. up up up we go.. Smeagoooool!!" };var AllQueries = [Question1, Question2, Question3, Question4, Question5, Question6];app.post("/creature", function (req, res) {    var aQuestion = req.param("question");    //var BestQuestion = req.param("bestQuestion");    var BestAnswer = req.param("bestAnswer");    var length = AllQueries.length;    Answer = "What does it ask us?? Gollum! Gollum!!...";   for (var i = 0; i < length; i++) {        if (AllQueries[i].Question === aQuestion) {            if (BestAnswer != undefined && BestAnswer.trim().length > 0) {                AllQueries[i].Response = BestAnswer;            }            Answer = AllQueries[i].Response;        }   }    res.sendfile("public/index.html");

以下是界面截图:

http://postimg.org/image/nbf3w6wr7/full/

此致

Chris


回答:

如果我正确理解了这个问题,以下方法可能会有效:

var Question1 = { Question: "Hello", Response: "Hello precious" };var Question2 = { Question: "What is your name?", Response: "My name is " + aHobbit.name + " " + "precious" };var Question3 = { Question: "How old are you?", Response: "I'm " + aHobbit.age + " " + "my love" };var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };var Question5 = { Question: "What do you like?", Response: "We love the precious. Yummy food we like, raw fish, rabbits, all of them.<br> I like them raw and raddeling. Yes precious raw we like them" };var Question6 = { Question: "What don't you like?", Response: "Filthy orcsisses, stupid fat hobbitsses.<br> Yes precious..  but juicy and tender they are.." };var Question7 = { Question: "How are you?", Response: "We are so happy precious oh yees..<br> Up and down down and up.. up up up we go.. Smeagoooool!!" };var AllQueries = [Question1, Question2, Question3, Question4, Question5, Question6];app.post("/creature", function (req, res) {    var aQuestion = req.param("question");    //var BestQuestion = req.param("bestQuestion");    var BestAnswer = req.param("bestAnswer");    var length = AllQueries.length;    Answer = "What does it ask us?? Gollum! Gollum!!...";   for (var i = 0; i < length; i++) {        if (AllQueries[i].Question === aQuestion) {            if (BestAnswer != undefined && BestAnswer.trim().length > 0) {                AllQueries[i].Response = BestAnswer;            }            Answer = AllQueries[i].Response;        }   }   //检查是否找到了答案,如果是默认答案,则回显用户的问题:   if(aQuestion != '' && Answer == "What does it ask us?? Gollum! Gollum!!...") {        Answer = aQuestion;   }    res.sendfile("public/index.html");

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注