Programming Assignment 08
"Oneyay allsmay epstay orfay anmay."
Objective:
Get more practice with Strings, looping, and files
Due:
Wednesday, April 23rd, 10:00 A.M.
An introduction
In this PA you will be writing the code for a project which will help us
convert words/sentences from English to traditional "Pig Latin."
Pig Latin?
Some of you may have played with Pig Latin as a kid. There are many
slightly different rule sets for Pig
Latin. The version we will use is:
- RULE 1 - If the word begins with a consonant - such as "hello" or "this" - divide
the word at the first vowel, swapping the front and back parts of the word and
append "ay" to the word. The previous 2 examples would produce "ellohay" and "isthay".
- RULE 2 - If the word begins with a vowel - such as "am", "are" or "i" - append "yay"
to the word. The previous examples would produce "amyay", "areyay" and "iyay".
- RULE 3 - If the word has no vowels - such as "my" or "thy" - append "yayy" to it.
The examples would produce "myyayy" and "thyyayy".
The Final Project
Eventually I want you to have code that will take an entire text file and
convert it to Pig Latin. For example, the code that I wrote to test this
out myself took the following file
and translated it to this file
[NOTE : for ease of use I have elected to not use any newline characters in
these files. This is probably not realistic in the long run, but it will
help you do this assignment for now]
However, if you simply start with that idea than you are likely to get
confused and run into problems. Instead, let's think about this problem
from the top down. Our full blown project should:
- Open/read a text file
- Break the file into one sentence from the file at a time
- Break each line/sentence into individual words
- For each word, convert it into PigLatin...
- by finding the first vowel (if it exists)
- And finally, writing these converted words back out to a second text file.
As I look at this project, we want to do this in several manageable steps
Part A - (Step 5) Finding the first vowel
Begin by writing a method called findFirstVowel().
- This method should take a String as a parameter.
- It is assumed that this
String is one word (although it turns out that even if this isn't true, the
method will "work.")
- The method should return an integer corresponding to
the index location of the first vowel ('a', 'e', 'i', 'o', 'u'), or a negative
1 when there is no vowel.
For example,
-
findFirstVowel("schafer") should return 3 since that is
the location of the first vowel - 'a'
-
findFirstVowel("able") should return 0 since that is the
location of the first vowel - 'a'
- findFirstVowel("my") should return –1 since there
is no vowel.
- Make sure that this method works regardless of the case of the letters
submitted
Part B - (Step 4) Translating one word
Next, write a method called translateWord()
- This should take a
single String as a parameter, assumed to be a word in English.
- The method
should use the helper method created above.
- It uses the result
of findFirstVowel() to see which of the three rules apply. It uses this
information to modify the word appropriately, and
return the String representing the translated word.
Part C - (Step 3) Translating one sentence
Next write a method called translateSentence().
- This method takes a String as a parameter.
- This String is assumed to be an English sentence
- You may assume there are no punctuation marks IN it (no commas, no
hyphens, etc.).
- You should assume that there is a period at the end of the sentence
- The method should return a String representing the translation of the
parameter, including a proper reintroduction of the period at the end and
capitalization of the first letter.
- For example, if the parameter was "One small step for man." the return
value should be similar to the title of this PA.
Part D - (Steps 1 and 2) Reading from a file
Next, write a method called translateFile()
- This method takes the filename of a text file as a parameter (like one
returned from pickAFile() )
- The method opens and reads this file...
- ... divides the file's contents into separate sentences
- ... send each sentence to translateSentence()
For testing purposes at this point you can simply print the results to the
screen
Part E - (Step 6) Writing to a file
Modify the translateFile() method
- This method now takes two filenames as parameters - the source file and
the output file, in that order.
- In addition to what was done above, the method should now write the
results to a file
Final Submission
As is normal, please prepare all of this in a file called pa08.py and located
in a directory called pa08 in your root directory on the p: drive.
Prepare a paper printout of your code to submit at the start of class.