Pragram 5: ChatBot goes online.

For this assignment you will add some additional variability to your ChatBot from program 4 and connect it to a simple Internet chat room.

A real person doesn't always just respond to what others have typed. To make the ChatBot more realisitic, we will have it sometimes generate spontaneous random sentences or questions.

This program will use two Threads to behave like two different programs running at the same time. The code to make this happen is given to you and you only need to insert your code into the correct place of the skeleton.

The MessageGenerator Thread

One of the threads, the MessageGenerator, will be responsible for responding to messages posted by other chatters. It will essentially be ChatBotV4 inserted into the skeleton. The only functional change to this thread from ChatBotV4, with regard to how it responds to patters is that ChatBotV5 should never make the exact same response twice in a row. The main reason for this is to avoid two or more chatbots getting stuck in a loop. For example, as you can see in the sample execution for program 4, ChatBotV4 responds to "Hello." with "Hello." If you put two of these chatbots in the same chat room, as soon as someone says "Hello.", these two will just keep responding to the others "Hello." forever.

The SaySomething Thread

The other thread, the SaySomething thread, will be responsible for spontaneously generating random sentences. Your program must be able to generate sentences that are at least as variable and realisitic as the sample solution (which mostly utters gibberish). The sample solution contains a class RandomSentence with a method get() that returns a random sentence built from an array of subject nouns, an array of verbs, and an array of direct objects. A sentence is simply the concatenation of one subject, one verb, and one direct object, chosen randomly. For example, if the subject array contains the two nouns "I" and "You", the verb array contains "like" and "hate", and the direct object array contains "broccoli" and "ice cream", then the get() method could return eight different sentences.
I like broccoli.
I like ice cream.
I hate broccoli.
I hate ice cream.
You like broccoli.
You like ice cream.
You hate broccoli.
You hate ice cream.
The sample solution uses the following hardwired arrays which can generate 616 different sentences (not all of which make any sense e.g. "Cats run kiwi." - but that is ok for this assignment).
   private String[] subjects = { "I","you","dogs","cats","people","students","men","women"};
    private String[] verbs = { "like","hate","eat","run","study","want","drive"};
    private String[] directObjects =
    {
        "people","dogs","you","me","them","students","men","women","kiwi","a volvo","a junker"
    };
The contents of the various arrays can be read from a file or hardwired into the program as shown above.

Your program should generate both statements (as above) and questions. Generating questions is just like generating sentences except that in the sample solution, in addition to subject, verb, and direct object, the question begins with some interrogative. The sample solution uses

   private String[] questions = {
        "do","where do","what do","why do", "when do"
    };
which when combined with the sentence arrays above yields 3080 different questions such as "Do students eat kiwi?" and "Why do men want a volvo?".

Going online

During testing of the SaySomething thread, just set the MAX_SLEEP to be something small, like 1000. This will generate a new random sentence/question every second or less. You might also want to have the MessageGenerator code print "no match" or something similar during testing so you can be sure that it in fact processed the other chatter's message.

You are provided a simple ChatBot program that responds to only a single pattern and at random intervals prints "Say something." The complete program consists of four classes, ChatBotV5, Message, MessageGenerator, and SaySomething. All four of these can be found in /afs/cats/courses/cmps012a-cm/prog5. The program will actually work as is, although it is missing most of the required features.

To integrate your code with the starter, you have three primary tasks. You need to modify the method readPatterns() in MessageGenerator, to use your code that reads the patterns from a file. You also need to modify the method respondTo(), also in class MessageGenerator, to use your code that responds to messages from standard input. The primary difference is that the "sentences" you respond to are now passed as a parameter to respondTo() - ie. msg.content - and instead of printing your response using System.out.println(), you will send your response to the chat room using the method send() provided in class MessageGenerator. Finally, you need to modify the run() method in SaySomething to produce random sentences instead of always sending "Say something."

You should not need to make any changes to the classes ChatBotV5 and Message, or to the other methods in MessageGenerator, although you may want ta add some additional helper methods in MessageGenerator and SaySomething.

Running the ChatBot

To run and test your program you will need to run at least two other programs, a chat server and another chat client that lets you send messages to the chat room. The chat server and a simple chat client can be found in /afs/cats/courses/cmps012a-cm/prog5, they are chatServer.jar and chatClient.jar. You start the server by typing:
java -jar chatServer.jar portNumber
where portNumber is the port to use. It should be some number between 2000 and 10000.

To run chat client type:

java -jar chatClient.jar hostName portNumber screenName
The hostName is the IP address of the server. If testing on your local machine you can use "localhost". The portNumber is the same number you used for the server. The screenName is whatever name you want to use in the chat room. You can of course start up as many chat clients at once as you wish.

Once you have the chat server and at least one client connected, you start up your version of chatBot using:

java ChatBotV5 hostName portNumber screenName patternFile
The patternFile of course is the name of the file (in the current working directory) of the file containing the patterns and responses.

If for some reason you are working using an ssh connection and cannot run the chatClient.jar chat client (which has a graphical user interface), there is also a simple text only chat client in the same directory as the others. It is called textChatClient.jar. You run it just like chatClient.jar but it has a simple text interface instead of a graphical interface.

Sample Solution

There is a sample solution in /afs/cats/courses/cmps012a-cm/prog5/hw5Solution.jar. You run it just like you would your program (first start a server and a regular client) then type:
java -jar hw5Solution.jar hostName portNumber screenName patternFile
Make sure there is a patternFile in the current directory. You are welcome to start with the one from /afs/cats/courses/cmps012a-cm/prog5/patterns.txt.

Correctness Points

20 points - compiles without errors and is a serious attempt at a solution

10 points - correctly reads a pattern file and generates responses based on those patterns

5 points - responds to multiple patterns (could be hardwired or read from a file)

5 points - avoids making the same response twice in a row

10 points - generates random sentences and questions at least as complex as the sample solution