因此,我正在构建一个聊天机器人,并且已经在线获取了一个开放源代码,并希望自己修改聊天脚本。源代码的脚本是用新数组构建的,下面是一些脚本示例:

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;
        }
    }
}

最佳答案

我建议

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>

09-30 16:59
查看更多