2016-10-12 5 views
7

में मिलाएं मैं लैटिन छंदों में एक स्ट्रिंग को हाइफनेट करने के लिए एक कोड लिखने की कोशिश कर रहा हूं। इसमें कुछ बाधाएं हैं जिनकी मैंने देखभाल की है, हालांकि मुझे वांछित आउटपुट नहीं मिलता है। मेरे कोड नीचे दिया गया हैएकाधिक रेगेक्स को एक

<?php 

$string = "impulerittantaenanimis caelestibusirae"; 

$precedingC = precedingConsonant($string); 
$xrule = xRule($precedingC); 
$consonantc = consonantCT($xrule); 
$consonantp = consonantPT($consonantc); 
$cbv = CbetweenVowels($consonantp); 
$tv = twoVowels($cbv); 

echo $tv; 

function twoVowels($string) 
{ 
    return preg_replace('/([aeiou])([aeiou])/', '$1-$2', $string); 
} 
function CbetweenVowels($string) 
{ 
    return preg_replace('/([aeiou])([^aeiou])([aeiou])/', '$1-$2$3', $string); 
} 
function consonantPT($string) 
{ 
    return preg_replace('/([^aeiou]p)(t[aeiou])/', '$1-$2', $string); 
} 
function consonantCT($string) 
{ 
    return preg_replace('/([^aeiou]c)(t[aeiou])/', '$1-$2', $string); 
} 
function precedingConsonant($string) 
{ 
    $arr1 = str_split($string); 
    $length = count($arr1); 
    for($j=0;$j<$length;$j++) 
    { 
     if(isVowel($arr1[$j]) && !isVowel($arr1[$j+1]) && !isVowel($arr1[$j+2]) && isVowel($arr1[$j+3])) 
     { 
      $pc++; 
     } 
    } 

    function strAppend2($string) 
    { 
     $arr1 = str_split($string); 
     $length = count($arr1); 


     for($i=0;$i<$length;$i++) 
     { 
      $check = $arr1[$i+1].$arr1[$i+2]; 
      $check2 = $arr1[$i+1].$arr1[$i+2].$arr1[$i+3]; 
      if($check=='br' || $check=='cr' || $check=='dr' || $check=='fr' || $check=='gr' || $check=='pr' || $check=='tr' || $check=='bl' || $check=='cl' || $check=='fl' || $check=='gl' || $check=='pl' || $check=='ch' || $check=='ph' || $check=='th' || $check=='qu' || $check2=='phl' || $check2=='phr') 
      { 
       if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3])) 
       { 
        $updatedString = substr_replace($string, "-", $i+1, 0); 
        return $updatedString; 
       } 
      } 
      else 
      { 
       if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3])) 
       { 
        $updatedString = substr_replace($string, "-", $i+2, 0); 
        return $updatedString; 
       } 
      } 
     } 
    } 
    $st1 = $string; 
    for($k=0;$k<$pc;$k++) 
    { 
     $st1 = strAppend2($st1); 
    } 

    return $st1; 
} 
function xRule($string) 
{ 
    return preg_replace('/([aeiou]x)([aeiou])/', '$1-$2', $string); 
} 
function isVowel($ch) 
{ 
    if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u') 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
function isConsonant($ch) 
{ 
    if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u') 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

?> 

मेरा मानना ​​है कि अगर मैं इन सभी कार्यों यह वांछित आउटपुट में परिणाम होगा गठबंधन। हालांकि मैं नीचे मेरी कमी निर्दिष्ट करेगा: यदि आप प्रत्येक नियम पर ध्यान से देखो

Rule 1 : When two or more consonants are between vowels, the first consonant is joined to the preceding vowel; for example - rec-tor, trac-tor, ac-tor, delec-tus, dic-tator, defec-tus, vic-tima, Oc-tober, fac-tum, pac-tus, 

Rule 2 : 'x' is joined to the preceding vowel; as, rex-i. 

However we give a special exception to the following consonants - br, cr, dr, fr, gr, pr, tr; bl, cl, fl, gl, pl, phl, phr, ch, ph, th, qu. These consonants are taken care by adding them to the later vowel for example - con- sola-trix 
n- sola-trix. 

Rule 3 : When 'ct' follows a consonant, that consonant and 'c' are both joined to the first vowel for example - sanc-tus and junc-tum 

Similarly for 'pt' we apply the same rule for example - scalp-tum, serp-tum, Redemp-tor. 

Rule 4 : A single consonant between two vowels is joined to the following vowel for example - ma-ter, pa-ter AND Z is joined to the following vowel. 

Rule 5 : When two vowels come together they are divided, if they be not a diphthong; as au-re-us. Diaphthongs are - "ae","oe","au" 
+1

क्या आप निश्चित हैं कि सिसीरो उससे सहमत होंगे? –

+0

एकाधिक रेगेक्स या फ़ंक्शंस को गठबंधन करने से पहले, आपको पहले यह निर्धारित करना होगा कि प्रत्येक फ़ंक्शन ऐसा करता है जो इसे करना है। उदाहरण के लिए, 'दोवॉल्स' फ़ंक्शन डिफथॉन्ग का खाता नहीं लेता है। –

+0

'strAppend2' क्या करना है? आपको अपना कोड टिप्पणी करनी चाहिए और अपने प्रत्येक फ़ंक्शन के लिए संक्षिप्त विवरण जोड़ना चाहिए (phpDoc के बारे में खोजें)। –

उत्तर

3

, आपको लगता है कि सब शुरुआत में एक स्वर या एक पूर्ववर्ती स्वर शामिल देख सकते हैं। एक बार जब आप यह है कि एहसास, आप एक ही शुरुआत में कारक में [aeiou] डाल पैटर्न बनाने की कोशिश कर सकते हैं:

$pattern = '~ 
    (?<=[aeiou]) # each rule involves a vowel at the beginning (also called a 
       # "preceding vowel") 
    (?: 
     # Rule 2: capture particular cases 
     ((?:[bcdfgpt]r | [bcfgp] l | ph [lr] | [cpt] h | qu) [aeiou] x) 
     | 
     [bcdfghlmnp-tx] 
     (?: 
      # Rule 3: When "ct" follows a consonant, that consonant and "c" are both 
      # joined to the first vowel 
      [cp] \K (?=t) 
      | 
      # Rule 1: When two or more consonants are between vowels, the first 
      # consonant is joined to the preceding vowel 
      \K (?= [bcdfghlmnp-tx]+ [aeiou]) 
     ) 
     | 
     # Rule 4: a single consonant between two vowels is joined to the following 
     # vowel 
     (?: 
      \K (?= [bcdfghlmnp-t] [aeiou]) 
      | 
      # Rule 2: "x" is joined to the preceding vowel 
      x \K (?= [a-z] | (*SKIP)(*F)) 
     ) 
     | 
     # Rule 5: When two vowels come together they are divided, if they not be a 
     # diphthong ("ae", "oe", "au") 
     \K (?= [aeiou] (?<! a[eu] | oe)) 
    ) 
~xi'; 

यह पैटर्न ही जहां (नियम 2 के विशेष मामलों को छोड़कर हाइफन डाल करने के लिए स्थिति मैच के लिए डिज़ाइन किया गया है), यही कारण है कि यह इस स्थिति पर मिलान परिणाम शुरू करने के लिए \K का उपयोग करता है और मिलान करने वाले पात्रों के बिना क्या होता है इसका परीक्षण करने के लिए देखो।

$string = <<<EOD 
Aeneadum genetrix, hominum diuomque uoluptas, 
alma Uenus, caeli subter labentia signa 
quae mare nauigerum, quae terras frugiferentis 
concelebras, per te quoniam genus omne animantum 
EOD; 

$result = preg_replace($pattern, '-$1', $string); 

ऐ-ne-ए-डम जीई-ने-ट्रिक्स, हो-मील-संख्या डाई-यू-ओम-qu-ए यू ओ-Lup-TAS,
अल मा UE-नुस , cae-li sub-ter la-ben-ti-a sig-na
qu-ae ma-re nau-i-ge-rum, qu-ae ter-ras fru-gi-fe-ren-tis
चोर-ce-Leb-रास, प्रति ते qu-ओ-नी-am जीई-नुस ओम-ne एक-नी-मानव tum

ध्यान दें कि मैं कश्मीर की तरह कई पत्र शामिल नहीं किया था, वाई और जेड जो लैटिन वर्णमाला में मौजूद नहीं है, अगर आप एन को शामिल करने के लिए स्वतंत्र महसूस करें अनुवादित ग्रीक शब्दों या अन्य को संभालने के लिए eed।

संबंधित मुद्दे