2013-11-21 43 views
0

真的需要一些幫助。 Iv'e用javascript和Node.js完成了一個AI。我對一些問題和答案進行了編碼,iv'e也使得用戶可以根據需要更改IA的答案。Javascript /結點AI - 如何使用匹配重複單詞中的單詞

大部分的JavaScript代碼都是在服務器端進行的。

我需要讓AI可以重複一個特定的詞。例如:

,如果用戶寫 - 「?你住在哪裏」,AI會返回「在中土我的寶貝」,因爲阿雷名單:

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

我必須使它如此用戶寫入「現場」會重複「現場」。我正在考慮使用匹配變異藻,但我不確定。索姆輸入將不勝感激!

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/

問候

克里斯

回答

0

如果我正確地理解這個問題,這可能會工作:

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

謝謝老兄!這個技巧:-) – koffe14