Algorithmcore
Hangul syllable composition
Also called Hangul syllable decomposition.
Map each of the 11,172 precomposed Hangul syllables to its leading consonant, vowel and optional trailing consonant jamo by arithmetic on the code point, and back, with no data table.
- Time
- Θ(n) worst · best
- Space
- O(1) extra
- Where
- n = the number of code points of the input
- Model
- Word RAM
- Properties
- deterministiconlineiterative
- Evidence
- Proof sketchCode in 1 languages
The 11,172 precomposed Hangul syllables U+AC00..U+D7A3 are not listed in the decomposition data of Unicode. Their canonical decompositions into conjoining jamo follow from a formula instead. Each syllable is one leading consonant (19 choices), one vowel (21 choices) and zero or one trailing consonant (27 choices), and the syllables are numbered in this mixed radix from U+AC00. So decomposition, composition and even the character name are a few divisions and additions, as section 3.12 of the core specification defines them.
In plain words
Korean writing groups letters, called jamo, into square blocks, one block per syllable. Unicode has a code for every possible modern block, and also codes for the single letters.
The block codes are in a fixed order: first by the first consonant, then by the vowel, then by the final consonant or none. So the number of a block tells you its letters, in the same way that the number 347 tells you its hundreds, tens and ones. To split a block into letters you divide. To join letters into a block you multiply and add.
Because of this, programs need no list of the 11,172 blocks. They compute the parts when they normalize Korean text and when they print the names of the characters.
The problem
The constants of section 3.12 are , , , , , , , and . The modern leading consonants (L, D122) are U+1100..U+1112, the vowels (V, D125) U+1161..U+1175 and the trailing consonants (T, D128) U+11A8..U+11C2. itself is not a trailing consonant: the trailing index 0 means "no trailing consonant".
Decomposition. Given a precomposed syllable (D132), return its full canonical decomposition, or .
Composition. Given a sequence of code points, compose each into an LV syllable (D130) and each LV syllable followed by into an LVT syllable (D131), as the canonical composition algorithm (D117) does.
Name. Given , return its character name.
The algorithm
HANGUL-DECOMPOSE(s)- 1
i <- s - 0xAC00 - 2
l <- i div 588 - 3
v <- (i mod 588) div 28 - 4
t <- i mod 28 - 5
if t = 0 then return [0x1100 + l, 0x1161 + v] - 6
return [0x1100 + l, 0x1161 + v, 0x11A7 + t] HANGUL-COMPOSE(c[1..n])- 7
out <- empty list - 8
for each code point c of the input, in order do - 9
if the last code point of out is an L and c is a V then - 10
replace the last code point by 0xAC00 + ((last - 0x1100) * 21 + (c - 0x1161)) * 28 - 11
else if the last code point of out is a syllable with (last - 0xAC00) mod 28 = 0 and c is a T then - 12
replace the last code point by last + (c - 0x11A7) - 13
else append c to out - 14
return out HANGUL-NAME(s)- 15
parts <- HANGUL-DECOMPOSE(s) - 16
return "HANGUL SYLLABLE " followed by the Jamo_Short_Name of each part
Lines L2 to L4 read the three digits of the syllable index in the mixed radix . Line L10 builds the LV syllable, whose trailing digit is 0. Line L11 accepts a trailing consonant only after an LV syllable, so an LVT syllable never takes a second one. Line L13 appends every other code point.
The Unicode Character Database gives the decomposition mapping of a syllable in two parts: an LV syllable maps to , and an LVT syllable maps to . Applying the mapping twice gives the result of line L6. The short names in line L16 come from Jamo.txt, where U+110B HANGUL CHOSEONG IEUNG has the empty name.
Example
Decompose U+D55C 한. Line L1 gives . Then , the remainder is 4, so and . Line L6 returns U+1112 U+1161 U+11AB, the jamo ᄒ, ᅡ and ᆫ.
| syllable | index | jamo | name | |
|---|---|---|---|---|
| U+AC00 가 | 0 | (0, 0, 0) | 1100 1161 | HANGUL SYLLABLE GA |
| U+AC01 각 | 1 | (0, 0, 1) | 1100 1161 11A8 | HANGUL SYLLABLE GAG |
| U+C544 아 | 6,468 | (11, 0, 0) | 110B 1161 | HANGUL SYLLABLE A |
| U+D55C 한 | 10,588 | (18, 0, 4) | 1112 1161 11AB | HANGUL SYLLABLE HAN |
| U+D7A3 힣 | 11,171 | (18, 20, 27) | 1112 1175 11C2 | HANGUL SYLLABLE HIH |
Compose U+1112 U+1161 U+11AB. Line L13 appends U+1112. Then U+1161 is a vowel after a leading consonant, so line L10 writes , which is 하. Then U+11AB follows an LV syllable, so line L12 writes .
Correctness
The map is the mixed radix representation, so it is a bijection from the triples of digits onto , and lines L2 to L4 are its inverse. This gives decomposition and shows that the syllables and the sequences and of modern jamo correspond one to one. For composition, every jamo and every syllable has combining class 0, so it is a starter (D107), and by D115 a starter can only combine with the starter directly before it in the result. The primary composites (D114) with a Hangul first part are exactly and , so lines L9 to L13 do what D117 does. The proof in proofs/correctness.md gives the details and the proof of the names.
Proof: Hangul syllable arithmetic decomposes, composes and names the 11,172 syllables correctlyfull
Claim. (a) HANGUL-DECOMPOSE (lines L1 to L6) returns, for each precomposed Hangul syllable , its full canonical decomposition as defined in section 3.12.2 of the core specification, and the map from syllables to jamo sequences is a bijection onto the sequences and of modern jamo. (b) HANGUL-COMPOSE (lines L7 to L14) gives the same result as the canonical composition algorithm (D117) on any sequence in which no character other than the jamo and the syllables takes part in a composition with them. In particular, HANGUL-COMPOSE(HANGUL-DECOMPOSE) . (c) Lines L15 and L16 give the character name of . All three stop after a number of steps linear in the input length.
Setup. The constants are , , , , , , , and . A leading consonant is a code point with , a vowel is with , and a trailing consonant is with . The value itself is not a trailing consonant: stands for "no trailing consonant".
Lemma 1 (mixed radix). The map is a bijection from onto , and its inverse is , , .
Proof. This is the representation of in the mixed radix . For with digits in range, and , which splits in the same way into and , and because . Both sets have elements.
Proof of (a). Section 3.12.2 defines the decomposition of a syllable by these formulas: the syllable index is , and the jamo are , , and if . Lines L1 to L4 compute , and as in Lemma 1, and lines L5 and L6 return the jamo. The jamo have no decomposition mappings, so this decomposition is full. By Lemma 1, different syllables give different digit triples, and every or comes from the syllable with index . The standard gives the decomposition mapping in two parts, to and to . Applied twice, the second form gives the same three jamo, because has .
Proof of (b). Every jamo and every syllable has canonical combining class 0, so it is a starter. In D117, a character is blocked from a starter before it if any starter lies between them (D115). So a jamo or syllable can combine only with the character directly before it in the current result, and only if that character is a starter. The primary composites (D114) with a Hangul first part are exactly the two-part mappings of (a): and . No syllable is excluded from composition.
HANGUL-COMPOSE keeps this invariant: after each step, its output is the result of D117 on the prefix read so far. Line L9 tests whether the last output character is a leading consonant and a vowel, and line L10 replaces it by , the LV syllable of Lemma 1. Line L11 tests whether the last output character is an LV syllable (index divisible by ) and a trailing consonant, and line L12 adds . Otherwise line L13 appends , which is also what D117 does, since no primary composite exists for this pair and is a starter. After line L10, the new LV syllable is the last output character, so a following trailing consonant composes with it in the next step, as D117 requires. An LVT syllable has an index not divisible by , so it takes no second trailing consonant, and fails the test of line L11.
For a syllable with digits , the input , gives at line L10, and a trailing adds at line L12. By Lemma 1 the result is .
Proof of (c). Section 3.12.4 gives the name as "HANGUL SYLLABLE " followed by the Jamo_Short_Name values of , and , with the empty string for a missing . Line L15 gets the jamo by (a), and line L16 concatenates their short names from Jamo.txt. The short name of U+110B is empty, so a syllable that starts with it has a name that starts with the vowel.
Termination. Lines L1 to L6 and L15 to L16 do a constant number of operations. HANGUL-COMPOSE runs its loop once per input code point, and each pass does work.
Complexity
In the word-RAM model, decomposing a syllable and computing its name take time. Composition is one pass with work per code point, so it takes time and extra space besides the output.
| claim | value |
|---|---|
| decomposition of one syllable | |
| composition of code points | |
| data | 9 constants and 67 short names, instead of 11,172 table entries |
Properties
- Bijective. Each precomposed syllable has exactly one jamo sequence, and composition gives the syllable back. The test
test_every_syllablechecks all 11,172 syllables againstunicodedata.normalizeandunicodedata.name. - Same as NFC and NFD on Hangul. On text made of jamo and syllables, composition gives NFC and decomposition gives NFD. The test
test_random_text_against_nfc_and_nfdchecks 4,000 random strings. - No exclusions. No syllable is a composition exclusion, so NFC always composes a modern or into one syllable.
- Left to right. A composition depends only on the last code point of the result, so the algorithm runs on a stream.
Variants and improvements
- Inside normalization. The normalization algorithm calls the decomposition when it finds a syllable in the input and the composition step when it tries to combine a starter with the next character. Its data tables then need no Hangul entries.
- Old Hangul. The jamo outside the modern ranges, such as the archaic consonants U+1113..U+115F, U+A960..U+A97C and the extended vowels and trailing consonants of U+D7B0..U+D7C6 and U+D7CB..U+D7FB, do not compose. A sequence with them stays a sequence of jamo, and a font must lay it out as a syllable block (D133).
- Fillers. U+115F HANGUL CHOSEONG FILLER and U+1160 HANGUL JUNGSEONG FILLER (D124, D127) stand for a missing L or V in a syllable block. They do not compose.
When to use it
Use it inside any implementation of Unicode normalization, to avoid a table of 11,172 entries, and in tools that need the jamo of a syllable, such as Korean input methods, search by initial consonant, and spelling checkers. Use the name formula when a program must print character names without the full name list. For the visible boundaries of Korean text, use the rules of text segmentation, which treat a syllable block of jamo as one grapheme cluster even when it does not compose.
Pitfalls
- Treating U+11A7 as a trailing consonant. is one below the first trailing consonant, so can mean "none". Line L11 must reject U+11A7. The test
test_no_composition_caseschecks it. - A second trailing consonant. An LVT syllable followed by a does not compose, even when a double consonant exists for the pair. The same test checks it.
- V and T without L. A vowel that does not follow a leading consonant stays a jamo, and so does a T after it.
- Checking the range of the result. Line L11 must test that the last code point is a syllable, in U+AC00..U+D7A3. A code point above that range can have an index divisible by 28 too.
- The empty short name. The leading consonant IEUNG has the empty short name, so 아 is HANGUL SYLLABLE A. Code that expects three nonempty parts gets this wrong.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hangul-syllable-composition/. The authors are listed in prov/QKVWX9.jsonl.
"""Hangul syllable decomposition, composition and names, as in The Unicode Standard 16.0.0,
section 3.12 (Conjoining Jamo Behavior), with the constants of its "Common Constants".
The code does not call unicodedata. The tests compare it with unicodedata.normalize and
unicodedata.name.
"""
SBASE, LBASE, VBASE, TBASE = 0xAC00, 0x1100, 0x1161, 0x11A7
LCOUNT, VCOUNT, TCOUNT = 19, 21, 28
NCOUNT = VCOUNT * TCOUNT # 588
SCOUNT = LCOUNT * NCOUNT # 11172
# The data fields of Jamo-16.0.0.txt, https://www.unicode.org/Public/16.0.0/ucd/Jamo.txt
# (property Jamo_Short_Name), with the character names as comments. The file has these 67
# lines: 19 leading consonants, 21 vowels and 27 trailing consonants. U+110B has an empty
# short name.
JAMO_LINES = """\
1100; G # HANGUL CHOSEONG KIYEOK
1101; GG # HANGUL CHOSEONG SSANGKIYEOK
1102; N # HANGUL CHOSEONG NIEUN
1103; D # HANGUL CHOSEONG TIKEUT
1104; DD # HANGUL CHOSEONG SSANGTIKEUT
1105; R # HANGUL CHOSEONG RIEUL
1106; M # HANGUL CHOSEONG MIEUM
1107; B # HANGUL CHOSEONG PIEUP
1108; BB # HANGUL CHOSEONG SSANGPIEUP
1109; S # HANGUL CHOSEONG SIOS
110A; SS # HANGUL CHOSEONG SSANGSIOS
110B; # HANGUL CHOSEONG IEUNG
110C; J # HANGUL CHOSEONG CIEUC
110D; JJ # HANGUL CHOSEONG SSANGCIEUC
110E; C # HANGUL CHOSEONG CHIEUCH
110F; K # HANGUL CHOSEONG KHIEUKH
1110; T # HANGUL CHOSEONG THIEUTH
1111; P # HANGUL CHOSEONG PHIEUPH
1112; H # HANGUL CHOSEONG HIEUH
1161; A # HANGUL JUNGSEONG A
1162; AE # HANGUL JUNGSEONG AE
1163; YA # HANGUL JUNGSEONG YA
1164; YAE # HANGUL JUNGSEONG YAE
1165; EO # HANGUL JUNGSEONG EO
1166; E # HANGUL JUNGSEONG E
1167; YEO # HANGUL JUNGSEONG YEO
1168; YE # HANGUL JUNGSEONG YE
1169; O # HANGUL JUNGSEONG O
116A; WA # HANGUL JUNGSEONG WA
116B; WAE # HANGUL JUNGSEONG WAE
116C; OE # HANGUL JUNGSEONG OE
116D; YO # HANGUL JUNGSEONG YO
116E; U # HANGUL JUNGSEONG U
116F; WEO # HANGUL JUNGSEONG WEO
1170; WE # HANGUL JUNGSEONG WE
1171; WI # HANGUL JUNGSEONG WI
1172; YU # HANGUL JUNGSEONG YU
1173; EU # HANGUL JUNGSEONG EU
1174; YI # HANGUL JUNGSEONG YI
1175; I # HANGUL JUNGSEONG I
11A8; G # HANGUL JONGSEONG KIYEOK
11A9; GG # HANGUL JONGSEONG SSANGKIYEOK
11AA; GS # HANGUL JONGSEONG KIYEOK-SIOS
11AB; N # HANGUL JONGSEONG NIEUN
11AC; NJ # HANGUL JONGSEONG NIEUN-CIEUC
11AD; NH # HANGUL JONGSEONG NIEUN-HIEUH
11AE; D # HANGUL JONGSEONG TIKEUT
11AF; L # HANGUL JONGSEONG RIEUL
11B0; LG # HANGUL JONGSEONG RIEUL-KIYEOK
11B1; LM # HANGUL JONGSEONG RIEUL-MIEUM
11B2; LB # HANGUL JONGSEONG RIEUL-PIEUP
11B3; LS # HANGUL JONGSEONG RIEUL-SIOS
11B4; LT # HANGUL JONGSEONG RIEUL-THIEUTH
11B5; LP # HANGUL JONGSEONG RIEUL-PHIEUPH
11B6; LH # HANGUL JONGSEONG RIEUL-HIEUH
11B7; M # HANGUL JONGSEONG MIEUM
11B8; B # HANGUL JONGSEONG PIEUP
11B9; BS # HANGUL JONGSEONG PIEUP-SIOS
11BA; S # HANGUL JONGSEONG SIOS
11BB; SS # HANGUL JONGSEONG SSANGSIOS
11BC; NG # HANGUL JONGSEONG IEUNG
11BD; J # HANGUL JONGSEONG CIEUC
11BE; C # HANGUL JONGSEONG CHIEUCH
11BF; K # HANGUL JONGSEONG KHIEUKH
11C0; T # HANGUL JONGSEONG THIEUTH
11C1; P # HANGUL JONGSEONG PHIEUPH
11C2; H # HANGUL JONGSEONG HIEUH
"""
def parse_jamo(text: str) -> dict[int, str]:
out = {}
for line in text.splitlines():
fields = line.split("#", 1)[0].split(";")
out[int(fields[0], 16)] = fields[1].strip()
return out
JAMO_SHORT_NAME = parse_jamo(JAMO_LINES)
def is_syllable(c: int) -> bool:
return SBASE <= c < SBASE + SCOUNT
def is_l(c: int) -> bool:
return LBASE <= c < LBASE + LCOUNT
def is_v(c: int) -> bool:
return VBASE <= c < VBASE + VCOUNT
def is_t(c: int) -> bool:
return TBASE < c < TBASE + TCOUNT # TBASE itself is not a trailing consonant
def decompose_syllable(s: int) -> tuple[int, ...]:
"""Full canonical decomposition of a precomposed syllable: (L, V) or (L, V, T)."""
if not is_syllable(s):
raise ValueError(f"U+{s:04X} is not a precomposed Hangul syllable")
sindex = s - SBASE # @line L1
lindex = sindex // NCOUNT # @line L2
vindex = (sindex % NCOUNT) // TCOUNT # @line L3
tindex = sindex % TCOUNT # @line L4
if tindex == 0: # @line L5
return (LBASE + lindex, VBASE + vindex)
return (LBASE + lindex, VBASE + vindex, TBASE + tindex) # @line L6
def decomposition_mapping(s: int) -> tuple[int, int]:
"""The two-part canonical decomposition mapping: LV to (L, V), LVT to (LV, T)."""
sindex = s - SBASE
tindex = sindex % TCOUNT
if tindex == 0:
return (LBASE + sindex // NCOUNT, VBASE + (sindex % NCOUNT) // TCOUNT)
return (s - tindex, TBASE + tindex)
def decompose(text: list[int]) -> list[int]:
"""Replace every precomposed syllable of text by its jamo; keep the rest."""
out: list[int] = []
for c in text:
out.extend(decompose_syllable(c) if is_syllable(c) else (c,))
return out
def compose(text: list[int]) -> list[int]:
"""Compose L V into LV and LV T into LVT, from left to right."""
out: list[int] = [] # @line L7
for c in text: # @line L8
last = out[-1] if out else -1
if is_l(last) and is_v(c): # @line L9
out[-1] = SBASE + ((last - LBASE) * VCOUNT + (c - VBASE)) * TCOUNT # @line L10
elif is_syllable(last) and (last - SBASE) % TCOUNT == 0 and is_t(c): # @line L11
out[-1] = last + (c - TBASE) # @line L12
else:
out.append(c) # @line L13
return out # @line L14
def syllable_name(s: int) -> str:
"""The character name of a precomposed syllable, from the Jamo short names."""
parts = decompose_syllable(s) # @line L15
return "HANGUL SYLLABLE " + "".join(JAMO_SHORT_NAME[j] for j in parts) # @line L16
test_hangul_syllable_composition.py 68 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hangul-syllable-composition/. The authors are listed in prov/QKVWX9.jsonl.
# Oracles: unicodedata.normalize and unicodedata.name. Written against Python 3.14 with
# unicodedata.unidata_version 16.0.0. The 11172 syllables and the modern jamo are in
# Unicode 2.0 and later, so they are assigned in 3.2, and the stability policy fixes their
# decompositions.
import random
import unicodedata
from hangul_syllable_composition import (
JAMO_SHORT_NAME, NCOUNT, SBASE, SCOUNT, TBASE, compose, decompose, decompose_syllable,
decomposition_mapping, syllable_name)
def cps(s: str) -> list[int]:
return [ord(ch) for ch in s]
def text(c: list[int]) -> str:
return "".join(map(chr, c))
def test_constants_and_check_values() -> None:
assert NCOUNT == 588 and SCOUNT == 11172
assert decompose_syllable(0xD55C) == (0x1112, 0x1161, 0x11AB)
assert syllable_name(0xD55C) == "HANGUL SYLLABLE HAN"
assert decompose_syllable(0xAC01) == (0x1100, 0x1161, 0x11A8)
assert decompose_syllable(0xAC00) == (0x1100, 0x1161)
assert decomposition_mapping(0xAC01) == (0xAC00, 0x11A8)
assert len(JAMO_SHORT_NAME) == 67
def test_every_syllable() -> None:
for s in range(SBASE, SBASE + SCOUNT):
jamo = list(decompose_syllable(s))
assert compose(jamo) == [s]
assert jamo == cps(unicodedata.normalize("NFD", chr(s)))
assert syllable_name(s) == unicodedata.name(chr(s))
first, second = decomposition_mapping(s)
if first >= SBASE: # LVT maps to LV and T; the LV part decomposes again
assert list(decomposition_mapping(first)) + [second] == jamo
else:
assert [first, second] == jamo
def test_all_syllables_are_in_unicode_3_2() -> None:
assert all(unicodedata.ucd_3_2_0.category(chr(s)) == "Lo" for s in range(SBASE, SBASE + SCOUNT, 97))
def test_random_text_against_nfc_and_nfd() -> None:
rng = random.Random(1)
pool = (list(range(0x1100, 0x1113)) + list(range(0x1161, 0x1176)) + list(range(0x11A8, 0x11C3))
+ [0x41, 0x20, SBASE, SBASE + 1, 0xD55C, 0xB2E4])
for _ in range(4000):
s = text([rng.choice(pool) for _ in range(rng.randrange(1, 10))])
assert text(compose(cps(s))) == unicodedata.normalize("NFC", s)
assert text(decompose(cps(s))) == unicodedata.normalize("NFD", s)
def test_no_composition_cases() -> None:
# An LVT syllable takes no second trailing consonant, V T without L stays apart, and
# TBASE (U+11A7) is not a trailing consonant.
assert compose([0xAC01, 0x11A8]) == [0xAC01, 0x11A8]
assert compose([0x1161, 0x11A8]) == [0x1161, 0x11A8]
assert compose([SBASE, TBASE]) == [SBASE, TBASE]
assert compose([0x1100, 0x1100, 0x1161, 0x11A8]) == [0x1100, 0xAC01]
History
Unicode 1.1 had 6,656 Hangul syllables in another place of the codespace. Unicode 2.0 (1996) removed them and encoded all 11,172 modern syllables at U+AC00..U+D7A3 in the order that the formula of this page uses. This change is the only time Unicode moved characters, and the stability policies of later versions forbid it. The decomposition and composition formulas have been part of the standard since then, and section 3.12 gives them with sample code.
References
- The Unicode Consortium, The Unicode Standard, Version 16.0.0, 2024, Chapter 3, Section 3.12 Conjoining Jamo Behavior: 3.12.1 Definitions (D122 to D134), 3.12.2 Hangul Syllable Decomposition, 3.12.3 Hangul Syllable Composition, 3.12.4 Hangul Syllable Name Generation, 3.12.5 Sample Code for Hangul Algorithms
- The Unicode Standard, Version 16.0.0, core specification, chapter 3 (online), Section 3.12 and Section 3.11.6 Canonical Composition Algorithm (D114 to D117)
- Unicode Character Database 16.0.0, Jamo.txt, Jamo_Short_Name values of the 67 modern jamo
- Mark Davis and Ken Whistler, Unicode Standard Annex 15, Unicode Normalization Forms, revision 56, Section 1.3 (Canonical composition) and the Hangul part of the decomposition data
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag QKVWX9 Edit this page History Report an error Markdown