Advertisement
Guest User

magpie4

a guest
Dec 11th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.17 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 to", 0) >= 0)
  53.         {
  54.             response = transformIWantToStatement(statement);
  55.         }
  56.         else if (findKeyword(statement, "I want", 0) >= 0)
  57.         {
  58.             response = transformIWantStatement(statement);
  59.         }
  60.         else
  61.         {
  62.             // Look for a two word (you <something> me)
  63.             // pattern
  64.             int psn = findKeyword(statement, "you", 0);
  65.             int psnI = findKeyword(statement, "I", 0);
  66.            
  67.             if (psn >= 0
  68.                     && findKeyword(statement, "me", psn) >= 0)
  69.             {
  70.                 response = transformYouMeStatement(statement);
  71.             }else if(psnI>=0&&findKeyword(statement, "you", psnI+1)>=0){              
  72.                 response=transformIYouStatement(statement);            
  73.             }
  74.             else
  75.             {
  76.                 response = getRandomResponse();
  77.             }
  78.         }
  79.         return response;
  80.     }
  81.  
  82.     /**
  83.      * Take a statement with "I want to <something>." and transform it into
  84.      * "What would it mean to <something>?"
  85.      * @param statement the user statement, assumed to contain "I want to"
  86.      * @return the transformed statement
  87.      */
  88.     private String transformIWantToStatement(String statement)
  89.     {
  90.         //  Remove the final period, if there is one
  91.         statement = statement.trim();
  92.         String lastChar = statement.substring(statement
  93.                 .length() - 1);
  94.         if (lastChar.equals("."))
  95.         {
  96.             statement = statement.substring(0, statement
  97.                     .length() - 1);
  98.         }
  99.         int psn = findKeyword (statement, "I want to", 0);
  100.         int startSubstring=psn + 9;
  101.         String restOfStatement = statement.substring(psn + startSubstring).trim();
  102.         return "What would it mean to " + restOfStatement + "?";
  103.     }
  104.  
  105.     /**
  106.      * Take a statement with "I want to <something>." and transform it into
  107.      * "What would it mean to <something>?"
  108.      * @param statement the user statement, assumed to contain "I want to"
  109.      * @return the transformed statement
  110.      */
  111.     private String transformIWantStatement(String statement)
  112.     {
  113.         //  Remove the final period, if there is one
  114.         statement = statement.trim();
  115.         String lastChar = statement.substring(statement
  116.                 .length() - 1);
  117.         if (lastChar.equals("."))
  118.         {
  119.             statement = statement.substring(0, statement
  120.                     .length() - 1);
  121.         }
  122.         int psn = findKeyword (statement, "I want", 0);
  123.         int startSubstring=psn + 6;
  124.         String restOfStatement = statement.substring(psn + startSubstring).trim();
  125.         return "Why would you want" + restOfStatement + "?";
  126.     }
  127.  
  128.     /**
  129.      * Take a statement with "you <something> me" and transform it into
  130.      * "What makes you think that I <something> you?"
  131.      * @param statement the user statement, assumed to contain "you" followed by "me"
  132.      * @return the transformed statement
  133.      */
  134.     private String transformYouMeStatement(String statement)
  135.     {
  136.         //  Remove the final period, if there is one
  137.         statement = statement.trim();
  138.         String lastChar = statement.substring(statement
  139.                 .length() - 1);
  140.         if (lastChar.equals("."))
  141.         {
  142.             statement = statement.substring(0, statement
  143.                     .length() - 1);
  144.         }
  145.  
  146.         int psnOfYou = findKeyword (statement, "you", 0);
  147.         int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
  148.  
  149.         String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
  150.         return "What makes you think that I " + restOfStatement + " you?";
  151.     }
  152.  
  153.     /**
  154.      * Take a statement with "you <something> me" and transform it into
  155.      * "What makes you think that I <something> you?"
  156.      * @param statement the user statement, assumed to contain "you" followed by "me"
  157.      * @return the transformed statement
  158.      */
  159.     private String transformIYouStatement(String statement)
  160.     {
  161.         //  Remove the final period, if there is one
  162.         statement = statement.trim();
  163.         String lastChar = statement.substring(statement
  164.                 .length() - 1);
  165.         if (lastChar.equals("."))
  166.         {
  167.             statement = statement.substring(0, statement
  168.                     .length() - 1);
  169.         }
  170.  
  171.         int psnOfI = findKeyword (statement, "I", 0);
  172.         int psnOfYou = findKeyword (statement, "you", psnOfI + 1);
  173.  
  174.         String restOfStatement = statement.substring(psnOfI + 3, psnOfYou).trim();
  175.         return "Why do you want to " + restOfStatement + " me?";
  176.     }
  177.  
  178.  
  179.  
  180.     /**
  181.      * Search for one word in phrase.  The search is not case sensitive.
  182.      * This method will check that the given goal is not a substring of a longer string
  183.      * (so, for example, "I know" does not contain "no").  
  184.      * @param statement the string to search
  185.      * @param goal the string to search for
  186.      * @param startPos the character of the string to begin the search at
  187.      * @return the index of the first occurrence of goal in statement or -1 if it's not found
  188.      */
  189.     private int findKeyword(String statement, String goal, int startPos)
  190.     {
  191.         String phrase = statement.trim();
  192.         //  The only change to incorporate the startPos is in the line below
  193.         int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
  194.  
  195.         //  Refinement--make sure the goal isn't part of a word
  196.         while (psn >= 0)
  197.         {
  198.             //  Find the string of length 1 before and after the word
  199.             String before = " ", after = " ";
  200.             if (psn > 0)
  201.             {
  202.                 before = phrase.substring (psn - 1, psn).toLowerCase();
  203.             }
  204.             if (psn + goal.length() < phrase.length())
  205.             {
  206.                 after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
  207.             }
  208.  
  209.             //  If before and after aren't letters, we've found the word
  210.             if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0))  //  before is not a letter
  211.                     && ((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0)))
  212.             {
  213.                 return psn;
  214.             }
  215.  
  216.             //  The last position didn't work, so let's find the next, if there is one.
  217.             psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
  218.  
  219.         }
  220.  
  221.         return -1;
  222.     }
  223.  
  224.     /**
  225.      * Search for one word in phrase.  The search is not case sensitive.
  226.      * This method will check that the given goal is not a substring of a longer string
  227.      * (so, for example, "I know" does not contain "no").  The search begins at the beginning of the string.  
  228.      * @param statement the string to search
  229.      * @param goal the string to search for
  230.      * @return the index of the first occurrence of goal in statement or -1 if it's not found
  231.      */
  232.     private int findKeyword(String statement, String goal)
  233.     {
  234.         return findKeyword (statement, goal, 0);
  235.     }
  236.  
  237.  
  238.  
  239.     /**
  240.      * Pick a default response to use if nothing else fits.
  241.      * @return a non-committal string
  242.      */
  243.     private String getRandomResponse()
  244.     {
  245.         final int NUMBER_OF_RESPONSES = 4;
  246.         double r = Math.random();
  247.         int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  248.         String response = "";
  249.  
  250.         if (whichResponse == 0)
  251.         {
  252.             response = "Interesting, tell me more.";
  253.         }
  254.         else if (whichResponse == 1)
  255.         {
  256.             response = "Hmmm.";
  257.         }
  258.         else if (whichResponse == 2)
  259.         {
  260.             response = "Do you really think so?";
  261.         }
  262.         else if (whichResponse == 3)
  263.         {
  264.             response = "You don't say.";
  265.         }
  266.  
  267.         return response;
  268.     }
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement