CS 161 Homework 5 - Dialect Converter
Due Fri Oct 18 at 5:00pm
Overview
In this assignment you will create a program to convert text files into into amusing dialects--in particular, into Pig Latin and Swedish Chef-ese (also here). Your program will be run on the terminal. It will first prompt the user to select a dialect, and then to input some text. This text can include multiple words--indeed, it could be an entire paragraph! Your program will convert the text into the previously choosen dialect, and then output the results. Your program will continue asking the user for text to convert until the user hits "enter" (gives a blank entry), at which point the program will exit. Examples of what your program might look like can be found below:
Welcome to the dialect converter! Choose a dialect: 'Pig Latin', 'Swedish Chef' Dialect: Pig Latin Phrase: Hello World "elloHay orldWay" Phrase: all your base are belong to us "allway ouryay asebay areway elongbay otay usway" Phrase: the quick brown fox jumped over the lazy dog "hetay uickqay rownbay oxfay umpedjay overway hetay azylay ogday" Phrase: I love computer science!! "Iway ovelay omputercay ciencesay!!" Phrase: Goodbye!
Or from an alternate run, selecting the Swedish Chef dialect:
Welcome to the dialect converter! Choose a dialect: 'Pig Latin', 'Swedish Chef' Dialect: Swedish Chef Phrase: Hello World "Helloo Woorld" Phrase: all your base are belong to us "ell yoouor bese-a ere-a beloong too us" Phrase: the quick brown fox jumped over the lazy dog "zee quoeeck broon foox juomped ofer zee lezy doog" Phrase: I love computer science!! "I loofe-a coompuoter sceeence-a!!" Phrase: Goodbye!
Note that these are fine "test" cases for your program!
This assignment should be completed individually.
Objectives
- To practice with conditionals and loops
- To continue working with Strings and Characters, manipulating and parsing
- To practice writing and working with helper methods
Necessary Files
You will need a copy of the
Hwk5.zip file.
The BlueJ project in this file includes the beginnings of a provided Dialectizer
class. You will be filling in and completing this class (as well as adding comments to existing code!) in order to complete your program.The project also contains the README.txt that you will need to fill out and turn in along with your assignment.
Remember to extract the files from the zip folder before proceeding!
Dialects
-
Your program should be able to convert normal text into "Pig Latin". For our purposes, Pig Latin is created by using the following rules:
- If a word starts with a consonant, move the consonant to the end of the word and add the ending "ay".
- If a word starts with a vowel, just add the ending "way" to the word.
You will need to apply these rule to every word in your test. For example "This is the number 10." shoud be converted to "histay isway umbernay 10."
-
Your program should also be able to convert normal text into that mirroring the patter of the Swedish Chef (from the Muppets). You can do this by using the following rules (there are a lot!):
- Replace every copy of the word "the" with "zee"
- Replace every "an" with "un"
- Replace every "au" with "oo"
- Replace every "a" in a longer word with "e". So the word "a" remains "a", but other "a"s change.
- Replace every "en" at the end of a word with "ee"
- Replace every "ew" at the end of a word with "oo"
- Replace every "e" at the beginning of a word with with "i"
- Replace every "e" at the end of a word with "e-a"
- Replace every "f" with "ff", unless at the start of a word
- Replace every "ir" with "ur", unless at the start of a word
- Replace the first "i" that is not at the start of a word an "ee" (whew!)
- Replace every "ow" with "oo", unless at the start of a word
- Replace every "o" with "oo", unless at the start of a word
- Replace every "th" at the end of a word with "t"
- Replace every "u" with "uo", unless at the start of a word
- Replace every "v" with "f"
- Replace every "w" with "v"
Rules adapted from http://www.cs.utexas.edu/~jbc/home/chef.html
Note that the output of the rules should not trigger another rule--that is, don't replace a "w" with a "v" (rule 17), and then replace that "v" with an "f" (rule 16). Applying rules in order will produce the best results.
Helpful Advice: Iterative Development
You should work to complete this assignment iteratively---you should build a small piece, test that it works, and then move on to the next bit. Below is a suggested order of steps, as well as good deadlines for getting each step working (if you haven't figure something out by the listed date, definitely ask for help!!):
-
Dialectizer
initialization: Tue 10/08 -
pigLatinWord()
method: Wed 10/09 -
swedishChefWord()
method: Thu 10/17 -
DialectConversion
class: Fri 10/18
Assignment Details
For this assignment you will modifying one existing class and creating one new class. The first class (Dialectizer
) will be your "object class" (with instance variables, a constructor, etc)--it will represent a "machine" that can take in Strings and output versions converted into a dialect. The beginnings of this clas, complete with some helpful methods, has been provided for you. The second class (DialectConversion
) you will create your self. This will be your "main class" that includes a user interface for soliciting text from the user and displayed the translated version of that text.
Overall, you should start by creating the Pig Latin functionality, then add the Swedish Chef functionality, then create the User Interface for using these functions.
Dialectizer
Your very first order of business should be filling in the Dialectizer
class and making sure it can compile and be used. You'll need to make the following changes:
- Add a constructor that takes in a single variable representing which dialect the generator should use. In effect, this variable will let you "decide" which dialect to use when the user calls the provided
convertText()
method---see that method for details. That dialect should be stored in an instance variable (one option has been provided for you). - You should also include a getter and setter for this attribute. This will let the user change the dialect later if they wish.
- Add in method signatures for the
pigLatinWord
andswedishChefWord
methods. Details about these methods are listed below. Note that your method signatures should allow the methods to work with the provided code. You can always return a temporary value (e.g., 0,""
, or null) as a place holder to make sure the class compiles. -
You will need to add complete comments to the provided
getNextWord()
andconvertText()
methods. These comments should include a details method comment (explaining both the parameter and return values), as well as in-line comments detailing what each line or block does. As always, your comments shoudl explain the why of the code, not the what---what is the purpose of this code?- Goal: Your comments should demonstrate to me that you understand what is going on in these methods. Prove that you can follow the flow of the code!
- Note: You can work on other aspects of the assignment before commenting this code (for example, if we haven't gone over loops yet).
This should get you set up to be able to test your code (constructing a Dialectizer and calling convertText()).
A note about capitalization: The current methods do not handle capitalization--as you can see from the example outputs, the capitalization may not remain constant. That is acceptable for this assignment. However, you are welcome to add in functionality to correctly handle capitalization. One simple solution might be to make all the text either capital or lowercase, such as by using the String.toUpperCase() or String.toLowerCase() methods.
Pig Latin
The next thing you should do is add the ability to convert text to Pig Latin to your Dialectizer
class.
-
In order to make a word into Pig Latin, you'll first need to figure out if that word is a vowel. Since you'll be doing this a lot, it might be helpful to have a helper method to perform this functionality. Create a method called
isVowel
that takes in a singlechar
and returns whether or not that character is a vowel.-
The easiest way to do this is to define a String that is a list of the vowels, and then use the
String.indexOf()
method to check whether the given char is inside the String. If it is not, the
.indexOf()
method will return -1.
-
The easiest way to do this is to define a String that is a list of the vowels, and then use the
String.indexOf()
method to check whether the given char is inside the String. If it is not, the
-
Because Pig Latin rules are applied at the "word" level, you'll be completing a helper method named
pigLatinWord
that will make a single word into Pig Latin. This method should take in a single word as a parameter, and return a Pig Latin version of that word.- Start by writing down the algorithm---the series of steps---you might need to go about this method. This is a good stage to check in with others; make sure you understand how the process will work, even beore you write the code!
- You can use the built-in String.charAt() method to figure out what the "first" character of the word is.
- You can use the
substring()
method to get the word without the first letter, and use String concatenation (with the+
operator) to add new endings to the word. - Remember to return the word when you are done!
-
Note that this method will not work if your text has punctuation (like periods, etc). That's okay--we're handing punctuation in the
getNextWord()
method. Do you see how? Nevertheless, this is good to keep in mind for testing. - Be sure and text your method thoroughly to make sure it works! You can either create a tester, or simply test it through the BlueJ UI.
Swedish Chef
Once you have written your Pig Latin converter, you'll need to write your Swedish Cheferizer. Again, rules for converting text to chefese will be applied at the word level, so you will need to create a method named swedishChefWord
that will convert a single word. This method should take in a single word as a parameter, and return a Swedish Chef version of that word.
-
Start by thinking about the algorithm you might follow to be able to solve this problem. How can you handle checking every letter to see if it should be replaced? How can you make sure you're not applying the same rule twice?
- This method is more complicated than the Pig Latin method (after all, there are more rules!), but the overall structure is fairly straightforward. If you aren't sure (or just want to confirm you're on the right track), please ask! :)
- I recommend handling the "special" words first: for example, one letter words ("a" and "I") do not get changed. The word "the" gets changed to "zee" (rule 1).
-
Rather than replacing letters, you might think about constructing a brand new String to return. In effect, you'll be rebuilding the String, but adding certain letters in place of others. So if you want to replace "a" with "e", we can just add an "e" where you would have added an "a".
- Don't forget to include letters that haven't been replaced in your final String!
-
Remember, you can compare characters (e.g.,
'a'
) using==
, but you need to compare Strings (e.g.,"a"
) using the.equals()
method! - For the rules that depend on being "at the start of a word", you can simple check if the index of the letter you are replacing is 0--if so, that letter is the first one of the word! Similarly, you can check if a letter is "at the end of a word" by checking if the index of the letter is the last index of the word (hint: it involves the .length() of the word!)
-
For rules that involve two letters (i.e., #2, 3, 5, 6, 10, 12, 14), you'll want to think about the first letter of the pair to see if a rule may apply to that. If so, you can check if the next letter (the letter at index+1) matches the criteria--if so, you can replace the both the characters. If you do this, remember to increase your index and skip the next letter! So if you've seen "ew" and decided to replace it with "oo", you'll want to skip that second letter (the "w").
- Be careful about finding the "next" letter if the first letter is at the end of the word! So don't check that an "e" is followed by a "w" if that "e" is the last letter of the word.
- This method will involve a large, complicated if/else block. That's expected--the challenge is getting the right set of conditionals and behaviors to handle all the rules.
- Remember to return the converted word when you are done!
- Be sure and text your method thoroughly to make sure it works! You can either create a tester, or simply test it through the BlueJ UI.
User Interface
Finally, once you've got your Dialectizer
working, create a DialectConversion
class to provide a user interface for using your generator. This class will be a "main" class--it will have a main method that prompts the user for input and provides output.
- You should get user input via the
Scanner
. - Your class should start by prompting the user for a dialect, then use this input to construct a new
Dialectizer
object for the appropriate dialect. - You should then ask the user to input a phrase, convert that phrase into a dialect, and output the new version.
- After you've asked the user, you should loop around and ask them again! Keep asking for words until the user enters a blank line (a line equal to
""
)---that's how you know you're done. - This is not a particularly complicated method, but does require understanding loops and the Scanner!
NOTE:
You are welcome to write this class first before you finish the Dialectizer
, and then to use it for testing.
Extensions
There are a number of potential extensions you might consider for this assignment. You can earn up to 5 points of extra credit for extensions on this assignment. Be sure to finish the rest of your program first!
- Modify the user interface so that instead of exiting when the user enters a blank screen, it instead prompts the user to enter a different dialect. Thus the user can translate to one dialect, then switch and translate to another. If the user enters a blank line when prompted for a dialect, the program should exit.
-
Add the following rule to your Swedish Chef dialect generation:
- At the end of every sentence (after a . ? or !) add "Bork Bork Bork!"
Note that this will require you to modify the
convertText()
method (since this doesn't happen at the word level anymore). Be sure to carefully document your changes! -
Add a third dialect of your own design to the
Dialectizer
this will likely involve another <dialect>Word() method, but could also potentially function at the sentence level. The Wikipedia article on language games includes a long list of options (I think spoonerisms are particularly fun).
Submitting Your Assignment
- Be sure and test your program thoroughly to make sure it works, and make sure it is documented with lots and lots of informative comments!
- Remember to fill out the README.txt file. You can open it in BlueJ by double-clicking on the icon.
- Upload the entire project directory (including both Java files, as well as the README.txt) to the Hwk5 submission folder on the hedwig server. Make sure you upload your work to the correct folder!.
- This assignment is due at 5:00pm on Friday, Oct 18. Note that this is the start of break, so you'll want to have it done early!
Grading
This assignment will be graded on approximately the following criteria:
- You have implemented a working
Dialectizer
class [5%] - You have included detailed and informative comments on the
convertText()
andgetNextWord()
methods [20%] - You have implemented a working
pigLatinWord()
method [15%] - You have implemented a working
swedishChefWord()
method [30%] - You have implemented a
DialectConversion
main class [15%] - Your code is properly formatted (indentation, good variable names, etc) and is detailed with comments. Each method should have a comment explaining what it does! Note that if I've left comments about style in previous assignments, you should take those to heart! [10%]
- You have completed the included README.txt file [5%]