是否可以将函数链接到数组

Is it possible to link a function to array?

本文关键字:链接 数组 函数 是否      更新时间:2023-09-26

所以我正在构建一个聊天机器人,我在网上获取了一个开源代码,并希望自己修改聊天脚本。源代码的脚本是使用新数组构建的,下面是一些脚本示例:

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>