Algorithm Search⌘K

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
Properties
deterministiconlineiterative

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 SB=AC00S_B = \mathtt{AC00}, LB=1100L_B = \mathtt{1100}, VB=1161V_B = \mathtt{1161}, TB=11A7T_B = \mathtt{11A7}, LC=19L_C = 19, VC=21V_C = 21, TC=28T_C = 28, NC=VCTC=588N_C = V_C T_C = 588 and SC=LCNC=11,172S_C = L_C N_C = 11{,}172. 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. TBT_B itself is not a trailing consonant: the trailing index 0 means "no trailing consonant".

Decomposition. Given a precomposed syllable ss (D132), return its full canonical decomposition, L VL\,V or L V TL\,V\,T.

Composition. Given a sequence of code points, compose each L VL\,V into an LV syllable (D130) and each LV syllable followed by TT into an LVT syllable (D131), as the canonical composition algorithm (D117) does.

Name. Given ss, return its character name.

The algorithm

  1. HANGUL-DECOMPOSE(s)
  2. 1i <- s - 0xAC00
  3. 2l <- i div 588
  4. 3v <- (i mod 588) div 28
  5. 4t <- i mod 28
  6. 5if t = 0 then return [0x1100 + l, 0x1161 + v]
  7. 6return [0x1100 + l, 0x1161 + v, 0x11A7 + t]
  8. HANGUL-COMPOSE(c[1..n])
  9. 7out <- empty list
  10. 8for each code point c of the input, in order do
  11. 9if the last code point of out is an L and c is a V then
  12. 10replace the last code point by 0xAC00 + ((last - 0x1100) * 21 + (c - 0x1161)) * 28
  13. 11else if the last code point of out is a syllable with (last - 0xAC00) mod 28 = 0 and c is a T then
  14. 12replace the last code point by last + (c - 0x11A7)
  15. 13else append c to out
  16. 14return out
  17. HANGUL-NAME(s)
  18. 15parts <- HANGUL-DECOMPOSE(s)
  19. 16return "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 (19,21,28)(19, 21, 28). 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 L,VL, V, and an LVT syllable maps to LV,TLV, T. 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 i=D55C−AC00=10,588i = \mathtt{D55C} - \mathtt{AC00} = 10{,}588. Then l=10,588div⁡588=18l = 10{,}588 \operatorname{div} 588 = 18, the remainder is 4, so v=0v = 0 and t=4t = 4. Line L6 returns U+1112 U+1161 U+11AB, the jamo ᄒ, ᅡ and ᆫ.

syllable index (l,v,t)(l, v, t) 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 AC00+(18⋅21+0)⋅28=D558\mathtt{AC00} + (18 \cdot 21 + 0) \cdot 28 = \mathtt{D558}, which is 하. Then U+11AB follows an LV syllable, so line L12 writes D558+4=D55C\mathtt{D558} + 4 = \mathtt{D55C}.

Correctness

The map (l,v,t)↦(21l+v)⋅28+t(l, v, t) \mapsto (21 l + v) \cdot 28 + t is the mixed radix representation, so it is a bijection from the triples of digits onto 0..11,1710..11{,}171, and lines L2 to L4 are its inverse. This gives decomposition and shows that the syllables and the sequences L VL\,V and L V TL\,V\,T 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 L+V→LVL + V \to LV and LV+T→LVTLV + T \to LVT, 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 ss, 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 L VL\,V and L V TL\,V\,T 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(s)(s)) =s= s. (c) Lines L15 and L16 give the character name of ss. All three stop after a number of steps linear in the input length.

Setup. The constants are SB=AC00S_B = \mathtt{AC00}, LB=1100L_B = \mathtt{1100}, VB=1161V_B = \mathtt{1161}, TB=11A7T_B = \mathtt{11A7}, LC=19L_C = 19, VC=21V_C = 21, TC=28T_C = 28, NC=VCTC=588N_C = V_C T_C = 588 and SC=LCNC=11172S_C = L_C N_C = 11172. A leading consonant is a code point LB+lL_B + l with 0≤l<190 \le l < 19, a vowel is VB+vV_B + v with 0≤v<210 \le v < 21, and a trailing consonant is TB+tT_B + t with 1≤t<281 \le t < 28. The value TBT_B itself is not a trailing consonant: t=0t = 0 stands for "no trailing consonant".

Lemma 1 (mixed radix). The map (l,v,t)↦(lVC+v)TC+t(l, v, t) \mapsto (l V_C + v) T_C + t is a bijection from {0…18}×{0…20}×{0…27}\{0 \ldots 18\} \times \{0 \ldots 20\} \times \{0 \ldots 27\} onto {0…11171}\{0 \ldots 11171\}, and its inverse is l=xdiv⁡NCl = x \operatorname{div} N_C, v=(x mod NC)div⁡TCv = (x \bmod N_C) \operatorname{div} T_C, t=x mod TCt = x \bmod T_C.

Proof. This is the representation of xx in the mixed radix (19,21,28)(19, 21, 28). For x=(lVC+v)TC+tx = (l V_C + v) T_C + t with digits in range, x mod TC=tx \bmod T_C = t and xdiv⁡TC=lVC+vx \operatorname{div} T_C = l V_C + v, which splits in the same way into vv and ll, and (x mod NC)div⁡TC=v(x \bmod N_C) \operatorname{div} T_C = v because x mod NC=vTC+tx \bmod N_C = v T_C + t. Both sets have 19⋅21⋅28=1117219 \cdot 21 \cdot 28 = 11172 elements. □\square

Proof of (a). Section 3.12.2 defines the decomposition of a syllable by these formulas: the syllable index is s−SBs - S_B, and the jamo are LB+lL_B + l, VB+vV_B + v, and TB+tT_B + t if t>0t > 0. Lines L1 to L4 compute ll, vv and tt 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 L VL\,V or L V TL\,V\,T comes from the syllable with index (lVC+v)TC+t(l V_C + v) T_C + t. The standard gives the decomposition mapping in two parts, LVLV to L,VL, V and LVTLVT to LV,TLV, T. Applied twice, the second form gives the same three jamo, because LV=SB+(lVC+v)TCLV = S_B + (l V_C + v) T_C has t=0t = 0. □\square

Proof of (b). Every jamo and every syllable has canonical combining class 0, so it is a starter. In D117, a character CC is blocked from a starter SS 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): L+V→LVL + V \to LV and LV+T→LVTLV + T \to LVT. 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 cc a vowel, and line L10 replaces it by SB+(lVC+v)TCS_B + (l V_C + v) T_C, the LV syllable of Lemma 1. Line L11 tests whether the last output character is an LV syllable (index divisible by TCT_C) and cc a trailing consonant, and line L12 adds tt. Otherwise line L13 appends cc, which is also what D117 does, since no primary composite exists for this pair and cc 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 TCT_C, so it takes no second trailing consonant, and TBT_B fails the test of line L11.

For a syllable ss with digits (l,v,t)(l, v, t), the input LB+lL_B + l, VB+vV_B + v gives SB+(lVC+v)TCS_B + (l V_C + v) T_C at line L10, and a trailing TB+tT_B + t adds tt at line L12. By Lemma 1 the result is ss. □\square

Proof of (c). Section 3.12.4 gives the name as "HANGUL SYLLABLE " followed by the Jamo_Short_Name values of LL, VV and TT, with the empty string for a missing TT. 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. □\square

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 O(1)O(1) work. ■\blacksquare

Complexity

In the word-RAM model, decomposing a syllable and computing its name take O(1)O(1) time. Composition is one pass with O(1)O(1) work per code point, so it takes Θ(n)\Theta(n) time and O(1)O(1) extra space besides the output.

claim value
decomposition of one syllable O(1)O(1)
composition of nn code points Θ(n)\Theta(n)
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_syllable checks all 11,172 syllables against unicodedata.normalize and unicodedata.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_nfd checks 4,000 random strings.
  • No exclusions. No syllable is a composition exclusion, so NFC always composes a modern L VL\,V or L V TL\,V\,T 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. TBT_B is one below the first trailing consonant, so t=0t = 0 can mean "none". Line L11 must reject U+11A7. The test test_no_composition_cases checks it.
  • A second trailing consonant. An LVT syllable followed by a TT 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.

hangul_syllable_composition.pyI0RawGitHub
# 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

  1. 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
  2. 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)
  3. Unicode Character Database 16.0.0, Jamo.txt, Jamo_Short_Name values of the 67 modern jamo
  4. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • History with a source
  • a scene preset for the example