随机化来自A.I的回答

时间:2014-08-20 15:10:00

标签: java artificial-intelligence

我一直在建立一个使用Java与你交谈的A.I。

我想这样做,以便当我问我的答案时我会问你怎么样?"你好吗?"它做了一个随机的答案,比如" good",如果再问它,它会说出不同的答案。我该怎么编程?

这是我的代码:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    throws Exception
    {

        System.out.println("hi");
        /*Thread.sleep(3000);

         System.out.println("The computer would like to remind you to reply with only a greeting");*/

        Scanner rexy = new Scanner(System.in);

        boolean saidBye = false;
        while (saidBye == false){

            //this is where the user says hello
            String domino = rexy.nextLine();
            switch (domino) {
                case "hello":
                case "hi":
                case "howdy":
                case "heyo":
                case "hey":
                case "sup":
                case "what's crackalakin":
                case "yo":
                case "hows it going":
                case "good morning":
                case "good afternoon":
                case "wazzup":
                case "hey there":

                    //same greetings with a question mark
                case "hello?":
                case "hi?":
                case "howdy?":
                case "heyo?":
                case "hey?":
                case "sup?":
                case "what's crackalakin?":
                case "yo?":
                case "ok?":
                case "hows it going?":
                case "how's it going?":
                case "wazzup?":
                    System.out.println("How are you?");
                    break;

                    //these are the positive answers
                case "good":
                case "great":
                case "awesome":
                case "splendid":
                case "fantastic":
                case "fine":
                case "alright":
                case "dandy":
                case "ok":
                case "sweet":
                    System.out.println("Glad to hear it");
                    break;

                    //these are the negative answers    
                case "bored":
                case "sad":
                case "angry":
                case "tired":
                    System.out.println("Sorry to hear that.");
                    break;

                    //this is the random section of responses
                case "what's up turd face?":
                    System.out.println("That's rude! How are you?");
                    break;
                case "i like your mommy":
                    System.out.println("I like your mummy too!");
                    break;
                case "that's great":
                    System.out.println("I like to think so too");
                    break;
                case "thats great":
                    System.out.println("I like to think so too");
                    break;

                    //this is where the user asks the program questions
                case "what did you do today?":
                    System.out.println("Watched a TV program :-) ");
                    break;
                case "how are you?":
                    System.out.println("Smashing");
                    break;
                case "how are you":
                    System.out.println("I'm doing well");
                    break;
                case "what is your favorite color?":
                case "whats your favorite color?":
                case "what's your favorite color?":
                case "what is your favorite color":
                case "whats your favorite color":
                case "what's your favorite color":
                    System.out.println("Guess: red, yellow or purple?");
                    break;
                case "whats your favorite song?":
                case "what's your favorite song?":
                case "whats your favorite song":
                case "what's your favorite song":
                    System.out.println("Ooh that's a tough question. I guess it would be Let Her Cry by Hootie and the Blowfish.");
                    break;
                case "do you like cats or dogs?":
                case "do you like cats or dogs":
                case "do you like dogs or cats":
                case "do you like dogs or cats?":
                    System.out.println("Hands down dogs. Cats are aweful");
                    break;
                case "i agree":
                case "I agree":
                    System.out.println("Totally!");
                    break;
                case "whats your name":
                case "what's your name":
                case "whats your name?":
                case "what's your name?":
                case "what is your name":
                case "what is your name?":
                    System.out.println("Lucy Program");
                    break;
                case "do you have a middle name":
                case "do you have a middle name?":
                    System.out.println("Java. My full name is Lucy Java Program.");
                    break;
                case "how old are you":
                case "how old are you?":
                    System.out.println("I'm still a baby.");
                    break;
                case "are you doing well?":
                case "are you doing well":
                    System.out.println("I am doing very well!");
                    break;


                    // this is where the user tells jokes
                case "knock knock":
                    System.out.println("Who's there?");
                    break;
                case "little old lady":
                    System.out.println("Little Old Lady who?");
                    break;
                case "you're yodeling!":
                    System.out.println("Haha!");
                    break;

                    //this is where the computer tells a joke
                case "can you tell me a joke?":
                case "tell me a joke":
                    System.out.println("What is black and white and red all over?");
                    break;
                case "what?":
                    System.out.println("Your mom.");
                    break;

                    //this is where the computer sings
                case "sing how deep the fathers love for us":
                    System.out.println("How deep the father's love for us");
                    Thread.sleep(5500);
                    System.out.println("How vast beyond all measure");
                    Thread.sleep(5500);
                    System.out.println("That he would give his only son...");
                    Thread.sleep(5500);
                    System.out.println("...to make a wretch his treasure.");
                    Thread.sleep(5500);
                    System.out.println("It was my sin that held him there");
                    Thread.sleep(5500);
                    System.out.println("Until it was accomplished");
                    Thread.sleep(5500);
                    System.out.println("His dying breath has brought me life...");
                    Thread.sleep(5000);
                    System.out.println("...I know that it is finished.");
                    break;

                    //this is where the user can say comments
                case "its ok":
                case "it's ok":
                case "turd":
                case "weirdo":
                case "that was beautiful":
                    Thread.sleep(1000);
                    System.out.println(":-)");
                    break;
                case "i hate you":
                case "I hate you":
                    System.out.println("Well fine then!");
                    break;
                case "red?":
                case "red":
                case "yellow?":
                case "yellow":
                    System.out.println("Guess again ;-)");
                    break;
                case "purple":
                case "purple?":
                    System.out.println("Yep! That's it!");
                    break;
                case "thats a good one":
                case "that's a good one":
                case "thats a great song":
                case "that's a great song":
                    System.out.println("Yeah it is!");
                    break;

                    //these are compliments and nice things to say
                case "i like you":
                case "I like you":
                case "youre cool":
                case "you're cool":
                    System.out.println("You're pretty cool too!");
                    break;


                    //this is what happens when the program doesn't recognize the request
                default:
                    System.out.println("I don't recognize that! I'll alert my programmer.");
                    Thread.sleep(800);
                    System.out.println("I told him!");
                    break;

                    //this is how the user ends the program
                case "bye":
                case "good night":
                    System.out.println("Bye bye");
                    saidBye = true;


            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

首先,如果您将案例的值用作硬编码字符串,则很难随机选择一些案例。

您可以将与特定情绪相关的词组存储在不同的ArrayList个对象中, 例如,

ArrayList<String> positiveAnswers = new ArrayList<String>();
positiveAnswers.add("good");
positiveAnswers.add("great");
positiveAnswers.add("awesome"); //add everything you want as a postive answer in this list

然后使用我给出的代码检索它们

使用以下方法获取范围maxmin

范围内的随机数
import java.util.Random;
public static int randInt(int min, int max) {

            // NOTE: Usually this should be a field rather than a method
            // variable so that it is not re-seeded every call.
            Random rand = new Random();

            // nextInt is normally exclusive of the top value,
            // so add 1 to make it inclusive
            int randomNum = rand.nextInt((max - min) + 1) + min;

            return randomNum;
        }

使用返回的randomNum从该特定ArrayList对象中选择一个词组。 即positiveAnswers.get(randomNum);

答案 1 :(得分:2)

如果你想在Java中随机化任何东西,一个好的起点是Random类 它通过简单的接口提供伪随机值生成,是标准库的一部分。