Search code examples
javajava.util.scannerchatbotmirroring

Java chatbot mirroring and canned responses


So I got this java chatbot program that I'm working on.

Depending on what the user says, the chatbot either gives a canned response canned_phrases or a mirrored response if the user's response has one of the keywords I love pizza --> you love pizza.

The problem is that the chatbot does not give back the mirrored version. I think the problem is that the words are being overwritten by each other but I'm not sure how to fix that.

Thank for your help!

import java.util.Random;
import java.util.Scanner;

public class Conversation {

    public static void main(String[] args) {
        String[] canned_phrases = {"Yes",
                                   "Hmmm. Please tell me more news",
                                   "Of course",
                                   "Okay",
                                   "Interesting...",
                                   "Indeed"};
        int canned_times = canned_phrases.length;

        Scanner conversation_start = new Scanner(System.in);
        System.out.println("\nWelcome!\n");
        System.out.println("How many rounds of conversation would you like to have?\n");
        int rounds = conversation_start.nextInt();
        conversation_start.nextLine();
        String[] transcript = new String[2 * rounds + 1];
        transcript[0] = "Sounds great! How are you doing today?";
        System.out.println(transcript[0]);
        for (int i = 0; i < rounds; i++) {
            String user_words = conversation_start.nextLine();
            String mirrored;
            String new_version = user_words.replace("I", "you");
            new_version = new_version.replace("me", "you");
            new_version = new_version.replace("am", "are");
            new_version = new_version.replace("you", "I");
            new_version = new_version.replace("my", "your");
            new_version = new_version.replace("your", "my");
            new_version = new_version.replace("my", "you");
            if (!new_version.equals(user_words)) {
                mirrored = new_version;
            }
            else {
                mirrored = canned_phrases[(int) Math.floor(canned_times * Math.random())];
            }
            System.out.println(mirrored);
            transcript[2 * i + 1] = user_words;
            transcript[2 * i + 1] = mirrored;
        }
        System.out.println("Thank you for chatting with me! Come back soon!");
        System.out.println(" ");
        System.out.println("TRANSCRIPT ");
        for (int i = 0; i <= transcript.length; i++) {
            System.out.println(transcript[i]);
        }
        System.exit(0);
    }
}

Solution

  • I think the best way to achieve what you want is to split the user-entered sentence into words and change the individual words according to your rules, for example if the word is my then change it to your. In the below code, I iterate through all the words in order, changing each word as needed and appending them to a StringBuilder so that after iterating through all the words, the StringBuilder contains the mirrored sentence.

    (More notes after the code.)

    import java.util.Random;
    import java.util.Scanner;
    
    public class Conversation {
    
        public static void main(String[] args) {
            String[] cannedPhrases = {"Yes",
                                      "Hmmm. Please tell me more news",
                                      "Of course",
                                      "Okay",
                                      "Interesting...",
                                      "Indeed"};
            int cannedTimes = cannedPhrases.length;
            Random rand = new Random();
            Scanner conversationStart = new Scanner(System.in);
            System.out.println("\nWelcome!\n");
            System.out.println("How many rounds of conversation would you like to have?\n");
            int rounds = conversationStart.nextInt();
            conversationStart.nextLine();
            String[] transcript = new String[2 * rounds];
            transcript[0] = "Sounds great! How are you doing today?";
            System.out.println(transcript[0]);
            int index = -1;
            for (int i = 0; i < rounds; i++) {
                String userWords = conversationStart.nextLine();
                String mirrored;
                StringBuilder result = new StringBuilder();
                String[] words = userWords.split(" ");
                boolean first = true;
                for (String word : words) {
                    if (first) {
                        first = false;
                    }
                    else {
                        result.append(' ');
                    }
                    switch (word) {
                        case "I":
                            word = "you";
                            break;
                        case "me":
                            word = "you";
                            break;
                        case "am":
                            word = "are";
                            break;
                        case "you":
                            word = "I";
                            break;
                        case "my":
                            word = "your";
                            break;
                        case "your":
                            word = "my";
                            break;
                    }
                    result.append(word);
                }
                String newVersion = result.toString();
                if (!newVersion.equals(userWords)) {
                    mirrored = newVersion;
                }
                else {
                    mirrored = cannedPhrases[rand.nextInt(cannedTimes)];
                }
                System.out.println(mirrored);
                transcript[++index] = userWords;
                transcript[++index] = mirrored;
            }
            System.out.println("Thank you for chatting with me! Come back soon!");
            System.out.println(" ");
            System.out.println("TRANSCRIPT ");
            for (int i = 0; i < transcript.length; i++) {
                System.out.println(transcript[i]);
            }
            System.exit(0);
        }
    }
    
    • You should try to adhere to Java naming conventions. I changed the variable names in your code accordingly.
    • Rather than manipulate the [first] for loop variable i to handle both the conversation round and the transcript index, I used a separate variable, named index for the transcript.
    • Switching on string was added in Java 7.
    • The for loop that prints the transcript was wrong. The terminating condition should be i < transcript.length (and not i <= transcript.length).
    • The above code assumes that the user-entered sentence consists of words separated by single spaces. If you want more sophisticated handling of the user-entered sentence, for example handling punctuation, like commas, periods, etc, then you will need to change the split method's regular expression.

    Here is output from a sample run:

    
    Welcome!
    
    How many rounds of conversation would you like to have?
    
    2
    Sounds great! How are you doing today?
    OK
    Of course
    Why do you say that
    Why do I say that
    Thank you for chatting with me! Come back soon!
     
    TRANSCRIPT 
    OK
    Of course
    Why do you say that
    Why do I say that