javascript - 是否可以将函数链接到数组?

标签 javascript arrays function cordova phonegap-build

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

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”,聊天机器人将从该数组中随机选择一个回复。我想知道是否可以将来自不同数组的用户输入链接到不同的函数中。因此,就像用户输入“我需要一个 friend ”一样,它不会从上面列出的选项中选择随机回复,而是链接到一个函数,例如 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>

关于javascript - 是否可以将函数链接到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35571446/

相关文章:

javascript - 在我按下按钮确认之前单击事件触发

php - 在多维数组中搜索

c# - 为什么不能通过其他函数调整数组大小?

c++ - 为什么我收到 clang 警告 : no previous prototype for function 'diff'

Javascript 函数存储在数组中与存储在变量中

javascript - 不透明背景中的非不透明文本

javascript - 不在选择标签下拉列表中选择以隐藏另一个输入元素的嵌套 Controller

javascript - pjax 是如何工作的?

arrays - 子数组的总和乘以子数组的最后一个元素

javascript - 确定是否存在静态命名的 JavaScript 函数以防止错误