-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPigLatinTranslator.java
More file actions
67 lines (54 loc) · 2.74 KB
/
PigLatinTranslator.java
File metadata and controls
67 lines (54 loc) · 2.74 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
package main.java;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 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 static String translate (String englishPhrase){
char ch = englishPhrase.charAt(0);
Pattern startWithSqu = Pattern.compile("^squ");
Pattern startWithTh = Pattern.compile("^th");
Pattern startWithThr = Pattern.compile("^thr");
Pattern startWithSch = Pattern.compile("^sch");
Pattern startWithYt = Pattern.compile("^yt");
Pattern startWithXr = Pattern.compile("^xr");
String s;
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') {
s = englishPhrase + "ay";
} else if (ch=='c') {
s = englishPhrase.substring(2) + "chay";
} else if (ch=='q') {
s = englishPhrase.substring(2) + "quay";
} else if (startWithSqu.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "squay";
} else if (startWithThr.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "thray";
} else if (startWithTh.matcher(englishPhrase).find()) {
s = englishPhrase.substring(2) + "thay";
} else if (startWithSch.matcher(englishPhrase).find()) {
s = englishPhrase.substring(3) + "schay";
} else if (startWithYt.matcher(englishPhrase).find()) {
s = englishPhrase + "ay";
} else if (startWithXr.matcher(englishPhrase).find()) {
s = englishPhrase + "ay";
} else {
s = englishPhrase.substring(1) + englishPhrase.charAt(0) + "ay";
}
return s;
}
}