我试图解决使用PHP转换为拉丁文的问题。我被卡住了,因为根据上下文的不同,Y既可以是元音也可以是辅音。如果Y在单词的开头,则被认为是辅音,如果它在中间被认为是元音。PHP猪拉丁语,Y既作为元音和辅音
例如“黄色风格”变成“ellowyay ylestay”。规则是:“以元音开始的单词(A,E,I,O,U)只在单词的末尾添加”WAY“。 以辅音开头的单词具有所有辅音字母第一个元音移到了单词的末尾(而不是第一个辅音字母),并且附加了“AY”(在这种情况下,'Y'被计算为元音)“
我的代码如下:
class Config{
public static $vowels = 'aeiou';
public static $vowelTermination = "way";
public static $consonants = 'b-df-hj-np-tv-z';
}
class Piglatin
{
public function convert($input)
{
$return = "";
$wordArray = explode(" ", $input);
foreach($wordArray as $word){
$return .= $this->translate($word);
$return .= " ";
}
return rtrim($return);
}
public function translate($input)
{
$translation = "";
if(!empty($input)){
if(is_numeric($input)){
return $input;
}
if($this->startVowel($input)){
$input = $input . Config::$vowelTermination;
return $input;
}
if($this->startConsonant($input) && strlen($input)===1){
return $input.'ay';
}
if($this->startConsonant($input)){
$input = preg_replace('/^([b-df-hj-np-tv-xz]*)([aeiouy].*)$/', "$2$1ay", $input);
return $input;
}
}
return $translation;
}
public function startVowel($input)
{
$regex = '/^['.Config::$vowels.']/i';
if(preg_match($regex, $input)){
return true;
}
return false;
}
public function startConsonant($input)
{
$regex = '/^['.Config::$consonants.']/i';
if(preg_match($regex, $input)){
return true;
}
return false;
}
}
其中给定输入“黄色风格”会产生接近预期结果但不完全的“黄色ylestay”。
关于如何解决这个问题的任何想法?
见https://github.com/davidyell/Pig -Latin-Translator/blob/master/lib/translate.php –
上面你说:'例如“黄色风格”变成“ellowyay ylestay”。规则是:'。在底部,你说:'哪个给定的输入“黄色风格”产生“ellowyay ylestay”这是接近预期的结果,但不完全' – sln
不重复,但存在于github:D @WiktorStribiżew – MohaMad