Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // MagPie
- // Spec: Create a program to carry on conversations with a human user.
- /**
- * A program to carry on conversations with a human user.
- * This is the initial version that:
- * <ul><li>
- * Uses indexOf to find strings
- * </li><li>
- * Handles responding to simple words and phrases
- * </li></ul>
- * This version uses a nested if to handle default responses.
- * @author Laurie White
- * @version April 2012
- */
- public class MagPie
- {
- /**
- * Get a default greeting
- * @return a greeting
- */
- public String getGreeting()
- {
- return "Hello, let's talk.";
- }
- /**
- * Gives a response to a user statement
- *
- * @param statement
- * the user statement
- * @return a response based on the rules given
- */
- public String getResponse(String statement)
- {
- String response = "";
- if (findKeyword(statement, "no", 0) >= 0)
- {
- response = "Why so negative?";
- }
- else if (findKeyword(statement, "mother", 0) >= 0
- || findKeyword(statement, "father", 0) >= 0
- || findKeyword(statement, "sister", 0) >= 0
- || findKeyword(statement, "brother", 0) >= 0)
- {
- response = "Tell me more about your family.";
- }
- else if (findKeyword(statement, "cat", 0) >= 0
- || findKeyword(statement, "dog", 0) >= 0)
- {
- if (findKeyword(statement, "cancat", 0) >= 0)
- {
- response = "Cancat is cool.";
- }
- else if (findKeyword(statement, "hotdog", 0) >= 0)
- {
- response = "I love hotdogs!";
- }
- response = "Tell me more about your pets.";
- }
- else if (findKeyword(statement, "Ascione", 0) >= 0)
- {
- response = "He's a great programmer!";
- }
- else if (findKeyword(statement, "I like", 0) >= 0)
- {
- response = "What do you like about it?";
- }
- else if (findKeyword(statement, "What's your name?", 0) >= 0
- || findKeyword(statement, "what's your name?", 0) >= 0
- || findKeyword(statement, "Whats your name?", 0) >= 0
- || findKeyword(statement, "whats your name?", 0) >= 0
- || findKeyword(statement, "What's your name", 0) >= 0
- || findKeyword(statement, "what's your name", 0) >= 0
- || findKeyword(statement, "Whats your name", 0) >= 0
- || findKeyword(statement, "whats your name", 0) >= 0)
- {
- response = "MagPie";
- }
- else if (statement.length() == 0)
- {
- response = "What was that?";
- }
- else
- {
- response = getRandomResponse();
- }
- return response;
- }
- /**
- * Pick a default response to use if nothing else fits.
- * @return a non-committal string
- */
- private String getRandomResponse()
- {
- final int NUMBER_OF_RESPONSES = 5;
- double r = Math.random();
- int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
- String response = "";
- if (whichResponse == 0)
- {
- response = "Interesting, tell me more.";
- }
- else if (whichResponse == 1)
- {
- response = "Hmmm.";
- }
- else if (whichResponse == 2)
- {
- response = "Do you really think so?";
- }
- else if (whichResponse == 3)
- {
- response = "You don't say.";
- }
- else if (whichResponse == 4)
- {
- response = "I don't care.";
- }
- return response;
- }
- /**
- * Search for one word in phrase. The search is not case
- * sensitive. This method will check that the given goal
- * is not a substring of a longer string (so, for
- * example, "I know" does not contain "no").
- *
- * @param statement
- * the string to search
- * @param goal
- * the string to search for
- * @param startPos
- * the character of the string to begin the
- * search at
- * @return the index of the first occurrence of goal in
- * statement or -1 if it's not found
- */
- private int findKeyword(String statement, String goal, int startPos)
- {
- String phrase = statement.trim();
- // The only change to incorporate the startPos is in
- // the line below
- int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
- // Refinement--make sure the goal isn't part of a
- // word
- while (psn >= 0)
- {
- // Find the string of length 1 before and after
- // the word
- String before = " ", after = " ";
- if (psn > 0)
- {
- before = phrase.substring(psn - 1, psn).toLowerCase();
- }
- if (psn + goal.length() < phrase.length())
- {
- after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
- }
- // If before and after aren't letters, we've
- // found the word
- if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) // before is not a letter
- && ((after.compareTo("a") < 0) || (after.compareTo("z") > 0)))
- {
- return psn;
- }
- // The last position didn't work, so let's find
- // the next, if there is one.
- psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
- }
- return -1;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // MagPie
- // Spec: Create a program to carry on conversations with a human user.
- import java.util.Scanner;
- /**
- * A simple class to run the Magpie class.
- * @author Laurie White
- * @version April 2012
- */
- public class MagPieClient
- {
- /**
- * Create a Magpie, give it user input, and print its replies.
- */
- public static void main(String[] args)
- {
- MagPie maggie = new MagPie();
- System.out.println (maggie.getGreeting());
- Scanner in = new Scanner (System.in);
- String statement = in.nextLine();
- while (!statement.equalsIgnoreCase("Bye"))
- {
- System.out.println(maggie.getResponse(statement));
- statement = in.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement