Notes

Whitespace Normalization

Text copied off the web arrives scuffed: a run of spaces where a layout collapsed, a tab left over from a spreadsheet, a non-breaking space that looks exactly like an ordinary one, a trailing blank nobody notices. Whitespace normalization is the quiet step that irons all of it flat.

What the step does
It rewrites the invisible characters to a single convention, usually in one pass of a regular expression:

  • runs of spaces, tabs and newlines collapse to one space;
  • exotic blanks — the non-breaking space, the zero-width space, the ideographic space — become an ordinary space or are removed;
  • leading and trailing whitespace is stripped and line endings are unified;
  • paragraph breaks are kept or dropped as a deliberate choice, because sentence segmentation often leans on them.

Why so small a step matters
Almost everything downstream treats whitespace as a boundary. Splitting on whitespace yields empty tokens from a double space; a non-breaking space inside New York prevents the split a plain space would have caused, so the pair becomes one unseen type. A TF-IDF vocabulary fills up with near-duplicates that differ only by an invisible character, deduplication by hash fails on two copies of one article that differ only in indentation, and a subword tokenizer like SentencePiece, which encodes the space itself as a marker, spends tokens on the mess and shifts the segmentation of the word that follows. Run HTML stripping first — markup is where most of the damage comes from.

Where to be careful
Whitespace is sometimes information: indentation in code, column alignment in tables, line breaks in poetry or in a PDF where they mark real layout. Collapsing it also moves every character offset, so entity spans and annotations recorded against the raw text must be recomputed against the cleaned string.

Whitespace normalization standardises the invisible characters in text: runs of spaces, tabs and newlines collapse to a single space, exotic blanks such as the non-breaking space become ordinary ones, line endings are unified and leading or trailing space is stripped. Since tokenizers treat whitespace as a boundary, skipping it yields empty tokens, near-duplicate vocabulary entries and failed exact matches — and because collapsing shifts character offsets, annotation spans must be recomputed afterwards.

Think of a proofreader tidying a page before it goes to print: double spaces squeezed shut, stray tabs taken out, blank lines at the end trimmed away. Not a word of the meaning changes. The page simply becomes consistent.

Whitespace normalization is that tidying, applied to text before software reads it. Text scraped from web pages is full of odd gaps, including blank-looking characters that are not the ordinary space at all. Programs chop sentences into words at the gaps, so an unexpected gap makes them see two words where you see one — or none where you see a space. Smoothing the gaps first keeps all the counting that follows honest.