Advertisement
StefanTobler

TERM 2 MAGPIE3

Jan 19th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. /**
  2.  * A program to carry on conversations with a human user.
  3.  * This version:
  4.  * <ul><li>
  5.  *    Uses advanced search for keywords
  6.  * </li></ul>
  7.  *    
  8.  * @author Laurie White
  9.  * @version April 2012
  10.  */
  11. public class Magpie3
  12. {
  13.  /**
  14.   * Get a default greeting
  15.   *
  16.   * @return a greeting
  17.   */
  18.  public String getGreeting()
  19.  {
  20.   return "Hello, let's talk.";
  21.  }
  22.  
  23.  /**
  24.   * Gives a response to a user statement
  25.   *
  26.   * @param statement
  27.   *            the user statement
  28.   * @return a response based on the rules given
  29.   */
  30.  public String getResponse(String statement)
  31.  {
  32.   String response = "";
  33.   if (statement.length() == 0)
  34.   {
  35.    response = "Say something, please.";
  36.   }
  37.   else if (findKeyword(statement, "no") >= 0)
  38.   {
  39.    response = "Why so negative?";
  40.   }
  41.   else if (findKeyword(statement, "mother") >= 0
  42.     || findKeyword(statement, "father") >= 0
  43.     || findKeyword(statement, "sister") >= 0
  44.     || findKeyword(statement, "brother") >= 0)
  45.   {
  46.    response = "Tell me more about your family.";
  47.   }
  48.  
  49.   // Change what is in the red then delete this line.
  50.   else if (findKeyword(statement,"dog") >= 0
  51.     || findKeyword(statement,"cat") >= 0
  52.     || findKeyword(statement,"hampster") >= 0
  53.     || findKeyword(statement,"ferret") >= 0)
  54.   {
  55.    response = "Tell me more about your pets.";
  56.   }
  57.   // Just the stuff between these two comments. Delete
  58.   // Change what is in the red then delete this line.
  59.   else if (findKeyword(statement,"Davis") >= 0
  60.     || findKeyword(statement,"Horton") >= 0)
  61.   {
  62.    response = "What teachers are those?";
  63.   }
  64.   // Just the stuff between these two comments. Delete
  65.   // Change what is in the red then delete this line.
  66.   else if (statement.trim().length() == 0)
  67.   {
  68.    response = "Only quite?";
  69.   }
  70.   // Just the stuff between these two comments. Delete
  71.   // Change what is in the red then delete this line.
  72.   else if (findKeyword(statement,"sailing") >= 0
  73.           || findKeyword(statement,"sail") >= 0
  74.           || findKeyword(statement,"boat") >= 0)
  75.   {
  76.    response = "Do you like sailing too?";
  77.   }
  78.   // Just the stuff between these two comments. Delete
  79.   // Change what is in the red then delete this line.
  80.   else if (statement.indexOf("sailing") >= 0
  81.           || statement.indexOf("sail") >= 0
  82.           || statement.indexOf("boat") >= 0)
  83.   {
  84.    response = "Do you like sailing too?";
  85.   }
  86.   // Just the stuff between these two comments. Delete
  87.  
  88.   else
  89.   {
  90.    response = getRandomResponse();
  91.   }
  92.   return response;
  93.  }
  94.  
  95.  /**
  96.   * Search for one word in phrase. The search is not case
  97.   * sensitive. This method will check that the given goal
  98.   * is not a substring of a longer string (so, for
  99.   * example, "I know" does not contain "no").
  100.   *
  101.   * @param statement
  102.   *            the string to search
  103.   * @param goal
  104.   *            the string to search for
  105.   * @param startPos
  106.   *            the character of the string to begin the
  107.   *            search at
  108.   * @return the index of the first occurrence of goal in
  109.   *         statement or -1 if it's not found
  110.   */
  111.  private int findKeyword(String statement, String goal,
  112.    int startPos)
  113.  {
  114.   String phrase = statement.trim();
  115.   // The only change to incorporate the startPos is in
  116.   // the line below
  117.   int psn = phrase.toLowerCase().indexOf(
  118.     goal.toLowerCase(), startPos);
  119.  
  120.   // Refinement--make sure the goal isn't part of a
  121.   // word
  122.   while (psn >= 0)
  123.   {
  124.    // Find the string of length 1 before and after
  125.    // the word
  126.    String before = " ", after = " ";
  127.    if (psn > 0)
  128.    {
  129.     before = phrase.substring(psn - 1, psn)
  130.       .toLowerCase();
  131.    }
  132.    if (psn + goal.length() < phrase.length())
  133.    {
  134.     after = phrase.substring(
  135.       psn + goal.length(),
  136.       psn + goal.length() + 1)
  137.       .toLowerCase();
  138.    }
  139.  
  140.    // If before and after aren't letters, we've
  141.    // found the word
  142.    if (((before.compareTo("a") < 0) || (before
  143.      .compareTo("z") > 0)) // before is not a
  144.            // letter
  145.      && ((after.compareTo("a") < 0) || (after
  146.        .compareTo("z") > 0)))
  147.    {
  148.     return psn;
  149.    }
  150.  
  151.    // The last position didn't work, so let's find
  152.    // the next, if there is one.
  153.    psn = phrase.indexOf(goal.toLowerCase(),
  154.      psn + 1);
  155.  
  156.   }
  157.  
  158.   return -1;
  159.  }
  160.  
  161.  /**
  162.   * Search for one word in phrase. The search is not case
  163.   * sensitive. This method will check that the given goal
  164.   * is not a substring of a longer string (so, for
  165.   * example, "I know" does not contain "no"). The search
  166.   * begins at the beginning of the string.
  167.   *
  168.   * @param statement
  169.   *            the string to search
  170.   * @param goal
  171.   *            the string to search for
  172.   * @return the index of the first occurrence of goal in
  173.   *         statement or -1 if it's not found
  174.   */
  175.  private int findKeyword(String statement, String goal)
  176.  {
  177.   return findKeyword(statement, goal, 0);
  178.  }
  179.  
  180.  /**
  181.   * Pick a default response to use if nothing else fits.
  182.   *
  183.   * @return a non-committal string
  184.   */
  185.  private String getRandomResponse()
  186.  {
  187.   final int NUMBER_OF_RESPONSES = 4;
  188.   double r = Math.random();
  189.   int whichResponse = (int) (r * NUMBER_OF_RESPONSES);
  190.   String response = "";
  191.  
  192.   if (whichResponse == 0)
  193.   {
  194.    response = "Interesting, tell me more.";
  195.   }
  196.   else if (whichResponse == 1)
  197.   {
  198.    response = "Hmmm.";
  199.   }
  200.   else if (whichResponse == 2)
  201.   {
  202.    response = "Do you really think so?";
  203.   }
  204.   else if (whichResponse == 3)
  205.   {
  206.    response = "You don't say.";
  207.   }
  208.  
  209.   return response;
  210.  }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement