findKeyword

/** * 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> * Tasks * 1) handle response to good or bad moods * 2) handle request for time or date * 3) handle request for time until an event (spring break, easter, summer) * 4) history of EA bot * 5) US History * 6) Misc * * */ 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 = ""; response = getRandomResponse(); if(findKeyword(statement, "cat", 0)>-1){ response = "How is your cat?"; }else{ response = "You did not talk about cats."; } return response; } /** * Pick a default response to use if nothing else fits. * @return a non-committal string */ private String getRandomResponse() { String response = ""; return response; } private int findKeyword(String statement, String goal, int startPos){ int spot = -1; statement = statement.toLowerCase(); statement = statement.replaceAll("[-+.^:,?!]",""); spot = statement.indexOf(" "+ goal +" "); if(spot==-1){ String newWord = statement.substring(0,goal.length()); if(newWord.equals(goal) && ( statement.length()==goal.length() || statement.substring(goal.length(),goal.length()+1).equals(" "))){ spot = 0; } } if(spot==-1){ String newWord = statement.substring(statement.length()-goal.length(),statement.length()); int look = statement.length()-(goal.length()+1); if(newWord.equals(goal) && ( statement.length()==goal.length() || statement.substring(look, look+1).equals(" "))){ spot = statement.length() - goal.length(); } } return spot; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.