Advertisement
StefanTobler

MAGPIE4

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