Skip to content

Instantly share code, notes, and snippets.

@dcorto
Last active January 29, 2018 18:10
Show Gist options
  • Save dcorto/d113ee550b8eb3f28607cff7b477f27d to your computer and use it in GitHub Desktop.
Save dcorto/d113ee550b8eb3f28607cff7b477f27d to your computer and use it in GitHub Desktop.
<?php
/**
* Goat Latin is a made-up language based off of English, sort of like Pig Latin.
*
* The rules of Goat Latin are as follows:
* 1. If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add 'ma'.
* For example, the word 'goat' becomes 'oatgma'.
*
* 2. If a word begins with a vowel, append 'ma' to the end of the word.
* For example, the word 'i' becomes 'ima'.
*
* 3. Add one letter "a" to the end of each word per its word index in the sentence, starting with 1.
* That is, the first word gets "a" added to the end, the second word gets "aa" added to the end, the third word in
* the sentence gets "aaa" added to the end, and so on.
*
* Write a function that, given a string of words making up one sentence, returns that sentence in Goat Latin. For example:
*
* string_to_goat_latin('I speak Goat Latin') would return: 'Imaa peaksmaaa oatGmaaaa atinLmaaaaa'
*
*/
error_reporting(E_ALL);
/**
* Translates from Latin to Goat Latin
*
* @param $input
* @return string
*/
function string_to_goat_latin($input)
{
$array = explode(" ", $input);
$translation = array();
$count = 1;
foreach ($array as $word) {
$translation[] = translate($word, $count);
$count++;
}
$translation = implode(" ", $translation);
return $translation;
}
/**
* Apply the diferent rules for translate from Latin to Goat Latin
*
* @param $word
* @param $count
* @return string
*/
function translate($word, $count)
{
$tranlation = $word;
if (!empty($tranlation)) {
if (testStartWithVowel($word)) {
$tranlation = vowel($word);
} elseif (testStartWithConsonant($word)) {
$tranlation = consonant($word);
}
$tranlation = append($tranlation, $count);
}
return $tranlation;
}
/**
* Apply the 1st modifier for the Goat Latin language
*
* @param $word
* @return string
*/
function consonant($word)
{
return substr($word, 1).$word[0].'ma';
}
/**
* Apply the 2nd modifier for the Goat Latin language
*
* @param $word
* @return string
*/
function vowel($word)
{
return $word.'ma';
}
/**
* Apply the 3rd modifier for the Goat Latin language
*
* @param $word
* @param $count
* @return string
*/
function append($word, $count)
{
$append = "a";
for ($i=1;$i<$count;$i++) {
$append .= 'a';
}
return $word.$append;
}
/**
* * Checks if $word start with consonant
*
* @param $word
* @return bool
*/
function testStartWithConsonant($word)
{
$regex = '/^[b-df-hj-np-tv-z]/i';
if (preg_match($regex, $word)) {
return true;
}
return false;
}
/**
* Checks if $word start with vowel
*
* @param $word
* @return bool
*/
function testStartWithVowel($word)
{
$regex = '/^[aeiou]/i';
if (preg_match($regex, $word)) {
return true;
}
return false;
}
echo string_to_goat_latin('I speak Goat Latin');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment