유니코드 한글 분석

 

초성

LIST_OF_ONSET = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

중성

LIST_OF_NUCLEUS = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']

종성

LIST_OF_CODA = [' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

한글 OFFSET

OFFSET = 0xAC00

조합

@staticmethod
def compose(onset, nucleus, coda):
    onset_index = Korean.index_of_onset(onset)
    nucleus_index = Korean.index_of_nucleus(nucleus)
    coda_index = Korean.index_of_coda(coda)

    result = ((onset_index * len(Korean.LIST_OF_NUCLEUS)) + nucleus_index) * len(Korean.LIST_OF_CODA) + coda_index + Korean.OFFSET
    return chr(result)

분해

@staticmethod
def decompose(syllable):
    onset_index = (ord(syllable) - Korean.OFFSET) // (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))
    nucleus_index = (ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA)))) // len(Korean.LIST_OF_CODA)
    coda_index = ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))) - (nucleus_index * len(Korean.LIST_OF_CODA))
    return (Korean.LIST_OF_ONSET[onset_index], Korean.LIST_OF_NUCLEUS[nucleus_index], Korean.LIST_OF_CODA[coda_index])

전체 코드

class Korean:
    LIST_OF_ONSET = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
    LIST_OF_NUCLEUS = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
    LIST_OF_CODA = [' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

    OFFSET = 0xAC00

    @staticmethod
    def index_of_onset(ch):
        return Korean.LIST_OF_ONSET.index(ch)

    @staticmethod
    def index_of_nucleus(ch):
        return Korean.LIST_OF_NUCLEUS.index(ch)

    @staticmethod
    def index_of_coda(ch):
        return Korean.LIST_OF_CODA.index(ch)

    @staticmethod
    def compose(onset, nucleus, coda):
        onset_index = Korean.index_of_onset(onset)
        nucleus_index = Korean.index_of_nucleus(nucleus)
        coda_index = Korean.index_of_coda(coda)

        result = ((onset_index * len(Korean.LIST_OF_NUCLEUS)) + nucleus_index) * len(Korean.LIST_OF_CODA) + coda_index + Korean.OFFSET
        return chr(result)

    @staticmethod
    def decompose_onset_index(syllable):
        onset_index = (ord(syllable) - Korean.OFFSET) // (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))
        return onset_index

    @staticmethod
    def decompose_nucleus_index(syllable):
        onset_index = Korean.decompose_onset_index(syllable)
        nucleus_index = (ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA)))) // len(Korean.LIST_OF_CODA)
        return nucleus_index

    @staticmethod
    def decompose_coda_index(syllable):
        onset_index = Korean.decompose_onset_index(syllable)
        nucleus_index = Korean.decompose_nucleus_index(syllable)
        coda_index = ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))) - (nucleus_index * len(Korean.LIST_OF_CODA))
        return coda_index

    @staticmethod
    def decompose(syllable):
        onset_index = (ord(syllable) - Korean.OFFSET) // (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))
        nucleus_index = (ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA)))) // len(Korean.LIST_OF_CODA)
        coda_index = ord(syllable) - Korean.OFFSET - (onset_index * (len(Korean.LIST_OF_NUCLEUS) * len(Korean.LIST_OF_CODA))) - (nucleus_index * len(Korean.LIST_OF_CODA))
        return (Korean.LIST_OF_ONSET[onset_index], Korean.LIST_OF_NUCLEUS[nucleus_index], Korean.LIST_OF_CODA[coda_index])

댓글

이 블로그의 인기 게시물

react + typescript + leaflet