聊天机器人从不正确的响应数组中返回
Chat bot returning from incorrect array of responses
我正在为一项任务开发一个聊天机器人,它接受一个输入句子,在一个数组中寻找特定的触发器,然后从中随机打印另一个响应数组的输出。我的问题是,当我键入诸如 "No" 之类的内容时,机器人会使用错误数组的响应进行响应。
我的 getResponse 方法:
public static String getResponse(String input) {
if(doesContain(input, negatives)){
getRandResponse(negResponse);
}
//If none of the criteria is met, the bot will ask a random question from the questions array.
return getRandResponse(quesResponse);
}
和我的 doesContain 方法:
public static boolean doesContain (String input, String[] tArr){
//Where tArr is an array of trigger words, and input is the users input
for(String i: tArr){
if(indexOfKeyword(input, i) != -1){
System.out.println("doesContain = true");
return true;
}
}
return false;
}
indexOfKeyword方法检查触发词是否在另一个词的内部,比如no在know内部,returns如果不在另一个词中则索引该词,否则returns -1。这是 indexOfKeyword 方法:
public static int indexOfKeyword( String s, String keyword ) {
s.toLowerCase();
keyword.toLowerCase();
int startIdx = s.indexOf( keyword );
while ( startIdx >= 0 ) {
String before = " ", after = " ";
if ( startIdx > 0 ) {
before = s.substring(startIdx - 1, startIdx);
}
int endIdx = startIdx + keyword.length();
if ( endIdx < s.length() ){
after = s.substring(endIdx, endIdx + 1);
}
if ((before.compareTo("a") < 0 || before.compareTo("z") > 0) && (after.compareTo("a") < 0 || after.compareTo("z") > 0)) {
return startIdx;
}
startIdx = s.indexOf(keyword, startIdx + 1);
}
return -1;
}
最后,我的 getRandResponse 方法:
public static String getRandResponse(String[] respArray){return respArray[random.nextInt(respArray.length)]; }
现在我的问题是,如果我键入 "no"(这是负数数组中的触发词)或数组中的任何触发词作为输入,我会得到一个随机问题作为输出,而不是来自 negResponse 数组的响应。也正在打印 "doesContain = true",但是它没有打印正确的响应。
您需要向您的函数添加一个 return,否则来自 negResponse
数组的响应将永远不会被 return 编辑,它将进入下一行并且 return 来自 quesResponse
的回复:
public static String getResponse(String input) {
if(doesContain(input, negatives)){
// add return here:
return getRandResponse(negResponse);
}
//If none of the criteria is met, the bot will ask a random question from the questions array.
return getRandResponse(quesResponse);
}
此外,无论如何,您的 doesContain
函数总是 return 为真。第二个 return 语句应更改为 return false
.
我正在为一项任务开发一个聊天机器人,它接受一个输入句子,在一个数组中寻找特定的触发器,然后从中随机打印另一个响应数组的输出。我的问题是,当我键入诸如 "No" 之类的内容时,机器人会使用错误数组的响应进行响应。 我的 getResponse 方法:
public static String getResponse(String input) {
if(doesContain(input, negatives)){
getRandResponse(negResponse);
}
//If none of the criteria is met, the bot will ask a random question from the questions array.
return getRandResponse(quesResponse);
}
和我的 doesContain 方法:
public static boolean doesContain (String input, String[] tArr){
//Where tArr is an array of trigger words, and input is the users input
for(String i: tArr){
if(indexOfKeyword(input, i) != -1){
System.out.println("doesContain = true");
return true;
}
}
return false;
}
indexOfKeyword方法检查触发词是否在另一个词的内部,比如no在know内部,returns如果不在另一个词中则索引该词,否则returns -1。这是 indexOfKeyword 方法:
public static int indexOfKeyword( String s, String keyword ) {
s.toLowerCase();
keyword.toLowerCase();
int startIdx = s.indexOf( keyword );
while ( startIdx >= 0 ) {
String before = " ", after = " ";
if ( startIdx > 0 ) {
before = s.substring(startIdx - 1, startIdx);
}
int endIdx = startIdx + keyword.length();
if ( endIdx < s.length() ){
after = s.substring(endIdx, endIdx + 1);
}
if ((before.compareTo("a") < 0 || before.compareTo("z") > 0) && (after.compareTo("a") < 0 || after.compareTo("z") > 0)) {
return startIdx;
}
startIdx = s.indexOf(keyword, startIdx + 1);
}
return -1;
}
最后,我的 getRandResponse 方法:
public static String getRandResponse(String[] respArray){return respArray[random.nextInt(respArray.length)]; }
现在我的问题是,如果我键入 "no"(这是负数数组中的触发词)或数组中的任何触发词作为输入,我会得到一个随机问题作为输出,而不是来自 negResponse 数组的响应。也正在打印 "doesContain = true",但是它没有打印正确的响应。
您需要向您的函数添加一个 return,否则来自 negResponse
数组的响应将永远不会被 return 编辑,它将进入下一行并且 return 来自 quesResponse
的回复:
public static String getResponse(String input) {
if(doesContain(input, negatives)){
// add return here:
return getRandResponse(negResponse);
}
//If none of the criteria is met, the bot will ask a random question from the questions array.
return getRandResponse(quesResponse);
}
此外,无论如何,您的 doesContain
函数总是 return 为真。第二个 return 语句应更改为 return false
.