import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BabyTalkTranslator {
// My little dictionary of baby talk words
private static final Map<String, String> SPECIFIC_TRANSFORMS = new HashMap<>();
static {
SPECIFIC_TRANSFORMS.put("the", "da");
SPECIFIC_TRANSFORMS.put("and", "an'");
SPECIFIC_TRANSFORMS.put("hello", "hewwo");
SPECIFIC_TRANSFORMS.put("goodbye", "bye-bye");
SPECIFIC_TRANSFORMS.put("mother", "mama");
SPECIFIC_TRANSFORMS.put("father", "dada");
SPECIFIC_TRANSFORMS.put("dog", "doggy");
SPECIFIC_TRANSFORMS.put("cat", "kitty");
SPECIFIC_TRANSFORMS.put("rabbit", "wabbit");
SPECIFIC_TRANSFORMS.put("tree", "twee");
SPECIFIC_TRANSFORMS.put("river", "wiver");
SPECIFIC_TRANSFORMS.put("boat", "boatie");
SPECIFIC_TRANSFORMS.put("day", "day-day");
// Could add more, but this gets us started!
}
/**
* Turns regular text into baby talk.
* @param text The text you want to translate
* @return The baby talk version
*/
public static String translate(String text) {
// This regex splits text into words (with apostrophes) and everything else
Pattern p = Pattern.compile("([\\w']+)|(\\W+)");
Matcher m = p.matcher(text);
StringBuilder result = new StringBuilder();
while (m.find()) {
String part = m.group();
if (part.matches("[\\w']+")) {
// It’s a word—let’s make it baby talk!
String transformed = transformWord(part);
result.append(transformed);
} else {
// Punctuation or spaces—keep them as is
result.append(part);
}
}
return result.toString();
}
/**
* Changes one word into baby talk, keeping the right capitals.
* @param word The word to change
* @return The baby talk word
*/
private static String transformWord(String word) {
String lowerWord = word.toLowerCase();
String transformed;
// Check my dictionary first
if (SPECIFIC_TRANSFORMS.containsKey(lowerWord)) {
transformed = SPECIFIC_TRANSFORMS.get(lowerWord);
} else {
// No match? Use the general rules
transformed = applySubstitutions(lowerWord);
}
// Match the original capitalization
if (word.length() > 0 && Character.isUpperCase(word.charAt(0))) {
transformed = Character.toUpperCase(transformed.charAt(0)) + transformed.substring(1);
}
if (word.equals(word.toUpperCase())) {
transformed = transformed.toUpperCase();
}
return transformed;
}
/**
* Applies my sound swap rules to a word.
* @param word The word to tweak
* @return The tweaked word
*/
private static String applySubstitutions(String word) {
word = word.replace("th", "d"); // "this" → "dis"
word = word.replace("r", "w"); // "run" → "wun"
if (word.endsWith("ing")) { // "singing" → "singin'"
word = word.substring(0, word.length() - 3) + "in'";
}
return word;
}
public static void main(String[] args) {
// The challenge’s sample text 1
String sample1 = "Then the two animals stood and regarded each other cautiously.\n" +
"\"Hullo, Mole!\" said the Water Rat.\n" +
"\"Hullo, Rat!\" said the Mole.\n" +
"\"Would you like to come over?\" enquired the Rat presently.\n" +
"\"Oh, it's all very well to talk,\" said the Mole rather pettishly, he being new to a river and riverside life and its ways.\n" +
"[...]\n" +
"\"This has been a wonderful day!\" said he, as the Rat shoved off and took to the sculls again. \"Do you know, I've never been in a boat before in all my life.\"";
System.out.println("Here’s the first sample in baby talk:");
System.out.println(translate(sample1));
System.out.println("\n");
// A little shoutout to Sri Lanka!
String sriLankaText = "Sri Lanka is a beautiful island with stunning beaches, lush mountains, and aromatic tea plantations. Visitors can explore ancient temples, enjoy wildlife safaris, and relax on golden sands.";
System.out.println("Now, a baby talk trip to Sri Lanka:");
System.out.println(translate(sriLankaText));
}
}