是否可以将函数链接到数组
Is it possible to link a function to array?
所以我正在构建一个聊天机器人,我在网上获取了一个开源代码,并希望自己修改聊天脚本。源代码的脚本是使用新数组构建的,下面是一些脚本示例:
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>
相关文章:
- Javascript排序索引链接数组
- 如何在不链接/jquery的情况下使用方法应用css属性数组
- 如何通过分配单个变量来减少此脚本中的链接数
- Javascript组将键值链接到数组中
- 序列promise链与数组数据,如何解析promise链
- 如何在jquery中链接多组动画
- Javascript:如何加入所有链接(数组)
- 是否可以将函数链接到数组
- 链接到数组中的对象的 Javascript 对象引用
- 基于相同键链接数组数据
- 带范围的数字的角度链接数组
- 如何返回整个网站的超链接数和ID
- CasperJS-如何打开链接数组中的所有链接
- 在jQuery中更改链接数组的文本
- 数组排序创建链接数组
- 将复选框链接到数组项
- JavaScript链接数组
- 在javascript中生成深度对象中所有属性链的数组
- 如何按链接对链接数组进行排序
- 当我迭代图像链接数组并使用innerHTML显示它们时,为什么IMG标记不能工作?