简体   繁体   中英

Chat bot returning from incorrect array of responses

I'm working on a chat bot for an assignment, which takes in an input sentence, looks for certain trigger works within an Array, and from that randomly prints an output from another Array of responses. My problem is that when I type something such as "No", the bot responds with a response from the wrong Array. My getResponse Method:

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

and my doesContain method:

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

The indexOfKeyword method checks whether a trigger word is inside of another word, such as no is inside of know, and returns the index of the word if it is not within another word, otherwise it returns -1. Here is the indexOfKeyword method:

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

and finally, my getRandResponse method:

public static String getRandResponse(String[] respArray){return respArray[random.nextInt(respArray.length)]; }

Now my problem is that if I type "no"(which is a trigger word inside of the negatives array), or any trigger word from the array as the input, I get a random question as the output, and not a response from the negResponse array. As well is "doesContain = true" being printed, however it does not print the correct response.

You need to add a return to your function, or else the response from the negResponse array will never be returned and it will go onto the next line and return the response from 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);
}

Also, your doesContain function always returns true no matter what. The second return statement should be changed to return false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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