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;
}
}
}
我的意思是我如何可以鏈接功能需要()到新陣列我創建。所以像新陣列(「。*你好。*」,需要()) – michelle9090
看到更新。只是測試它是一個字符串還是函數 – mplungjan
謝謝!更新的答案是我所需要的。我用:'新陣列(「。*你好。*」,你好)'。它鏈接到函數Hello(),但是你知道爲什麼當我提交表單時,soutput textarea會一直顯示單詞「undefined」嗎? Hello()函數會執行,但是「undefined」總是顯示在textarea中。 – michelle9090