Simulating a conversation

The aim of this assignment is to test the use of virtual functions, and to test ideas in debugging existing code.

Problem description

Write a program to simulate the verbal responses of a group of speakers having a conversation. Each type of speaker has different characteristics according to his or her personality. Their vocabulary is finite and small and the sentences they speak are limited to simple subject, object verb combinations, either in statement form or question form. Their behavior is to be simulated by the use of probabilities. Each speaker has an independent probability of producing a thought in the form of a sentence, and then each speaker has an equal chance of actually making an utterance in the conversation. These utterances are remembered by the speakers according to their type.

The speaker types

There are three characteristics of a speaker:

  1. how they generate ideas
  2. how they produce a utterance
  3. how they remember utterances

There are four types of speaker: Babblers, Thinkers, Egotists and Fillers. Their characteristics are summarized in the table below:
Babbler Thinker Egotist Filler
Sentences remembered 0 10 4 2
How ideas are produced Choose subject, object and verb at random Recycle the oldest sentence remembered Choose two remembered sentences and form a new one by taking the object from one and verb from the other; subject will be "I" Take two most recent sentences, take subject and object from the older one, and the verb from the newer.
Probability of idea 0.8 0.1 0.4 0.4
Utterance Always a statment Always a question Always a statement (with subject "I") Always a statement; the probability should increase when no-one speaks

Sentence construction

Use the following class to represent sentences, and to produce utterances (this will be mailed to you):

// a class to handle the sentences produced by the speakers
class Sentence {
private:
   string sub, obj, verb;
   string past(string v);      // convert verb to past tense
   string object(string n);   // handle I->me or add "the"
   string subject(string o);   // handle I or add "the"
   const static string nouns[];      // the nouns that the speakers use
   const static string verbs[];      // the verbs that the speakers use
   const static int nNouns, nVerbs;   // the number of each
public:
      Sentence(string s, string o, string v) :
         sub(s), obj(o), verb(v) {}
   string state() {
      // make a statement
      return subject(sub) + past(verb) + object(obj) + ".";
   }
   string query() {
      // make a query
      return string("did") + subject(obj) + verb + object(sub) + "?";
   }
   string getSub() { return sub; }      // accessor
   string getObj() { return obj; }      // accessor
   string getVerb() { return verb; }   // accessor
   static string randNoun() {
	// generate a noun
      return nouns[Rand(nNouns - 1)];
   }
   static string randVerb() {
   	// generate a verb
   	return verbs[Rand(nVerbs - 1)];
   }
};

string Sentence::past(string v) {
   // convert a verb to past tense
   if (v == ...) return string(...);
   else if (v == ...) return string(...);
   ...
   else return v;
}

string Sentence::object(string n) {
   // handle I and adding the
   return (n == "I") ? string("me") : string("the") + n;
}

string Sentence::subject(string o) {
   // handle I and adding the
   return (o == "I") ? o : string("the") + o;
}

Rand is a function that returns a random integer between 0 and its single argument. It can be written as follows:

#include <ACG.h>
#include <RndInt.h>
#include <unistd.h>
static int Rand(int max) {
static Random r(0, 4, new ACG(getpid()));
return r;
}
You will need to define the static constants nouns, verbs, nNouns and nVerbs, and fill in the details in the function past, which converts the present form of a verb into its past tense. An example of the last is "talk" ==> "talked". Notice that the class uses the standard C++ class string. The operations form the class string you will need are:

Use at least ten nouns and five verbs in your testing.

Debugging

The code for the class Sentence is basically correct, but produces unacceptable results. You will need to adjust the code in a few crucial aspects to make it work better. Your test suite should include special consideration of this class.

Representing the speakers

You should do an object design that includes the above Sentence class, and an application class to manage the conversation. Then design an abstract class to contain elements and pure virtual functions common to the four types of speaker, and then derive four classes from it to specialize the speaker class. Each derived class will have its own version of the virtual functions declared in the speaker class. You will need, at least, a function for producing an idea, one for producing an utterance, and one for remembering the current utterance.

The conversation

Use a "round robin" scheduler for simulating the conversation. The basic algorithm is:

  1. Give each speaker in the group a chance to produce an idea sentence. This is governed by the speaker's internal probability, and so there is a possibility of producing no idea.
  2. From those speakers who produce an idea, choose one speaker at random and print an utterance. If no-one produces an idea, the utterance may be empty.
  3. Give each speaker a chance to remember the utterance, including the speaker. Everyone except the babbler will remember it.
  4. Repeat this loop for a fixed number of cycles.

What to submit

  1. Your object design and function member description.
  2. Your source code, with comments.
  3. A brief description of the problems with the class Sentence, and how you fixed them.
  4. A sample output of a conversation produced by the program. It should contain 25 utterances tagged by the speaker's type. Indicate cycles where nobody speaks. Make sure your conversation includes utterances by all four speakers.

A sample conversation is printed below:

Babbler: the man looked at the cat.
(no-one spoke)
(no-one spoke)
(no-one spoke)
Babbler: the man drove the woman.
(no-one spoke)
(no-one spoke)
(no-one spoke)
(no-one spoke)
Egotist: I looked at the cat.
(no-one spoke)
(no-one spoke)
Egotist: I drove the woman.
(no-one spoke)
Babbler: the cat liked the woman.
Egotist: I drove the woman.
Babbler: the dog broke the man.
(no-one spoke)
(no-one spoke)
(no-one spoke)
Thinker: did the cat look at the man?
Filler: the dog looked at the man.
(no-one spoke)
Babbler: the man looked at the dog.

Due date

May 14th. before 5pm.