유니코드 텍스트 분절
텍스트의 경계를 찾는 알고리즘: 문자소 클러스터, 단어, 문장 경계. 커서 이동, 텍스트 선택, 텍스트 처리에 필수적입니다.
Boundaries in Unicode Text
Text is not a flat sequence of code points — it has structure. Users think in terms of characters, words, and sentences. But Unicode code points do not map cleanly to these concepts. A single visible character (a grapheme cluster) can span multiple code points. A "word" means different things in English, Japanese, and Arabic. A sentence boundary after a period is ambiguous when periods also appear in abbreviations and numbers.
Unicode Text Segmentation (UAX #29) defines algorithms for finding grapheme cluster boundaries, word boundaries, and sentence boundaries. These algorithms are the foundation for correct cursor movement, text selection, word counting, and spell checking in any Unicode-aware application.
The Grapheme Cluster Problem
Python's len() function counts code points, not user-perceived characters:
# Emoji with ZWJ sequence: 1 visible character, 7 code points
family = "\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466"
print(len(family)) # 7 (code points)
# User sees: 👨👩👧👦 (one family emoji)
# Combining characters
cafe = "cafe\u0301" # e + combining acute = é
print(len(cafe)) # 5 (code points)
print(len("café")) # 4 (precomposed NFC)
# Both render as "café" — 4 user-perceived characters
# Flag emoji: 2 regional indicator symbols = 1 flag
flag = "\U0001F1FA\U0001F1F8" # 🇺🇸
print(len(flag)) # 2 (code points)
# User sees: 🇺🇸 (1 flag)
A grapheme cluster is the minimal unit a user thinks of as a single character. UAX #29 defines grapheme cluster boundary rules that handle: - Base + combining marks - Hangul syllable sequences (jamo combining rules) - Regional indicator pairs (flags) - Zero Width Joiner (ZWJ) sequences (family/profession emoji) - Extend characters (tags, emoji modifiers)
Using UAX #29 in Python
The grapheme package provides UAX #29-compliant grapheme cluster segmentation:
# pip install grapheme
import grapheme
family = "\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466"
print(grapheme.length(family)) # 1
print(list(grapheme.graphemes(family))) # ['👨👩👧👦']
text = "Hello, 世界! 🌍"
print(grapheme.length(text)) # 11 (user-perceived chars)
# Safe string slicing (by grapheme, not code point)
print(grapheme.slice(text, 0, 5)) # 'Hello'
For industrial-strength segmentation including word and sentence boundaries, use ICU via PyICU:
from icu import BreakIterator, Locale
text = "Don't stop. Dr. Smith arrived at 3.14 PM."
bi = BreakIterator.createSentenceInstance(Locale("en_US"))
bi.setText(text)
start = 0
for end in bi:
print(repr(text[start:end]))
start = end
# "Don't stop. " | "Dr. Smith arrived at 3.14 PM."
Quick Facts
| Property | Value |
|---|---|
| Specification | Unicode Standard Annex #29 (UAX #29) |
| Boundary types | Grapheme cluster, word, sentence |
Python len() |
Counts code points, not grapheme clusters |
| Python package | grapheme (pip install grapheme) |
| Full ICU support | PyICU — BreakIterator.createGraphemeInstance() etc. |
| ZWJ sequences | Zero Width Joiner (U+200D) joins emoji into single grapheme cluster |
| Regional indicators | Two regional indicator letters form a single flag grapheme cluster |
| Hangul | Jamo sequences (L + V + T) form a single syllable grapheme cluster |
관련 용어
알고리즘의 더 많은 용어
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. …
정규화 형식 C: 분해 후 정규 재합성하여 가장 짧은 형식을 생성합니다. 데이터 …
정규화 형식 D: 재합성 없이 완전히 분해합니다. macOS HFS+ 파일 시스템에서 사용됩니다. …
정규화 형식 KC: 호환 분해 후 정규 합성. 시각적으로 유사한 문자를 통합합니다(fi→fi, …
정규화 형식 KD: 재합성 없이 호환 분해. 가장 강력한 정규화 방식으로 서식 …
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
유니코드 단어 경계 규칙에 따라 결정된 단어 사이의 위치. 단순히 공백으로 분리하는 …
유니코드 규칙에 따른 문장 사이의 위치. 마침표로만 분리하는 것보다 복잡하며, 약어(Mr.), 생략 …
문자 양방향 범주와 명시적 방향 재정의를 사용하여 혼합 방향 텍스트(예: 영어 + …