Substitutions

Here is a table of the type of substitutions to be used in the doctor program:

old word

new word

I

you

me

you

my

your

mine

yours

am

are

you

I

are

am

Note that the first five substitutions are always valid, but the last two can sometimes be invalid. The word "you" could translate to either "I" or "me"; here we "guess" it should be "I", but we could be wrong. And "are" could be a verb for the third person plural, as in "Clouds are pretty", in which case we really don't want to change it to "am". We would need a more sophisticated approach to handle these cases correctly.

The doctor program contains a parseWords() method that correctly parses a string into a list of strings that are its component words and punctuation marks. For example, if sentence is the string

"You know, I really miss him!"

then parseWords(sentence) yields an ObjectList of strings with the following printed representation:

[You, know, ,, I, really, miss, him, !]

(Here ",," should be interpreted as the punctuation string "," followed by a comma delimiting the components of the ObjectList. ) The "inverse" of parseWords() is unparseWords(), which glues a list of words and punctuation marks back into a sentence that is a single string.

Using these methods, we can define a changePerson() method that applies the above person-changing substitutions to a sentence:

	public String changePerson (String sentence) {
		return unparseWords(substitute(substitutions, parseWords(sentence)));
	}

Here, substitutions is a class variable of Doctor containing a list of substitution pairs that alternates between old words and the corresponding new words:

[I, you, me, you, my, your, mine, yours, am, are, you, I, are, am]

To make changePerson() work, you need to define the following two methods:

public static String subst1 (ObjectList substs, String word)
Assume substs is a list of strings with alternating "old"/"new" word pairs. If word is any "old" word in substs, returns the corresponding"new" word in the pair. Otherwise, returns the given word unchanged.

public static ObjectList substitute (ObjectList substs, ObjectList words)
Assume substs is a list of strings with alternating "old"/"new" word pairs, and words is a list of strings. Returns the list of strings that results from mapping the subst1 substitution process over every word in words.

Examples

Suppose we have the following test lists:

L0 = [ ]
L1 = [You, are, not, LISTENING, to, me, !]
L2 = [I, am, what, I, am, ;, you, are, what, you, are]
L3 = [I, am, saying, that, my, goal, is, to, get, what, is, mine, .]
substitutions = [I, you, me, you, my, your, mine, yours, am, are, you, I, are, am]

Then here are some test cases of these two methods:

subst1(substitutions, "I") = you
subst1(substitutions, "me") = you
subst1(substitutions, "my") = your
subst1(substitutions, "mine") = yours
subst1(substitutions, "am") = are
subst1(substitutions, "you") = I
subst1(substitutions, "are") = am
subst1(substitutions, "cat") = cat
 
substitute(substitutions, L0) = [ ]
substitute(substitutions, L1) = [You, am, not, LISTENING, to, you, !]
substitute(substitutions, L2) = [you, are, what, you, are, ;, I, am, what, I, am]
substitute(substitutions, L3) = [you, are, saying, that, your, goal, is, to, get, what, is, yours, .]