2012-01-10 11 views

risposta

11

Penso che dovresti essere in grado di utilizzare the grapheme_extract function per iterare sui caratteri combinati (che tecnicamente sono chiamati "grapheme cluster").

In alternativa, se si preferisce l'approccio regex, penso che è possibile utilizzare questo:

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

dove \pL significa una "lettera" Unicode, e \pM significa un Unicode "marchio".

(Disclaimer: non ho ancora testato uno di questi approcci.)

+0

L'ho provato e corrisponde correttamente ai caratteri. – Danack

+0

@ Danack57: Fantastico, grazie mille. :-) – ruakh

+0

grazie. Funziona bene. – priyacst

2

se ho capito bene la tua domanda, hai una stringa unicode contiene codepoints, e si desidera convertire questo in un array di graphames?

Sto lavorando allo sviluppo di una libreria Python open source per eseguire attività come questa per un Tamil Language website.

Non ho usato PHP da un po ', quindi posterò la logica. Puoi dare un'occhiata al codice nello amuthaa/TamilWord.py file's split_letters() function.

Come ha detto ruakh, i grafemi tamil sono costruiti come codepoint.

  • Le vocali (உயிர் எழுத்து), aytham (ஆய்த எழுத்து - ஃ) e tutte le combinazioni ((உயிர்-மெய் எழுத்து) in 'un' colonna (அ வரி - cioè க, ச, ட, த , ப, ற, ங, ஞ, ண, ந, ம, ன, ய, ர, ள, வ, ழ, ல) ciascuno utilizzano un'unica codepoint

  • ogni consonante si compone di due codepoints:. la una combinazione lettera + la pulli Ad esempio ப் = ப + ்

  • Ogni combinazione diversa dalle combinazioni a è o costituiti da due codepoints: la lettera a-combinazione di + una marcatura: ad esempio பி = ப் + ி, தை = த் + ை

Quindi, se la logica sta per essere qualcosa di simile:

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 

Questo ovviamente presuppone che la stringa sia ben formata e non si abbiano elementi come due segni di fila.

Ecco il codice Python, nel caso in cui lo trovi utile. Se vuoi aiutarci a portarlo in PHP, per favore fatemelo sapere:

@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 
Problemi correlati