Advertisement
Guest User

Magpie4

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