การทำให้เป็นมาตรฐาน
กระบวนการแปลงข้อความ Unicode เป็นรูปแบบ canonical มาตรฐาน มี 4 รูปแบบ: NFC (รวม), NFD (แยก), NFKC (compatibility รวม), NFKD (compatibility แยก)
Why the Same Text Can Look Identical But Be Different
Consider the letter é. You can encode it two ways: as a single precomposed character é (U+00E9, LATIN SMALL LETTER E WITH ACUTE) or as the sequence e (U+0065) followed by the combining acute accent ´ (U+0301). Both render identically, both are valid Unicode — but they are different byte sequences and will not compare as equal with a naive string comparison.
This is the core problem Unicode Normalization solves. Without normalization, the same word typed on macOS (which prefers decomposed forms) can fail to match the same word stored on a Linux system (which may prefer composed forms). Searching, sorting, deduplication, and hashing all break when you have silent encoding differences.
The Four Normal Forms
Unicode defines four normalization forms, each serving different needs:
| Form | Full Name | What it does |
|---|---|---|
| NFC | Canonical Decomposition + Canonical Composition | Decompose then recompose — most compact canonical form |
| NFD | Canonical Decomposition | Fully decompose to base + combining marks |
| NFKC | Compatibility Decomposition + Canonical Composition | Like NFC but also folds compatibility variants |
| NFKD | Compatibility Decomposition | The most aggressive decomposition |
The "K" variants additionally fold compatibility characters — characters that are semantically equivalent but visually or historically distinct, such as fi (fi ligature, U+FB01) → fi, or ² (superscript 2, U+00B2) → 2.
Using Normalization in Python
Python's unicodedata module provides normalization through a single function:
import unicodedata
text = "caf\u00e9" # café with precomposed é (NFC)
nfd = unicodedata.normalize("NFD", text)
print(len(text)) # 4
print(len(nfd)) # 5 (e + combining acute)
# Roundtrip
assert unicodedata.normalize("NFC", nfd) == text
# Checking which form a string is already in
print(unicodedata.is_normalized("NFC", text)) # True
print(unicodedata.is_normalized("NFD", text)) # False
A safe comparison pattern for user-facing text:
def normalize_for_comparison(s: str) -> str:
return unicodedata.normalize("NFC", s.casefold())
Quick Facts
| Property | Value |
|---|---|
| Unicode standard | The Unicode Standard, Section 3.11 |
| Python module | unicodedata.normalize(form, string) |
| Valid form names | "NFC", "NFD", "NFKC", "NFKD" |
| Web standard | W3C recommends NFC for all web content |
| macOS file system | HFS+ stores filenames in NFD |
| Idempotency | Applying normalization twice gives the same result |
| Related concept | Canonical equivalence, compatibility equivalence |
คำศัพท์ที่เกี่ยวข้อง
เพิ่มเติมใน อัลกอริทึม
Mapping characters to a common case form for case-insensitive comparison. More comprehensive …
Rules (UAX#29) for determining where one user-perceived character ends and another begins. …
Normalization Form C: แยกส่วนแล้วรวมใหม่แบบ canonical ได้รูปแบบที่สั้นที่สุด แนะนำสำหรับการจัดเก็บและแลกเปลี่ยนข้อมูล เป็นรูปแบบมาตรฐานของเว็บ
Normalization Form D: แยกส่วนอย่างสมบูรณ์โดยไม่รวมใหม่ ใช้โดยระบบไฟล์ macOS HFS+ é (U+00E9) → e + …
Normalization Form KC: แยกส่วนแบบ compatibility แล้วรวมแบบ canonical รวมอักขระที่มีลักษณะคล้ายกัน (fi→fi, ²→2, Ⅳ→IV) ใช้สำหรับการเปรียบเทียบตัวระบุ
Normalization Form KD: แยกส่วนแบบ compatibility โดยไม่รวมใหม่ เป็นการ normalize ที่เข้มงวดที่สุด สูญเสียข้อมูลการจัดรูปแบบมากที่สุด
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
อักขระที่ถูกยกเว้นจากการรวมแบบ canonical (NFC) เพื่อป้องกันการแตกย่อยแบบ non-starter และรับประกันความเสถียรของอัลกอริทึม ระบุไว้ใน CompositionExclusions.txt
อัลกอริธึมสำหรับค้นหาขอบเขตในข้อความ: ขอบเขต grapheme cluster, คำ และประโยค มีความสำคัญสำหรับการเลื่อนเคอร์เซอร์ การเลือกข้อความ และการประมวลผลข้อความ
ตำแหน่งระหว่างคำตามกฎการแบ่งคำของ Unicode ไม่ใช่แค่การแบ่งตามช่องว่าง แต่รองรับ CJK (ไม่มีช่องว่าง) คำย่อ และตัวเลขอย่างถูกต้อง