聊天机器人应用程序回复总是缺少变量的一个字母

chatbot app reply always missing a letter of the variable

本文关键字:一个 变量 回复 应用程序 机器人 聊天      更新时间:2023-09-26

我正在使用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;
    }
  }
}

然而,如果我问机器人"我希望我能飞",机器人会回答"能飞是什么感觉?"注意到"fly"在末尾缺少一个"y"字母。无论我键入什么,每次都会发生这种情况,例如"我叫Michelle",机器人回复"很高兴见到你Michell",再次缺少变量的最后一个字母。

删除正则表达式中的最后一个点

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?"))

你可以在这里测试:

https://regex101.com/r/hN2gA2/1