-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPigLatinTranslator.java
More file actions
71 lines (63 loc) · 3.62 KB
/
PigLatinTranslator.java
File metadata and controls
71 lines (63 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Implement a program that translates from English to Pig Latin.
*
* Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below),
* but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.
*
* Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
* Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay").
* Rule 2: If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound to the end of the word.
* Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay").
* Rule 3: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay").
* Rule 4: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").
*
* There are a few more rules for edge cases, and there are regional variants too.
*
* See http://en.wikipedia.org/wiki/Pig_latin for more details.
*/
public class PigLatinTranslator {
public String translate(String englishPhrase) {
String[] words = englishPhrase.split(" ");
String pigLatinPhrase = "";
for (String word: words) {
if (pigLatinPhrase != "") {
pigLatinPhrase += " ";
}
pigLatinPhrase += translateWord(word);
}
return pigLatinPhrase;
}
public String translateWord(String word) {
// If word begins with a vowel, add an "ay" to the end of the word
if(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o")
|| word.startsWith("u") || word.startsWith("xr") || word.startsWith("yt")) {
return word + "ay";
}
// If word starts with a consonant followed by "qu",
// move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay")
else if(word.indexOf("qu") == 1) {
word = word.substring(3) + word.substring(0, 1) + "quay";
}
// word starting with "sch" or "thr" or "th" or "qu" or "ch" --> take first substring and move it to the end, then add "ay"
else if(word.startsWith("sch") || word.startsWith("thr")) {
word = word.substring(3) + word.substring(0, 3) + "ay";
}
// word starting with "th" or "qu" or "ch" --> take first substring and move it to the end, then add "ay"
else if(word.startsWith("th") || word.startsWith("qu") || word.startsWith("ch")) {
word = word.substring(2) + word.substring(0, 2) + "ay";
}
// If word contains a "y" after a consonant cluster or as the second letter in a two letter word
// it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").
else if(!(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u")) &&
!(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u")) &&
word.indexOf("y") == 2) {
word = word.substring(2) + word.substring(0, 2) + "ay";
}
// If word begins with consonant, move it to the end of the word and then add "ay" to the end of the word
else if(word.substring(0, 1) != "a" || word.substring(0, 1) != "e" || word.substring(0, 1) != "i"
|| word.substring(0, 1) != "o" || word.substring(0, 1) != "u") {
word = word.substring(1) + word.substring(0, 1) + "ay";
}
return word;
}
}