Javascript chatbot app reply始终缺少变量的一个字母

Javascript chatbot app reply始终缺少变量的一个字母,javascript,html,Javascript,Html,我正在开发一个使用HTML和javascript的聊天机器人。我使用了一个在线提供的开源的ELIZA风格的代码作为我的出发点。但是,我注意到代码有一个问题: 例如,代码说: var convpatterns = new Array ( new Array (".*my name is (.*)\.", "Nice to meet you $1!"), new Array ("^I

我正在开发一个使用HTML和javascript的聊天机器人。我使用了一个在线提供的开源的ELIZA风格的代码作为我的出发点。但是,我注意到代码有一个问题:

例如,代码说:

var convpatterns = new Array (
new Array (".*my name is (.*)\.", "Nice to meet you $1!"),                                                          
new Array ("^I (?:wish |would like )(?:I could |I was able to |to be able to )(.*)\.","What would it be like to be able to $1?"));

uinput = ""
soutput = ""
dialog = ""

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 + "System: " + soutput +  '\r' + "\n";
      break;
    }
  }
}
var convpatterns=新数组(
新数组(“.*我的名字是(.*)\”,“很高兴认识你$1!”),
新数组(“^I(?:wish | like)(?:I can | I can | I can | to can | to | to | to | to | to | to | to | to | to | to |)(.*),“;
uinput=“”
soutput=“”
dialog=“”
函数mainproutine(){
uinput=document.mainscreen.BasicTextArea4.value;
dialog=dialog+“用户:”+uinput+'\r'+“\n”;
会话模式()
dialog=dialog+'\r'+“\n”;
updatescreen()
}
函数conversationpatterns(){
对于(i=0;i<0.length;i++){
re=新的RegExp(convpatterns[i][0],“i”);
如果(重新测试(输出)){
len=convpatterns[i].长度-1;
index=Math.ceil(len*Math.random());
答复=附件[i][index];
soutput=uinput.replace(re,reply);
soutput=初始CAP(soutput);
dialog=dialog+“系统:”+soutput+'\r'+“\n”;
打破
}
}
}
然而,如果我问机器人“我希望我能飞”,机器人会回答“能飞会是什么样子?”
注意到“苍蝇”在末尾缺少一个“y”字母。无论我键入什么,每次都会发生这种情况,例如“我的名字是Michelle”,机器人回答“很高兴认识你Michelle”,再次丢失变量的最后一个字母。

删除regexp中的最后一个点

new Array ("^I (?:wish |would like )(?:I could |I was able to |to be able to )(.*)\.","What would it be like to be able to $1?"))
----------------------------------------------------------------------------------^^
换成这个

new Array ("^I (?:wish |would like )(?:I could |I was able to |to be able to )(.*)","What would it be like to be able to $1?"))
您可以在此处进行测试: