Skip to content

Instantly share code, notes, and snippets.

@allieus
Created December 13, 2016 16:16
Show Gist options
  • Save allieus/d1d03c41a689300392319c09eb692f1a to your computer and use it in GitHub Desktop.
Save allieus/d1d03c41a689300392319c09eb692f1a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Hangul:
BASE_CODE = 44032
CHOSUNG = 588
JUNGSUNG = 28
# 초성 리스트. 00 ~ 18
CHOSUNG_LIST = [
'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ',
'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
# 중성 리스트. 00 ~ 20
JUNGSUNG_LIST = [
'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ',
'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
# 종성 리스트. 00 ~ 27 + 1(1개 없음)
JONGSUNG_LIST = [
None, 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ',
'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ',
'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
def split(self, ch):
base_code = ord(ch) - self.BASE_CODE
c1 = base_code // self.CHOSUNG
c2 = (base_code - (self.CHOSUNG * c1)) // self.JUNGSUNG
c3 = (base_code - (self.CHOSUNG * c1) - (self.JUNGSUNG * c2))
try:
return (self.CHOSUNG_LIST[c1], self.JUNGSUNG_LIST[c2], self.JONGSUNG_LIST[c3])
except IndexError:
return (None, None, None)
hangul = Hangul()
@allieus
Copy link
Author

allieus commented Dec 13, 2016

샘플코드

from hangul import hangul
result = hangul.split('가')
print(result)

실행 예

('ㄱ', 'ㅏ', None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment