Skip to content

Instantly share code, notes, and snippets.

@allieus
Last active August 2, 2018 00:02
Show Gist options
  • Save allieus/0e8b609fe146ad63462ca81c70b2f5a2 to your computer and use it in GitHub Desktop.
Save allieus/0e8b609fe146ad63462ca81c70b2f5a2 to your computer and use it in GitHub Desktop.
BASE_CODE, CHOSUNG, JUNGSUNG = 0xac00, 0x024c, 0x1c
CHOSUNG_LIST = ('r', 'R', 's', 'e', 'E', 'f', 'a', 'q', 'Q', 't', 'T', 'd',
'w', 'W', 'c', 'z', 'x', 'v', 'g')
JUNGSUNG_LIST = ('k', 'o', 'i', 'O', 'j', 'p', 'u', 'P', 'h', 'hk', 'ho',
'hl', 'y', 'n', 'nj', 'np', 'nl', 'b', 'm', 'ml', 'l')
JONGSUNG_LIST = (' ', 'r', 'R', 'rt', 's', 'sw', 'sg', 'e', 'f', 'fr', 'fa',
'fq', 'ft', 'fx', 'fv', 'fg', 'a', 'q', 'qt', 't', 'T', 'd',
'w', 'c', 'z', 'x', 'v', 'g')
def korean_to_code(keyword):
ch_list = []
for ch in keyword:
ch_code = ord(ch) - BASE_CODE
idx1 = ch_code // CHOSUNG
idx2 = (ch_code - (CHOSUNG * idx1)) // JUNGSUNG
idx3 = ch_code - (CHOSUNG * idx1) - (JUNGSUNG * idx2)
ch_list.append(CHOSUNG_LIST[idx1] + JUNGSUNG_LIST[idx2] + \
JONGSUNG_LIST[idx3].strip())
return ''.join(ch_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment