2016-02-23 21 views
1

因此,我構建了一個聊天機器人,並且我已經在線開發了一個開放源代碼,並希望自己修改聊天腳本。源代碼的腳本是建立與新的陣列,下面是腳本的一些例子:是否可以將函數鏈接到數組?

var convpatterns = new Array (
new Array (".*hello.*","Hello there! How are you?","Greetings!","Hi How are you?","Good day! "), 
new Array ("I need (.*)" , "Why do you need $1?", "Would it really help you to get $1?" , "Are you sure you need $1?"), 
new Array ("I remember (.*)", "Do you often think of $1?", "What else do you recollect?", "What in the present situation reminds you of $1?", "What else does $1 remind you of?"), 

因此,舉例來說,如果用戶輸入「你好」時,聊天機器人將隨機選擇從該陣列的答覆之一。我想知道的是,如果可以將來自不同陣列的用戶輸入鏈接到不同的功能。因此,如果用戶輸入「我需要一個朋友」,而不是從上面列出的答覆中選擇一個隨機答覆,它將鏈接到一個函數,例如Need()函數,我可以添加更多選項,如IF和ELSE規則。

產生對話的功能:

function mainroutine() { 
    uinput = document.mainscreen.BasicTextArea4.value; 
    dialog = dialog + "User: " + uinput + '\r' + "\n"; 
    conversationpatterns(); 
    dialog = dialog + '\r' + "\n"; 
    updatescreen(); 
} 

function conversationpatterns() { 
    for (i=0; i < convpatterns.length; i++) { 
     re = new RegExp (convpatterns[i][0], "i"); 
     if (re.test(uinput)) { 
      len = convpatterns[i].length - 1; 
      index = Math.ceil(len * Math.random()); 
      reply = convpatterns[i][index]; 
      soutput = uinput.replace(re, reply); 
      soutput = initialCap(soutput); 
      dialog = dialog + "Avatar: " + soutput + '\r' + "\n"; 
      break; 
     } 
    } 
} 

回答

0

我建議

reply = convpatterns[i][index]; 
if (typeof reply === "function") reply = reply(); 

現在你可以有

function need() { ...; return someString; } 
... 
".*hello.*",need, "Hello there! How are you?" 

".*hello.*",function() { ...; return someString }, "Hello there! How are you?" 

像這樣:

var convpatterns = [ 
 
    [".*hello.*", function(str) { return str.toUpperCase()+"?" }, "Hello there! How are you?", "Greetings!", "Hi How are you?", "Good day! "], 
 
    ["I need (.*)", "Why do you need $1?", "Would it really help you to get $1?", "Are you sure you need $1?"], 
 
    ["I remember (.*)", "Do you often think of $1?", "What else do you recollect?", "What in the present situation reminds you of $1?", "What else does $1 remind you of?"] 
 
]; 
 

 
function updatescreen() { 
 
    document.getElementById("update").innerHTML=dialog; 
 
} 
 
function initialCap(str) { 
 
    return str.charAt(0).toUpperCase()+str.substring(1); 
 
} 
 
var dialog=""; 
 

 
function mainroutine() { 
 
    uinput = document.getElementById("BasicTextArea4").value; 
 
    dialog = dialog + "User: " + uinput + '\r' + "\n"; 
 
    conversationpatterns(); 
 
    dialog = dialog + '\r' + "\n"; 
 
    updatescreen(); 
 
} 
 

 
function conversationpatterns() { 
 
    for (i = 0; i < convpatterns.length; i++) { 
 
    re = new RegExp(convpatterns[i][0], "i"); 
 
    if (re.test(uinput)) { 
 
     len = convpatterns[i].length - 1; 
 
     index = Math.ceil(len * Math.random()); 
 
     reply = convpatterns[i][index]; 
 
     if (typeof reply === "function") reply = reply(uinput); 
 
     soutput = uinput.replace(re, reply); 
 
     soutput = initialCap(soutput); 
 
     dialog = dialog + "Avatar: " + soutput + '\r' + "\n"; 
 
     break; 
 
    } 
 
    } 
 
}
pre { 
 
white-space: pre-wrap;  /* css-3 */ 
 
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ 
 
white-space: -pre-wrap;  /* Opera 4-6 */ 
 
white-space: -o-pre-wrap; /* Opera 7 */ 
 
word-wrap: break-word;  /* Internet Explorer 5.5+ */ 
 
}
<textarea id="BasicTextArea4"></textarea><button type="button" onclick="mainroutine()">Talk</button> 
 
<div id="update" class="pre"></div>

+0

我的意思是我如何可以鏈接功能需要()到新陣列我創建。所以像新陣列(「。*你好。*」,需要()) – michelle9090

+0

看到更新。只是測試它是一個字符串還是函數 – mplungjan

+0

謝謝!更新的答案是我所需要的。我用:'新陣列(「。*你好。*」,你好)'。它鏈接到函數Hello(),但是你知道爲什麼當我提交表單時,soutput textarea會一直顯示單詞「undefined」嗎? Hello()函數會執行,但是「undefined」總是顯示在textarea中。 – michelle9090

相關問題