Tokenize 文本为 NLP 过程
Tokenization splits 文本为 tokens (words, sentences, subwords). Learn 字, sentence, 和 subword tokenization 为 NLP 任务.
- 1
Tokenization splits 文本为 units 为过程. 字 tokenization splits 开 whitespace 和 punctuation. Sentence tokenization splits 开 sentence boundaries (. ! ?). Subword tokenization (BPE, WordPiece) splits 罕见 words 为常见 subwords — used 通过 LLMs. choice depends 开 NLP 任务.
- 2 字数统计器 →
统计 words and get basic word statistics. This gives a rough tokenization by splitting on whitespace. For more accurate tokenization, handle punctuation (don't split "don't" into "don" and "t"), contractions, and hyphenated words.
- 3 正则表达式测试器 →
Use regex 为字 tokenization: \b\w+\b matches 字 boundaries. 为 sentence tokenization: [^.!?]+[.!?] splits 开 sentence-ending punctuation. 为更多 accurate tokenization, use NLP 库 (NLTK, spaCy) 该 handles 边界情况 (abbreviations, ellipses, quotes).
常见问题
- 字 vs subword tokenization?
- 字 tokenization splits 开 whitespace — 简单但 produces 大 vocabulary 和 cannot 处理 unknown words. Subword tokenization (BPE, WordPiece) splits words 为常见 subword units (e.g., "playing" → "play" + "ing") — smaller vocabulary, handles unknown words. LLMs use subword tokenization.
- 如何 I 处理 punctuation 在 tokenization?
- 为字 tokenization, 决定是否 punctuation 是 separate 令牌或 attached 到字. "Hello, world!" → ["Hello", ",", "world", "!"] (separate) 或 ["Hello,", "world!"] (attached). 为 NLP, separate tokens 是标准. Use regex 或 NLP 库为 consistent handling.