Skip to content
Search

Hangul-toolkit

lastmod 2025-04-14created 2025-04-094 edits

A toolkit for decomposing/composing Hangul jamo (Apache 2.0)

Supports Hangul jamo decomposition, composition (automata), attaching josa (postpositional particles), decomposing/composing initial/medial/final consonants and vowels, checking whether text is Hangul/Hanja/English, and more.

base code forked from Hangulpy 1

Motivation

hgtk is a jamo splitter that can be used for Korean character embedding.

English allows character embedding based on single letters (lowercase and uppercase -> 52 characters), but

for Korean, character embedding by individual syllable (11172 characters) requires far too much computation.

So when doing Korean character embedding, text is sometimes split into jamo instead of syllables.

Install

pip install hgtk

Samples

Letter

Decompose character:

>>> hgtk.letter.decompose('감')
('ㄱ', 'ㅏ', 'ㅁ')

Compose character:

>>> hgtk.letter.compose('ㄱ', 'ㅏ', 'ㅁ')
'감'

Text

Decompose text:

>>> hgtk.text.decompose('학교종이 땡땡땡! hello world 1234567890 ㅋㅋ!')
'ㅎㅏㄱᴥㄱㅛᴥㅈㅗㅇᴥㅇㅣᴥ ㄸㅐㅇᴥㄸㅐㅇᴥㄸㅐㅇᴥ! hello world 1234567890 ㅋᴥㅋᴥ!'

The default composition-complete marker is ᴥ, and it can be changed with the compose_code option as shown below.

>>> hgtk.text.decompose('학교종이 땡땡땡! hello world 1234567890 ㅋㅋ!', compose_code='/')
'ㅎㅏㄱ/ㄱㅛ/ㅈㅗㅇ/ㅇㅣ/ㄸㅐㅇ/ㄸㅐㅇ/ㄸㅐㅇ/! hello world 1234567890 ㅋ/ㅋ/!'

Compose text (Automata):

>>> hgtk.text.compose('ㅎㅏㄱᴥㄱㅛᴥㅈㅗㅇᴥㅇㅣᴥ ㄸㅐㅇᴥㄸㅐㅇᴥㄸㅐㅇᴥ! hello world 1234567890 ㅋᴥㅋᴥ!')
'학교종이 땡땡땡! hello world 1234567890 ㅋㅋ!'

See also

Favorite site

References

author Zer0 sitemap