Segmentación de texto
Algoritmos para encontrar límites en el texto: límites de clúster de grafemas, palabras y oraciones. Fundamental para el movimiento del cursor, la selección de texto y el procesamiento de texto.
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 |
Términos relacionados
Más en Algoritmos
Algoritmo que determina el orden de visualización de los caracteres en texto …
Algoritmo estándar para comparar y ordenar cadenas Unicode mediante comparación multinivel: carácter …
Reglas para determinar dónde puede dividirse el texto para pasar a la …
Mapping characters to a common case form for case-insensitive comparison. More comprehensive …
Caracteres excluidos de la composición canónica (NFC) para evitar la descomposición no …
Rules (UAX#29) for determining where one user-perceived character ends and another begins. …
La posición entre oraciones según las reglas de Unicode. Más complejo que …
La posición entre palabras según las reglas de separación de palabras de …
Forma de Normalización C: descomponer y luego recomponer canónicamente, produciendo la forma …
Forma de Normalización D: descomposición total sin recomponer. Usada por el sistema …