【问题标题】:Javascript/Node AI - How to use match to repeat a word from an array of wordsJavascript/Node AI - 如何使用匹配从单词数组中重复单词
【发布时间】:2013-11-21 22:47:43
【问题描述】:

在这方面真的需要一些帮助。我用 javascript 和 Node.js 做了一个 AI。我已经编码了一堆问题和答案,如果用户愿意,我也可以更改 IA 的答案。

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

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

如果用户写-“你住在哪里?”,由于数组列表,ai 将返回“在中土我的宝贝”:

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

我必须让用户写“live”它会重复“live”。我正在考虑使用匹配变量,但我不确定。一些输入将不胜感激!

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/

问候

克里斯

【问题讨论】:

    标签: javascript arrays node.js artificial-intelligence


    【解决方案1】:

    如果我正确理解了这个问题,这可能会起作用:

    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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 2021-09-03
      • 2016-02-17
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多