In class, we briefly discussed chatbot programs. One well-‐known chatbot is ELIZA, which was written by computer scientist Joseph Weizenbaum in the 1960s to parody a conversation with a psychotherapist. ELIZA used scripted replies to reply to simple sentences typed in by the user. For this project, you will implement a simple chatbot that mostly simulates ELIZA (note that, due to this version's extreme simplicity, many of its responses will be grammatically incorrect and may appear to be nonsensical; this is all right for our purposes).
Start by defining a new Java class named ChatBot. This class contains the following methods:
A public method named getResponse(). This method takes a String as its input, and returns a String value, according to the rules that follow (use the longestWord() method from step 1(b) to find the longest word X in the input String):
A private method named longestWord(). This method takes a String (representing a sentence) as its input, and returns a String that matches the longest single word in the input. You may assume that the input does not contain any punctuation, and that the words are all separated by spaces.
We will use a Scanner to process the input String one word at a time. So far, we have used Scanner to read from the keyboard (System.in), but we can also tell a Scanner to use a String value as its input source.
In particular, we will use two new (to us) Scanner methods:
hasNext() — this method returns true if the Scanner’s input source has at least one more word left in it to be read; otherwise, the method returns false.
next() — this method returns the next “word” in the Scanner’s input source. A word is defined as a sequence of characters separated from what precedes or follows it by spaces. For example, the String “abc 123 def 456” has four words in it.
Your longestWord() method will follow the procedure below:
When the loop ends, return longest.
Create a second class to test your ChatBot class. Your program should create a new ChatBot object and then ask "What would you like to talk about?" Until the user types "Goodbye", your program should enter a loop where it passes the user’s input to getResponse(), prints the String returned from that method, and then reads in a new sentence from the user.
A pseudocode algorithm for main() would look something like the following:
Create a new Scanner to read from the keyboard
Create a new ChatBot named e (for example)
prompt <- "What would you like to talk about?"
Print prompt input <- user input
while input does not equal "Goodbye":
print e.getResponse(input)
Read in user input
Sample Program Execution (program output is in boldface, user input is in italics)
What would you like to talk about?
how are you?
I'm not important. Let's talk about you instead.
I like Java programming Now we are getting somewhere. How does programming affect you the most?
bugs make me sad
Tell me more about bugs.
they mean the code is not right
Why do you think right is important?
CSE 110 is fun Maybe we should move on. Is there something else you would like to talk about?
Goodbye