liveBook · Manning

Subword tokenization

As explained in Natural Language Processing in Action, Second Edition

A subword is a word fragment, character sequence, or complete word treated as a unit in subword-based language modeling.

Subword tokenization treats character sequences, word fragments, syllables, or complete words as language-modeling units. It occupies a useful middle ground: unlike word tokenization, it can represent unfamiliar words; unlike character tokenization, it can preserve frequent, meaningful chunks. Subword vocabularies therefore balance sequence length, vocabulary size, and generalization. section 2.2 section 2.2.1

A subword tokenizer builds units from neighboring characters rather than relying only on spaces or punctuation:

  • Count character n-grams and identify sequences that frequently occur together, such as common parts of words or compounds. section 2.2.1
  • Keep individual characters in the vocabulary so unfamiliar words can be decomposed, provided they contain no unseen characters. section 2.2.1
  • Use frequent whole words or word pieces when their statistics support them, while retaining smaller pieces for rare forms. section 2.2 section 2.2.1

Inspecting character n-grams in Python

A character analyzer makes the intermediate vocabulary visible: CountVectorizer(ngram_range=(1, 2), analyzer='char') learns single characters and adjacent pairs from texts. Transforming the texts into a frequency table shows which fragments dominate; filtering by length then isolates two-character candidates such as “en”, “an”, and “uc”. section 2.2.1

The same idea supports embeddings. fastText represents n-character grams—including words, misspellings, partial words, and single characters—and can compose a rare or unseen word from familiar pieces. For example, a scarce “Superwoman” can draw on vectors for “super” and “woman,” reducing reliance on a generic out-of-vocabulary vector. This improves coverage, but pretrained model quality still varies with the language and available training corpus. section 6.4.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains token construction, character n-gram statistics, vocabulary coverage, and embedding composition, but does not specify a complete tokenizer algorithm.
Examples
Demonstrates character bigram counts with CountVectorizer and composes fastText representations for rare or unseen words.
What it assumes
Assumes basic NLP pipeline and token concepts, while introducing character units, subwords, vocabulary statistics, and OOV behavior.
What you can do after
Apply it via CountVectorizer or fastText to inspect character fragments and improve representations for rare and unseen words.
How it connects
Connects character tokenization, word tokenization, sentence pieces, transformers, word embeddings, Word2Vec, and out-of-vocabulary handling.

Also explained in

Compare how 7 books explain subword tokenization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.