Created
October 23, 2011 22:42
-
-
Save devlaers/1308006 to your computer and use it in GitHub Desktop.
This program translates a user input phrase into pirate talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 | |
hello ; ahoy | |
hi ; yo-ho-ho | |
pardon me ; avast | |
excuse me ; arrr | |
my ; me | |
friend ; me bucko | |
sir ; matey | |
madam ; proud beauty | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Name: Rachael Devlaeminck | |
// Assignment: Lab 02 | |
// Title: Pirate Translator | |
// Course: CSCE 270 | |
// Lab Section: Section 01 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 9/21/10 | |
// Sources consulted: None | |
// Program description: translates what the use types into pirate | |
// Known Bugs: None | |
// Creativity: maintaining capitalization through translation using StringBuilder | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class pirateTranslator { | |
private TranslationRule[] rules; | |
private int num; | |
private void loadRules() | |
{ | |
//make sure the file can be opened | |
try | |
{ | |
File file = new File("pirate_phrases.txt"); | |
Scanner scan = new Scanner(file); | |
num = scan.nextInt(); | |
scan.nextLine(); | |
rules = new TranslationRule[num]; | |
for(int i = 0; i < num; i++) | |
{ | |
String line = scan.nextLine(); | |
String[] sl = line.split(" ; "); | |
rules[i] = new TranslationRule(sl[0], sl[1]); | |
} | |
} | |
catch(FileNotFoundException e) | |
{ | |
System.out.println("File not found."); | |
return; | |
} | |
} | |
public pirateTranslator() | |
{ | |
loadRules(); | |
} | |
public String translate(String s) | |
{ | |
String[] words = s.split("[\\.!?\"]+"); | |
StringBuilder result = new StringBuilder(); | |
String finalString = ""; | |
String letter; | |
int count = 0; | |
for(int j = 0; j < words.length; j++) | |
{ | |
count += words[j].length(); | |
//test the first letter of the first word of the user input | |
if(j == 0) | |
{ | |
//see if the first word of the string is capitalized and translate | |
if(words[j].charAt(0) >= 'A' && words[j].charAt(0) <= 'Z') | |
{ | |
letter = words[j].substring(0, 1).toLowerCase(); | |
words[j] = letter + words[j].substring(1); | |
for(int i = 0; i < rules.length; i++) | |
{ | |
String e = rules[i].getEnglish(); | |
String p = rules[i].getPirate(); | |
words[j] = words[j].replaceAll("\\b" + e + "\\b", p); | |
} | |
letter = words[j].substring(0, 1); | |
result.append(letter.toUpperCase()); | |
result.append(words[j].substring(1)); | |
if (j == words.length-1) | |
result.append(s.substring(count)); | |
else | |
result.append(s.substring(count, count + 1)); | |
} | |
//if the first word is not capitalized | |
else | |
{ | |
for(int i = 0; i < rules.length; i++) | |
{ | |
String e = rules[i].getEnglish(); | |
String p = rules[i].getPirate(); | |
words[j] = words[j].replaceAll("\\b" + e + "\\b", p); | |
} | |
result.append(words[j]); | |
if (j == words.length-1) | |
result.append(s.substring(count)); | |
else | |
result.append(s.substring(count, count + 1)); | |
} | |
} | |
//test the first letter of the rest of the strings in the array | |
//to see if it is capitalized and translate | |
else | |
{ | |
//see if the first word of the string is capitalized and translate | |
if(words[j].charAt(1) >= 'A' && words[j].charAt(1) <= 'Z') | |
{ | |
letter = words[j].substring(1, 2).toLowerCase(); | |
words[j] = words[j].substring(0, 1) + letter + | |
words[j].substring(2); | |
for(int i = 0; i < rules.length; i++) | |
{ | |
String e = rules[i].getEnglish(); | |
String p = rules[i].getPirate(); | |
words[j] = words[j].replaceAll("\\b" + e + "\\b", p); | |
} | |
letter = words[j].substring(1, 2); | |
result.append(words[j].substring(0, 1)); | |
result.append(letter.toUpperCase()); | |
result.append(words[j].substring(2)); | |
if (j == words.length-1) | |
result.append(s.substring(count)); | |
else | |
result.append(s.substring(count, count + 1)); | |
} | |
//if the first word is not capitalized | |
else | |
{ | |
for(int i = 0; i < rules.length; i++) | |
{ | |
String e = rules[i].getEnglish(); | |
String p = rules[i].getPirate(); | |
words[j] = words[j].replaceAll("\\b" + e + "\\b", p); | |
} | |
result.append(words[j]); | |
if (j == words.length-1) | |
result.append(s.substring(count)); | |
else | |
result.append(s.substring(count, count + 1)); | |
} | |
} | |
count++; | |
finalString = result.toString(); | |
} | |
return finalString; | |
} | |
public void runInteraction() | |
{ | |
String line; | |
String pLine; | |
pirateTranslator pt = new pirateTranslator(); | |
Scanner keyboard = new Scanner(System.in); | |
System.out.println("Please enter a phrase."); | |
line = keyboard.nextLine(); | |
pLine = pt.translate(line); | |
System.out.println(pLine); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Name: Rachael Devlaeminck | |
// Assignment: Lab 02 | |
// Title: Pirate Translator | |
// Course: CSCE 270 | |
// Lab Section: Section 01 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 9/21/10 | |
// Sources consulted: None | |
// Program description: translates what the use types into pirate | |
// Known Bugs: None | |
// Creativity: maintaining capitalization through translation using StringBuilder | |
public class PirateTranslatorMain | |
{ | |
public static void main(String[] args) | |
{ | |
pirateTranslator translator = new pirateTranslator(); | |
translator.runInteraction(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Name: Rachael Devlaeminck | |
// Assignment: Lab 02 | |
// Title: Pirate Translator | |
// Course: CSCE 270 | |
// Lab Section: Section 01 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 9/21/10 | |
// Sources consulted: None | |
// Program description: translates what the use types into pirate | |
// Known Bugs: None | |
// Creativity: maintaining capitalization through translation using StringBuilder | |
public class TranslationRule { | |
private String english; | |
private String pirate; | |
public TranslationRule(String e, String p) | |
{ | |
english = e; | |
pirate = p; | |
} | |
public String getEnglish() | |
{ | |
return english; | |
} | |
public String getPirate() | |
{ | |
return pirate; | |
} | |
public void setEnglish(String newE) | |
{ | |
english = newE; | |
} | |
public void setPirate(String newP) | |
{ | |
pirate = newP; | |
} | |
public String applyRule(String english) | |
{ | |
return pirate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment