Renaissance translator. Huzzah!

Table of Contents

Update on 4/10, 7am: I've tweaked the wording to disambiguate the word "translate".

This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

An important and common use for computer science ideas is for translating or converting text from one form, or one language, to another. Tools like Google Translate will convert text from one language to another. For this assignment, you'll create a computer translator that will take text and convert it to Renaissance speech, a form of speech used at Renaissance fairs (or perhaps I should say Renaissance Faires) that may or may not be based in historical reality.

2 Specifics

Your mission is to create a program to convert English to Renaissance. Create a directory called renaissance for your code, and name your program renaissance.py. Specifically, here are some rules to follow.

2.1 Translation

Replace words as follows:

  • hello -> well met
  • goodbye -> fare the well
  • please -> prithee
  • restroom -> privy
  • here -> hither
  • maybe -> perchance

Feel free to add more if you like, but do that later as that's easy once you've got the above working. You might want to get this working with just a few words, do the rest of the assignment, then come back to finish this for fun.

(Clarification added 4/10, 7am): Bear in mind that the above list contains the words that are changed when we translate from English to Renaissance. When I use the word translate, it applies to all words, even those that aren't changed. For example, the phrase "Hello friend" translates to "Well met friend". The words "Hello" and "friend" are both translated from English to Renaissance. Only the word "Hello" is changed as part of the translation.

2.2 Capitalization

If a word begins with a capital letter, then when you translate it, it should also begin with a capital letter. Ignore the case of the rest of the word. In other words, translate "Hello" / "HELLO" / "HeLlO" / "HeLLO" / etc. as "Well met", but translate "hello" / "hELLO" / "hElLo" / "hEllo" / etc. as "well met". You should only output "Well met" or "well met" as a translation for any form of "hello" – none of the other letters in "Well met" or "well met" should ever be capitalized apart from possibly the first. (You should definitively not do this by testing individually for all possible ways the word "HeLlO" might appear. You'll have over 200 cases for "restroom" if you do it that way.)

(Clarification added 4/10, 7am): Similarly, the word "FrienD" should translate as "Friend".

2.3 Huzzah!

Renaissance people are very enthusiastic, and have a habit of saying "Huzzah!" a lot. Get your translator to randomly insert "Huzzah!" between sentences. Anytime you encounter the end of a sentence (as denoted by a period, question mark, or exclamation point), decide with a 50/50 chance whether to insert a "Huzzah!"

3 Breaking up the words

Separating the words from the punctuation is kind of tricky in Python. Python makes it easy when things you want to read are separated by spaces, not punctuation. Therefore, your input should have spaces between anything you want Python to read in separately, such as:

Hello ! Please , could you maybe tell me where the restroom is ?

Your program should allow you to type in your input, and print out to the terminal window the translated version of your text. Just to get you started, the below sample code reads a phrase that you enter and then prints out space-delimited words one-by-one, so that in principle you could do something with each word if you wished. Feel free to take a look and use this if it is helpful, but remember to type in yourself any code that you use (copying and pasting with the mouse tends to make you skip actually learning it, whereas retyping it seems to get it to sink in better.)

phrase = input()

for word in phrase.split():
    print(word, end = ' ')

print()

4 Redirecting the input from a file

While testing your code, you will likely find that it gets cumbersome to keep retyping your input over and over. It will be much faster if you put your input in a file, and redirect the input to your program to come from that file. In other words, create a file called source.txt (for example) that contains your input text, such as the example in the previous section. Then when you run your program, redirect your input to come from that file. Specifically, at a UNIX prompt, run your program like this:

$ python3 renaissance.py < source.txt

Think of the < as an arrow, directing the text inside source.txt to be the input to your program.

5 Optional bonus: dealing with the punctuation better

This section is completely optional. If you want to, feel free to try to get your program working without needing to put spaces between words and punctuation. The below sample program is much like the input sample provided above, but separates words whenever the text transitions from a "word character" (i.e., a letter) to a "non-word character", or vice-versa. You're not responsible for knowing how the weird delimiter stuff works in this program, though I'm happy to explain it if anyone asks.

from re import *
from sys import *

phrase = input()

regex = compile('\w+|\W+')
for word in regex.findall(phrase):
    print(word, end="")

print()

6 Test your code

As in the last assignment, you should test your code. Do some more examples on your own, work out by hand what the results should be, and see if your program gets them right. We will test your program similarly.

Make sure to also to think through all the possible ways in which your program might go wrong, and test them.

7 Style

Make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a comment at the top explaining what the program does, and use comments above consecutive portions of Python code explaining what they do. You should also use meaningful and readable variable names.

8 The last point

If you complete the above successfully (and have good style in your code), you will receive nearly all the points for the assignment. If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the last point, do the following extra challenge.

  • Get your program to be able to successfully change the English phrase "thank you" to the Renaissance phrase "grammercy". It should handle capitalization reasonably as for the rest of the assignment. However, if the word "thank" appears on its own without the word "you" immediately following it, the word "thank" should remain unchanged. We will not test the particular complicated case of "thank thank you", so your program can handle that situation however you like and still get the point if the above is working. (Language tweaked 4/10, 7am: using "change" instead of "translate")

9 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate