Talk Like A Pirate

David Reed         Creighton University


In 1995, John Baur and Mark Summers invented a new holiday, International Talk Like a Pirate Day. As they proposed it, on every September 19th, people around the world would be encouraged to talk like a pirate (with lots of "Arrrrs" and "Ahoys" and the like). At the official Talk Like a Pirate Day Web site, www.talklikeapirate.com, you can read about the history of the holiday, order official merchandise, and even experiment with a simple Web page that translates simple English phrases into pirate talk.


PART 1:

You are to design and implement a better Web page for translating English text into Pirate talk. JavaScript code for performing a simple translation is provided below. The PHRASES array contains English words/phrases and their pirate translations. The array is used by the Translate function, which takes a string as input, searches that string for substrings from PHRASES, and replaces the English phrases with their pirate translations. Your Web page should include a text area where the user can enter text, a button or clickable image to initiate the translation, and another text area where the translated text will appear. Feel free to be creative, adding images or embellishments to the page and expanding the English-Pirate vocabulary in PHRASES.

PHRASES = [["hello", "ahoy"], ["hi", "yo-ho-ho"], ["pardon me", "avast"], ["excuse me", "arrr"], ["my", "me"], ["friend", "me bucko"], ["sir", "matey"], ["madam", "proud beauty"], ["miss", "comely wench"], ["stranger", "scurvy dog"], ["officer", "foul blaggart"], ["where", "whar"], ["is", "be"], ["the", "th'"], ["you", "ye"], ["tell", "be tellin'"], ["know", "be knowin'"], ["how far", "how many leagues"], ["old", "barnacle-covered"], ["attractive", "comely"], ["happy", "grog-filled"], ["nearby", "broadside"], ["restroom", "head"], ["restaurant", "galley"], ["hotel", "fleabag inn"], ["pub", "Skull & Scuppers"], ["bank", "buried treasure"] ]; function Translate(text) // Returns: a copy of text with English phrases replaced by piratey equivalents { for (var i = 0; i < PHRASES.length; i++) { var toReplace = new RegExp("\\b"+PHRASES[i][0]+"\\b", "i"); var index = text.search(toReplace); while (index != -1) { text = text.replace(toReplace, PHRASES[i][1]); index = text.search(toReplace); } } return text; }


PART 2:

When searching for an English word or phrase to translate, the above code is case-insensitive. Thus, a search for "excuse me" would match whether that phrase occurred at the beginning, middle, or end of a sentence. Since the pirate phrase to be substituted is always lowercase, this can yield inconsistent results. Modify the JavaScript code so that if a match occurs and the first letter in the phrase to be replaced is uppercase, then the first letter in the substituted pirate phrase is likewise capitalized. Thus, "Excuse me, sir." would be translated as "Arrr, matey.", while "Sir, excuse me." would be translated as "Matey, arrr."


PART 3:

As we all know from watching too many bad pirate movies, pirates had a penchant for saying "Arrr". Add an additional feature to the Translate function so that it randomly inserts "Arrr." into the text between sentences. Your new code should be added at the end of the function, after the text substitutions have been completed (but before returning the modified text). Your code should traverse the text looking for periods, and after each period randomly decide (with a 50/50 chance) whether to insert an "Arrr." At no time should consecutive "Arrr."s be inserted, however.