Hangul.py import 해서 한글 초성,중성,종성으로 쪼게거나 초성, 중성, 종성으로 한글 만들기
test_hangul = Hangul() // 객체생성
cho, joong, jong = test_hangul.decompose(each) // decompose 이용해 한글 초성,중성,종성 저장
test_hangul.compose(cho, joong, jong) // 초성, 중성, 종성으로 한글 만들기
https://greenbnn98.tistory.com/73
[Python] 한글 유니코드
목차 uhangul.py > hcompose.py > key_hangul.py > dcompose.py 초,중,종 담기 초,중,종 입력>글자 한글>16진법 한글>16진법>초,중,종 찾기 자음 : 19개 ㄱ - ㅎ : 0x1100 ~ 0x1112 모음 : 21개 ㅏ - ㅣ : 0x1161 ~ 0x1175 받침 : 28
greenbnn98.tistory.com
우리는 한글 유니코드 에서 Hangul.py 와 hcompose.py 를 만들었다.
각각 초성,중성,종성을 리스트로 저장해 놓은 파일이고
#uhangul.py
#coding:utf-8
first_letters=list('ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ')
middle_letters='ㅏ,ㅐ,ㅑ,ㅒ,ㅓ,ㅔ,ㅕ,ㅖ,ㅗ,ㅘ,ㅙ,ㅚ,ㅛ,ㅜ,ㅝ,ㅞ,ㅟ,ㅠ,ㅡ,ㅢ,ㅣ'.split(',')
final_letters=',ㄱ,ㄲ,ㄳ,ㄴ,ㄵ,ㄶ,ㄷ,ㄹ,ㄺ,ㄻ,ㄼ,ㄽ,ㄾ,ㄿ,ㅀ,ㅁ,ㅂ,ㅄ,ㅅ,ㅆ,ㅐ,ㅈ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ'.split(',')
초성, 중성, 종성을 각각 입력하면 합쳐주는 파일이었다.
#hcompose.py
#coding:utf-8
from uhangul import first_letters, middle_letters, final_letters
def compose(chosung, joongsung, jongsung):
first_index = first_letters.index(chosung)
middle_index = middle_letters. index(joongsung)
final_index = final_letters.index(jongsung)
return chr(0xAC00 + (first_index * 21 * 28) + (middle_index * 28) + final_index)
print(compose('ㄱ', 'ㅏ', 'ㄱ'))
이제 이것을 조금 사용하기 편하도록 Class 화 하고 함수를 합쳐보겠다.
#Hangul.py
#coding:utf-8
class Hangul(str):
first_letters=list('ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ')
middle_letters='ㅏ,ㅐ,ㅑ,ㅒ,ㅓ,ㅔ,ㅕ,ㅖ,ㅗ,ㅘ,ㅙ,ㅚ,ㅛ,ㅜ,ㅝ,ㅞ,ㅟ,ㅠ,ㅡ,ㅢ,ㅣ'.split(',')
final_letters=',ㄱ,ㄲ,ㄳ,ㄴ,ㄵ,ㄶ,ㄷ,ㄹ,ㄺ,ㄻ,ㄼ,ㄽ,ㄾ,ㄿ,ㅀ,ㅁ,ㅂ,ㅄ,ㅅ,ㅆ,ㅐ,ㅈ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ'. split(',')
# OxAC00+ (first_index * 21 * 28) + (middle_index * 21) + final_index
def decompose(self, hangul_sylla): #reverse way of composing
code = ord(hangul_sylla) - 0xAC00 # section of indices
final_index = code % 28 # remainder for final
code = code // 28 #for the next
middle_index = code % 21 #remainder for middle
code = code // 21 # for the next
first_index = code # all firsts are displayed in the syllable, so. no divided by num of firsts
return (self.first_letters[first_index], self.middle_letters[middle_index], self. final_letters[final_index])
def compose(self, chosung, joongsung, jongsung):
first_index = self.first_letters.index(chosung)
middle_index = self.middle_letters. index(joongsung)
final_index = self.final_letters.index(jongsung)
return chr(0xAC00 + (first_index * 21 * 28) + (middle_index * 28) + final_index)
if __name__ == '__main__':
test_hangul = Hangul()
for each in list('한글사랑'):
#print(test_hangul.decompose(each))
cho, joong, jong = test_hangul.decompose(each)
print(cho, joong, jong)
print(test_hangul.compose(cho, joong, jong))
우선 Hangul 이라는 객체에 우리가 쓸 초성, 중성, 종성을 저장해놓는다.
이후 decompose 라는 한글 한 글자를 입력하면 모두 쪼게서 각 자음, 모음으로 반환을 해주는 함수를 넣어준다.
마지막으로 compose 라는 자음, 모음 을 입력하면 다시 한 글자로 합쳐주는 함수를 넣어준다.
테스트를 하기위해 '한글사랑' 을 한 글자씩 보내 decompose 로 쪼게고, 다시 합쳐본다.
'수업정리 > Fundamental' 카테고리의 다른 글
Python basic 6 - os.path, glob(), str, file.read() readlines() write() writelines() (0) | 2023.04.19 |
---|---|
Python basic 5 - object(), super(), repr(), grob() (0) | 2023.04.14 |
Python basic 4 - map(), import, zip() 한글을 입력하면 유니코드로 (0) | 2023.04.12 |
Python basic 3 - 파일 open, write2file.py (0) | 2023.04.05 |
[Python] 한글 유니코드 2 - 한글을 byte 데이터로 만들기, byte(), bytearray() (0) | 2023.04.05 |