簡體   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