首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript/Node如何使用match从一系列单词中重复一个单词

Javascript/Node如何使用match从一系列单词中重复一个单词
EN

Stack Overflow用户
提问于 2013-11-21 22:47:43
回答 1查看 81关注 0票数 0

这件事真的需要帮助。Iv'e用javascript和Node.js做了一个人工智能。我已经对一系列的问题和答案进行了重新编码,如果用户愿意的话,这也使得用户可以更改IA的答案。

大多数javascript代码ha都是在服务器端生成的。

我需要使人工智能能够重复一个特定的单词。例如:

如果用户写-“你住在哪里?”,人工智能将返回“在中土我的宝贝”,因为阿莱名单:

代码语言:javascript
运行
复制
var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };

我必须使它,以便用户写“现场”,它将重复“现场”。我正在考虑用火柴,但我不确定。欢迎您输入!

如果用户对BestAnswer进行了更改,则返回新的应答,否则将返回默认应答。下面是发送到client/html端的数组和代码:

代码语言:javascript
运行
复制
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/

问候

克里斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-21 23:00:32

如果我正确理解这个问题,这可能会奏效:

代码语言:javascript
运行
复制
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;
        }

   }

   //check if answer was found, if it's default echo user's question:
   if(aQuestion != '' && Answer == "What does it ask us?? Gollum! Gollum!!...") {
        Answer = aQuestion;
   }


    res.sendfile("public/index.html");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20133528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档