2017-03-01 49 views
-1

我試圖解決使用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」。

關於如何解決這個問題的任何想法?

+2

見https://github.com/davidyell/Pig -Latin-Translator/blob/master/lib/translate.php –

+2

上面你說:'例如「黃色風格」變成「ellowyay ylestay」。規則是:'。在底部,你說:'哪個給定的輸入「黃色風格」產生「ellowyay ylestay」這是接近預期的結果,但不完全' – sln

+0

不重複,但存在於github:D @WiktorStribiżew – MohaMad

回答

0

preg_replace中的正則表達式與第一個捕獲組中的Y不匹配:([b-df-hj-np-tv-xz]*)。所以這些組最終爲$1=''$2='yellow',因此結果是yelloway

我沒有測試過,但這應該工作,不是嗎?

preg_replace('/^([b-df-hj-np-tv-z]*)([aeiouy].*)$/', "$2$1ay", $input) 
+0

通過這種方式,他不再考慮字母中間的Y作爲元音。例如,我現在得到的是「ellowyay estylay」而不是「ellowyay ylestay」,whoch是預期的結果。 –

1

對我來說,你有3個單獨的條件。
這個正則表達式使用分支重置(並行組)與這些條件。
必須強制執行每個條件,這就是它的作用。

請注意,它不計算邊界,但如果您需要,我會使用空白邊界。 (?<!\S)(?: regex)(?!\S)
此外,您還可以其他字符[b-df-hj-np-tv-xz]
但不能添加到[a-z]類或元音類[aeiouy][y]類。

基本上它是
查找(?i)(?|([aeiou][a-z]*)()|([b-df-hj-np-tv-xz]+)([aeiouy][a-z]*)|([y]+[b-df-hj-np-tv-xz]*)([aeiouy][a-z]*))
更換$2$1ay

擴展

(?i)        # Modifier - case independent 
(?|        # Branch Reset 
     ([aeiou] [a-z]*)    # (1), Condition: starts with a vowel 
     ()        # (2) 
    | 
     ([b-df-hj-np-tv-xz]+)   # (1), Condition: starts with a non-Y consonant 
     ([aeiouy] [a-z]*)    # (2), and Y as vowel 
    | 
     ([y]+ [b-df-hj-np-tv-xz]*)  # (1), Condition: starts with a Y consonant 
     ([aeiouy] [a-z]*)    # (2), and Y as vowel 
) 

樣品輸入yellow style atak yyerto yystay start
樣本輸出ellowyay ylestay atakay ertoyyay ayyystay artstay

+0

它總是有意義的。將盡快嘗試。 –

+0

@CláudioRibeiro - 你解決了什麼問題? – sln