2015-12-02 18 views
0

我正在开发一个使用HTML和JavaScript的聊天机器人。我使用在线提供的开源ELIZA风格代码作为我的出发点。不过我注意到的代码的问题:chatbot应用程序的回复总是缺少变量的一个字母

例如代码说:

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; 
    } 
    } 
} 

但是如果我问机器人“我希望我能飞”,机器人就会回复“那将是什么样能够fl“ 注意到”飞“在最后缺少一个”y“字母。它每次都会发生,不管我输入什么,例如“我的名字是米歇尔”,机器人回复“很高兴认识你米歇尔”,再次错过了变量的最后一个字母。

回答

0

这个

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

+1

感谢那些解决了这个问题对我来说! – michelle9090

+0

欢迎您:) –