Note: this work must be entirely your own, with no outside assistance other than the instructor.
CodingBat is a free, educational site developed by Nick Parlante at Stanford. It contains short programming problems that can be completed online, either in Java or Python. Problems are divided into categories such as String-1 (String manipulation exercises) and Logic-1 (conditionals and boolean logic exercises). Each problem provides a small description and then requires you to enter the code into a function skeleton. When you click on the Go button, your code is automatically compiled, executed, and graded in the page.
For the first part of this assignment, you must complete:
In order to receive credit for your work, you must create an account at CodingBat and list me as your instructor (davereed@creighton.edu). That way, I can review your answers and keep track of how many you have solved. Note: you must be logged in when working on problems in order for CodingBat to remember you work.
translate
function takes three inputs, the names of the source file (the file to be translated), the vocabulary file (the corresponding phrases in two languages), and the destination file (the file where the translation is to be stored).
Two different vocabulary files, spanish.txt (English to Spanish) and pirate.txt (English to Pirate), have been provided for you. Each line in that file consists of an English word/phrase and its corresponding translation, separated by "|"
.
translate
function so that it utilizes file dialog windows to allow the user to select the three files. Each file dialog window should have a title that tells the user the purpose of the file they are selecting, e.g., Select the text file to be translated.
translate
function is case sensitive. For example, if "bathroom|bano"
is in the vocabulary file, then each occurrence of "bathroom"
would be replaced with "bano"
, but "Bathroom"
would not. Modify the translate
function so that it also replaces the capitalized versions of the vocabulary words (e.g., "bathroom"
→"bano"
and "Bathroom"
→"Bano"
). Note: the Python string method capitalize
should be useful here.
translate
function will replace words/phrases regardless of where they appear in the text. This can lead to some unexpected results. For example, the provided pirate vocabulary contains "is|be"
, so that "It is good."
would translate to "It be good."
However, it also means that "His wish"
would translate to "Hbe wbeh."
In order to take context into account when replacing strings, a function from a separate module is needed. The re
module contains a function named sub
, which similarly substitutes one piece of text for another in a string. However, if the first input, the text to be replaced, is surrounded by "\\b"
, then that text will only be substituted for if it is a distinct word/phrase (surrounded by either whitespace or punctuation marks). For example,
Modify the translate
function so that it utilizes re.sub
to perform the substitutions and only replaces words/phrases that are distinct.
translateBack
that behaves similarly to the translate
function, but in reverse. For example, given the pirate.txt
vocabulary file, the translateBack
would translate Pirate words and phrases back into English. In theory, translating text from one language to another and then back again should result in the recovery of the original text. However, translator incompleteness and ambiguities in the language will often result in imperfect translations, which will become quite apparent on the reverse translation.
Note: since the translateBack
function behaves similarly to translate
, it would not be surprising if there was significant duplication in the code of the two functions. For (a modest amount of) EXTRA CREDIT, factor out common code so that duplication is minimized.