繁体   English   中英

聊天机器人从错误的响应数组返回

[英]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);
}

和我的didContain方法:

    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在已知词内,如果触发词不在另一个词内,则返回该词的索引,否则返回-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数组的响应将永远不会返回,它将进入下一行并从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);
}

同样, doesContaindoesContain函数始终返回true。 第二个return语句应更改为return false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM