อัลกอริทึม

NFKC (Compatibility Composition)

Normalization Form KC: แยกส่วนแบบ compatibility แล้วรวมแบบ canonical รวมอักขระที่มีลักษณะคล้ายกัน (fi→fi, ²→2, Ⅳ→IV) ใช้สำหรับการเปรียบเทียบตัวระบุ

· Updated

NFKC (Normalization Form KC — Compatibility Decomposition followed by Canonical Composition) is the most useful normalization form for search engines, programming language identifiers, and any system that needs to treat visually or semantically similar characters as equivalent.

The "K" in NFKC stands for "Kompatibility" — beyond the canonical decompositions that NFC handles, NFKC also folds compatibility characters: Unicode code points that exist for historical or typographic reasons but represent the same abstract character as something already encoded.

What Compatibility Folding Does

Compatibility decompositions cover hundreds of character categories:

Original After NFKC Category
(U+FB01) fi Ligature
² (U+00B2) 2 Superscript
(U+2103) °C Symbol
(U+FF21) A Fullwidth
(U+FF41) a Fullwidth
(U+2460) 1 Enclosed
(U+338F) km Compatibility CJK
(U+FF85) Halfwidth Katakana
import unicodedata

# Ligature folding
assert unicodedata.normalize("NFKC", "file") == "file"

# Fullwidth ASCII folding (common in CJK input)
fw = "A1B2"   # fullwidth
ascii_eq = unicodedata.normalize("NFKC", fw)
print(ascii_eq)  # "A1B2"

# Superscript folding
assert unicodedata.normalize("NFKC", "x²") == "x2"

# Combined with casefold for case-insensitive search
def normalize_for_search(s: str) -> str:
    return unicodedata.normalize("NFKC", s).casefold()

print(normalize_for_search("HELLOWORLD゙"))  # "helloworld゛" → further processing needed

NFKC in Standards

Python identifiers: Python 3 uses NFKC normalization for identifiers. That means MyClass (fullwidth) is valid and equivalent to MyClass.

PRECIS (RFC 8264): The successor to stringprep for usernames and passwords uses NFKC as a normalization step.

IDNA (Internationalized Domain Names): Domain name processing uses NFKC.

Password hashing: Many systems apply NFKC before hashing to ensure finance and finance hash the same way.

NFKC vs NFC: The Trade-off

NFKC loses information — ² and 2 are distinct characters with different semantic roles, but NFKC makes them identical. This is intentional for search and identifiers but wrong for document storage. Never use NFKC as your storage format if you need to preserve superscripts, ligatures, or enclosed numbers.

Quick Facts

Property Value
Full name Normalization Form Compatibility Composition
Algorithm NFKD first, then canonical composition
Python identifiers Normalized with NFKC (PEP 3131)
PRECIS framework RFC 8264 uses NFKC
Python unicodedata.normalize("NFKC", s)
Lossy? Yes — compatibility distinctions are discarded
Typical use Search normalization, identifier comparison, password processing
Handles accents? Yes (also decomposes/recomposes canonical characters)

คำศัพท์ที่เกี่ยวข้อง

เพิ่มเติมใน อัลกอริทึม

Case Folding

Mapping characters to a common case form for case-insensitive comparison. More comprehensive …

Grapheme Cluster Boundary

Rules (UAX#29) for determining where one user-perceived character ends and another begins. …

NFC (Canonical Composition)

Normalization Form C: แยกส่วนแล้วรวมใหม่แบบ canonical ได้รูปแบบที่สั้นที่สุด แนะนำสำหรับการจัดเก็บและแลกเปลี่ยนข้อมูล เป็นรูปแบบมาตรฐานของเว็บ

NFD (Canonical Decomposition)

Normalization Form D: แยกส่วนอย่างสมบูรณ์โดยไม่รวมใหม่ ใช้โดยระบบไฟล์ macOS HFS+ é (U+00E9) → e + …

NFKD (Compatibility Decomposition)

Normalization Form KD: แยกส่วนแบบ compatibility โดยไม่รวมใหม่ เป็นการ normalize ที่เข้มงวดที่สุด สูญเสียข้อมูลการจัดรูปแบบมากที่สุด

String Comparison

Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …

การทำให้เป็นมาตรฐาน

กระบวนการแปลงข้อความ Unicode เป็นรูปแบบ canonical มาตรฐาน มี 4 รูปแบบ: NFC (รวม), NFD (แยก), …

การยกเว้นการประกอบ

อักขระที่ถูกยกเว้นจากการรวมแบบ canonical (NFC) เพื่อป้องกันการแตกย่อยแบบ non-starter และรับประกันความเสถียรของอัลกอริทึม ระบุไว้ใน CompositionExclusions.txt

การแบ่งส่วนข้อความ

อัลกอริธึมสำหรับค้นหาขอบเขตในข้อความ: ขอบเขต grapheme cluster, คำ และประโยค มีความสำคัญสำหรับการเลื่อนเคอร์เซอร์ การเลือกข้อความ และการประมวลผลข้อความ

ขอบเขตคำ

ตำแหน่งระหว่างคำตามกฎการแบ่งคำของ Unicode ไม่ใช่แค่การแบ่งตามช่องว่าง แต่รองรับ CJK (ไม่มีช่องว่าง) คำย่อ และตัวเลขอย่างถูกต้อง