所以我正在建立一个聊天机器人,我已经在线获取了一个开源代码,并希望自己修改聊天脚本。源代码的脚本是使用新数组构建的,下面是脚本的一些示例:
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?"),
因此,例如,如果用户键入" Hello",聊天机器人将随机选择该阵列中的一个回复。我想知道的是,如果可以将来自不同阵列的用户输入链接到不同的功能中。因此,如果用户键入"我需要一个朋友",而不是从上面列出的那些中选择随机回复,它将链接到一个函数,例如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 :(得分: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>