|
| |
Pig Latin
In this assignment, you'll be writing a translator from
English to Pig Latin.
Note: The point of
this exercise is to learn how to implement a subclass of class Reader, and
to use that subclass in a couple of typical applications . It
is certainly possible to implement this not using a subclass of Reader,
but that will make things harder to do when you get to use the resulting
code in the applications.
Also, as with all such assignments, I expect you to implement a
general solution, not one that is merely focused on the applications that
are used in this assignment. I expect you to write a solution that
could be used by your "customers" in a general way;
that is, you or your "customer" should be able to simply use the
resulting class without any need to change it for a particular
application. In the real world, this is what you'll be expected to
do, so it's a good idea to get used to it right now!
Introduction
Pig Latin is a word game usually played by children. They
love to become proficient at it, because a lot of adults can't
understand what they're saying when they converse in Pig
Latin. If you have any children, you'll understand the
motivation.
There are a number of dialects of Pig Latin, but here's
one with some simple rules:
- Take any passage of English
- Break the passage up into individual words and
punctuation
- Each word is transformed as follows:
- If the word begins with a vowel ('a', 'e',
'i', 'o', or 'u') append "yay" to
the word.
For example: "am" becomes
"amyay", 'I' becomes
"Iyay", "aardvaark"
becomes "aardvaarkyay", etc.
- If the word begins with a consonant (any
letter that is not a vowel), find the first
vowel in the word, divide the word just
before that vowel, swap the two parts of the
word, and append "ay" to the
resulting word. If the original word had its
first letter capitalized, make sure that that
letter is not capitalized after the word is
divided, and ensure that the first letter of
the resulting word is capitalized.
For example, "string" becomes
"ingstray", "Latin"
becomes "Atinlay" (note the capitalization),
"student" becomes
"udentstay", "professor"
becomes "ofessorpray", etc.
- If the word has no vowels (we count 'y' as a
consonant, here), append "yay" to
it.
For example, "my" becomes
"myyay", "pry" becomes
"pryyay".
- Punctuation is not changed by the translation
process.
In particular, treat apostrophe (') and double-quote (")
characters as word separators, not as part of a word. The
apostrophe is used in contractions, such as "can't", as well
as in other uses, such as "his 'notorious' aunt", so it's
quite difficult to distinguish between these usages. Since this
assignment is focused on how to write a subclass of Reader, we'll take
the easy way out, by ignoring the problem..
- Put the translated words and punctuation back into
the same order as their corresponding original words
and punctuation.
- The whitespace (space characters, tab characters,
newlines, etc.) between words should be maintained.
- If the first letter of an untranslated word is capitalized, then the
first letter of the translated word should also be capitalized.
If the original first letter is moved during the word's translation so
that it is no longer the first letter of the word, then it should be
lowercased.
Note: Since you're going to
implement this as subclass of Reader, think in terms of treating the input
as a stream that potentially has no end. Do not simply read
until you're read everything in the stream, and only then return some translated output. If you did
that on a very long (or infinite stream), you'd soon run out of memory!
Instead, translate a word at a time, which involves a relatively small
amount of read-ahead on the input stream. This illustrates the
whole concept of a stream -- it is something that allows processing
"as the stream flows by", rather than blocking the stream (which
we all know can cause flooding).
For example, here is an applet that can transform a web
site into the Pig Latin equivalent. Just type in your
favorite web site address and click on the "Go!"
button. Note that not all web sites are amenable to such
translation; ones that are include:
- http://www.rivier.edu/
- http://www.rivier.edu/faculty/bhiggs/web/cs690aweb/
- http://www.netscape.com/
- Try your own favorite!
Below is a Java applet that uses the above algorithm to
translate from English to Pig Latin. Enter your English text
in the upper (white) text area, and then click on the
Translate button, and you'll see the Pig Latin equivalent in
the lower (grey) text area.
What To Do
Write a Reader Class to perform the English to Pig
Latin Translation
(I called my class EnglishToPigLatinReader.)
The class should:
- Be placed in package
pigLatin
- Extend from class
PushbackReader
- Have a constructor of the form:
public EnglishToPigLatinReader(Reader in)
so that the class may be used as a
conventional Reader class, with the usual nesting capabilities:
new BufferedReader(
new EnglishToPigLatinReader(
new FileReader(input) )
- Override the Reader
methods:
public int read()
throws IOException |
public int read(char[] cbuf, int off, int len)
throws IOException |
- Add any private
methods necessary, based on your particular implementation.
- Have a main() method
that tests the class' capabilities.
Some hints, based on my implementation experiences:
- I tried using
StreamTokenizer,
but found that it was overkill. Also it did not allow
me to maintain the whitespace characters
- I thought about using
StringTokenizer,
but also felt that it was overkill.
- I
found it useful to allow characters to occasionally
be pushed back onto the input, which is why I extended from
PushbackReader.
- I also found it useful to encapsulate the translation
of a single English word to Pig Latin inside a private method (which I
called
latinize()).
For what it's worth, my implementation of this class
was 263 lines long, including comments and (lots of) blank lines.
Write an Application that, given a file containing English text,
outputs the translated text to standard output.
The application must use the EnglishToPigLatinReader
class you implemented above.
Note: If you implemented the EnglishToPigLatinReader
class correctly, you should not have to change the EnglishToPigLatinReader
class in the slightest way. If you find you have to modify
the EnglishToPigLatinReader class in any way, go back to
part A, and
implement it properly, then try again.
Write an Application to Emulate the above Translation
Applet
The application must use the EnglishToPigLatinReader
class you implemented above. If you implemented the EnglishToPigLatinReader
class correctly, you should not have to change the EnglishToPigLatinReader
class in the slightest way.
Note: My applet is a mere 87 lines long,
including comments and empty lines, so it's not a huge
effort.
|