{}
Learn DSA the way it should be — with step-by-step code visualization.
Try now!
Learn DSA with step-by-step code visualization.
Try now!
run-icon
main.php
<?php function babyTalk($text) { // Limit to 100 words $words = explode(' ', $text); $words = array_slice($words, 0, 100); $baby_words = []; // Simple word replacements $dictionary = [ 'mother' => 'mama', 'father' => 'dada', 'hello' => 'hewwo', 'hullo' => 'hewwo', 'yes' => 'yeth', 'no' => 'nuu', 'boat' => 'boatie', 'rabbit' => 'wabbit', 'run' => 'wun', 'little' => 'wittle', 'big' => 'biig', 'very' => 'vewwy', 'accident' => 'boo-boo', 'garden' => 'gawden', 'life' => 'wifey', 'fields' => 'feeldsies', 'go' => 'go-go', 'said' => 'sed', 'once' => 'wunce', ]; foreach ($words as $word) { $original = $word; $lower = strtolower(preg_replace('/[^a-z]/i', '', $word)); $punct = preg_replace('/[a-z]/i', '', $word); // Save punctuation // Replace from dictionary if possible if (isset($dictionary[$lower])) { $baby_word = $dictionary[$lower]; } else { // Baby-ify the word $baby_word = babyify($lower); } // Restore punctuation if (ctype_upper($original[0])) { $baby_word = ucfirst($baby_word); } $baby_words[] = $baby_word . $punct; } return implode(' ', $baby_words); } // Baby-ify a word using simple phonetic softening and doubling function babyify($word) { if (strlen($word) <= 3) return $word; // Softening hard consonants $word = str_replace(['r', 'l'], 'w', $word); $word = preg_replace('/tion$/', 'shun', $word); // Duplicate first syllable if short if (preg_match('/^([b-df-hj-np-tv-z]{1,2}[aeiou])/', $word, $match)) { $word = $match[1] . '-' . $word; } // Add cute suffix if (substr($word, -1) === 'y') { return $word; } else { return $word . 'y'; } } // === Test Run === $sample1 = <<<EOT Then the two animals stood and regarded each other cautiously. "Hullo, Mole!" said the Water Rat. "Hullo, Rat!" said the Mole. "Would you like to come over?" enquired the Rat presently. "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. "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." EOT; $sample2 = <<<EOT Once upon a time there were four little Rabbits, and their names were— Flopsy, Mopsy, Cotton-tail, and Peter. They lived with their Mother in a sand-bank, underneath the root of a very big fir-tree. 'Now my dears,' said old Mrs. Rabbit one morning, 'you may go into the fields or down the lane, but don't go into Mr. McGregor's garden: your Father had an accident there; he was put in a pie by Mrs. McGregor.' 'Now run along, and don't get into mischief. I am going out.' EOT; echo "=== Sample 1 Baby Talk ===\n"; echo babyTalk($sample1); echo "\n\n=== Sample 2 Baby Talk ===\n"; echo babyTalk($sample2); ?>
Output