PHP

2012-01-10 4 views
10

में तमिल वर्णों को एक स्ट्रिंग में विभाजित करने के लिए कैसे करें मैं तमिल वर्णों को एक स्ट्रिंग में कैसे विभाजित करूं?PHP

जब मैं preg_match_all('/./u', $str, $results) उपयोग करते हैं,
मैं वर्ण प्राप्त "த", "ம", "ி", "ழ" और "்"।

मैं संयुक्त अक्षर "த", "மி" और "ழ்" कैसे प्राप्त करूं?

उत्तर

11

मुझे लगता है कि आप संयुक्त वर्णों (जिसे तकनीकी रूप से "ग्रैफेम क्लस्टर" कहा जाता है) पर फिर से चलाने के लिए the grapheme_extract function का उपयोग करने में सक्षम होना चाहिए।

वैकल्पिक रूप से, अगर आप regex दृष्टिकोण पसंद करते हैं, मुझे लगता है कि आप इस का उपयोग कर सकते हैं:

preg_match_all('/\pL\pM*|./u', $str, $results) 

जहां \pL यूनिकोड "पत्र" का अर्थ है, और \pM का मतलब है एक यूनिकोड "निशान"।

(अस्वीकरण: मैं इनमें से किसी उपाय का परीक्षण किया है नहीं।)

+0

मुझे लगता है कि परीक्षण किया है और इसे सही ढंग से वर्णों से मेल नहीं करता है। – Danack

+0

@ डेनैक 57: कूल, बहुत बहुत धन्यवाद। :-) – ruakh

+0

धन्यवाद। यह अच्छा काम करता है। – priyacst

2

अगर मैं आपके सवाल का सही ढंग से समझ, आप कोड पॉइंट्स युक्त एक यूनिकोड स्ट्रिंग मिल गया है, और आप graphames की एक सरणी में इस परिवर्तित करना चाहते हैं?

मैं Tamil Language website के लिए इस तरह के कार्यों को करने के लिए एक ओपन सोर्स पायथन पुस्तकालय विकसित करने पर काम कर रहा हूं।

मैंने थोड़ी देर में PHP का उपयोग नहीं किया है, इसलिए मैं तर्क पोस्ट करूंगा। आप amuthaa/TamilWord.py file's split_letters() function में कोड को देख सकते हैं।

जैसा कि खंड का उल्लेख किया गया है, तमिल ग्रैफेम्स को कोडपॉइंट्स के रूप में बनाया गया है।

  • स्वर (உயிர் எழுத்து), aytham (ஆய்த எழுத்து - ஃ) और सभी संयोजनों ((உயிர்-மெய் 'एक' कॉलम में எழுத்து) (அ வரி - यानी க, ச, ட, த , ப, ற, ம, ஞ, σ, ந, ம, வ, 繁, ர, ள, வ, ழ, ல) प्रत्येक एक एकल कोडपॉइंट का उपयोग करते हैं।

  • प्रत्येक व्यंजन दो कोडपॉइंट्स से बना होता है: एक संयोजन पत्र + पुली। जैसे ப் = ப + ்

  • ए-संयोजन के अलावा प्रत्येक संयोजन als हैं ओ दो कोड पॉइंट्स से बना: एक-संयोजन पत्र + एक अंकन: जैसे பி = ப் + ி, தை = த் + ை

इसलिए यदि आपके तर्क कुछ इस तरह होने जा रहा है:

initialize an empty array 

for each codepoint in word: 

    if the codepoint is a vowel, a-combination or aytham, it is also its grapheme, so add it to the array 

    otherwise, the codepoint is a marking such as the pulli (i.e. ்) or one of the combination extensions (e.g. ி or ை), so append it to the end of the last element of the array 

यह निश्चित रूप से मानता है कि आपकी स्ट्रिंग अच्छी तरह से बनाई गई है और आपके पास पंक्ति में दो चिह्नों जैसी चीजें नहीं हैं।

यहां पाइथन कोड है, यदि आपको यह उपयोगी लगता है। आप PHP करने के लिए हमें बंदरगाह इस मदद करना चाहते हैं, तो मुझे रूप में अच्छी तरह से अवगत कराएं:

@staticmethod 
def split_letters(word=u''): 
    """ Returns the graphemes (i.e. the Tamil characters) in a given word as a list """ 

    # ensure that the word is a valid word 
    TamilWord.validate(word) 

    # list (which will be returned to user) 
    letters = [] 

    # a tuple of all combination endings and of all அ combinations 
    combination_endings = TamilLetter.get_combination_endings() 
    a_combinations = TamilLetter.get_combination_column(u'அ').values() 

    # loop through each codepoint in the input string 
    for codepoint in word: 

     # if codepoint is an அ combination, a vowel, aytham or a space, 
     # add it to the list 
     if codepoint in a_combinations or \ 
      TamilLetter.is_whitespace(codepoint) or \ 
      TamilLetter.is_vowel(codepoint) or \ 
      TamilLetter.is_aytham(codepoint): 

      letters.append(codepoint) 

     # if codepoint is a combination ending or a pulli ('்'), add it 
     # to the end of the previously-added codepoint 
     elif codepoint in combination_endings or \ 
      codepoint == TamilLetter.get_pulli(): 

      # ensure that at least one character already exists 
      if len(letters) > 0: 
       letters[-1] = letters[-1] + codepoint 

      # otherwise raise an Error. However, validate_word() 
      # should catch this 
      else: 
       raise ValueError("""%s cannot be first character of a word""" % (codepoint)) 

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

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