Text Preprocessing in NLP: When Cleaning Helps and Hurts

Text preprocessing is model-dependent. Lowercasing, stemming, stop-word removal, normalization, and tokenization can help some pipelines and damage others.

Topics

Text preprocessing is not a universal sequence of steps. The correct pipeline depends on the representation and model.

A bag-of-words classifier may benefit from lowercasing and vocabulary normalization. A pretrained transformer may rely on punctuation, casing, and subword structure learned during pretraining. Applying aggressive cleaning can move the input away from the distribution on which the model was trained.

Tokenization

Tokenization maps text into units consumed by a model.

Classical pipelines may tokenize words with whitespace and punctuation rules.

Modern transformers frequently use subword tokenization, so uncommon words are decomposed into reusable pieces.

The tokenizer is part of the model. Replacing it casually can invalidate pretrained embeddings.

Unicode normalization

Visually similar strings can have different Unicode encodings.

Normalization forms such as NFC or NFKC can reduce accidental variation, but compatibility normalization may also change semantically meaningful distinctions.

Unicode handling should therefore be explicit when text comes from heterogeneous systems.

Lowercasing

Lowercasing reduces vocabulary size:

Apple

and

apple

become the same token.

That can help tasks where casing is noise. It can hurt named-entity recognition, authorship signals, or any task where capitalization carries meaning.

Cased pretrained models should generally receive text compatible with their pretraining convention.

Stop-word removal

Words such as "the", "of", and "is" are often removed in classical information-retrieval or bag-of-words pipelines.

That is not universally safe.

Negation, function words, and syntax can matter. Removing "not" from

not effective

would reverse the meaning of the phrase.

Transformer models typically do not need manual stop-word deletion.

Stemming

Stemming heuristically removes affixes.

Examples may map several surface forms to a common stem, but the output need not be a valid word.

This can reduce dimensionality in lexical models, at the cost of linguistic precision.

Lemmatization

Lemmatization maps inflected forms to a dictionary lemma using linguistic analysis.

For example,

running

may map to

run.

Lemmatization is usually more linguistically informed than stemming but is also more computationally involved and language-dependent.

Neither should be applied automatically to contextual transformer models.

Punctuation

Removing all punctuation can discard signal.

Punctuation can encode sentence boundaries, emphasis, code structure, decimals, dates, emoticons, or legal syntax.

The preprocessing choice should follow the task.

Numbers

Replacing every number with a generic token can reduce sparsity, but it can also destroy essential content.

In finance, medicine, engineering, and scientific text, numbers often carry the main information.

A better approach may preserve magnitudes, units, or structured numerical entities.

URLs and email addresses

Whether URLs should be removed depends on whether their identity matters.

For spam detection, domain names can be predictive.

For privacy-sensitive applications, email addresses and identifiers may need redaction before training.

Regex should be used carefully because real URLs and email addresses are more complex than simple tutorial patterns.

Whitespace and formatting

HTML, Markdown, tables, line breaks, and code blocks may contain structure.

Stripping them all into plain text can harm tasks involving document layout or section boundaries.

For retrieval systems, keeping headings and document structure can improve chunk quality.

Preprocessing and leakage

Preprocessing can leak test information.

Vocabulary selection, TF-IDF document frequencies, feature pruning, learned normalization, or topic models must be fitted on training data only.

A correct pipeline is

$$ \text{train text} \rightarrow \text{fit preprocessing} \rightarrow \text{transform train/test separately}. $$

The test corpus should not influence preprocessing parameters.

Deduplication

Duplicate or near-duplicate documents can create severe train-test leakage.

Web corpora, customer tickets, legal templates, and generated text often contain repeated material.

Deduplication should happen before splitting whenever duplicates represent the same underlying content.

Language identification

Multilingual corpora can require language detection before applying tokenizers, stemmers, or dictionaries.

Language identification itself can be uncertain for short text and mixed-language documents.

Do not silently apply English preprocessing rules to multilingual data.

Transformer pipelines

For pretrained transformers, a conservative default is usually:

  1. preserve original text structure
  2. apply only necessary Unicode and privacy normalization
  3. use the model's native tokenizer
  4. truncate or chunk according to context limits
  5. validate preprocessing choices empirically

Manual stemming, stop-word removal, and aggressive punctuation deletion are usually unnecessary.

Classical sparse pipelines

For TF-IDF plus a linear model, more normalization may be useful:

  • optional lowercasing
  • word or character n-grams
  • vocabulary thresholds
  • possibly stemming or lemmatization
  • task-specific token rules

Character n-grams can be especially robust to spelling variation and morphology.

Conclusion

Text preprocessing is part of the statistical model because it determines what information reaches the learner.

The right question is not

Which cleaning steps should every NLP pipeline use?

It is

Which transformations preserve signal, reduce irrelevant variation, and remain compatible with the representation and deployment domain?

References

  • Jurafsky, D., & Martin, J. H. Speech and Language Processing.
  • Manning, C. D., Raghavan, P., & Schütze, H. (2008). Introduction to Information Retrieval.

Embed interactive plots, widgets, and demos using <figure>, <iframe>, or <div class="interactive-embed"> containers. Ensure each embed includes descriptive captions for accessibility.

How to cite

Use the quick export buttons to save citations for reference managers or copy the formatted text directly.

Diogo Ribeiro (2024). Text Preprocessing in NLP: When Cleaning Helps and Hurts. Faculty of Media Arts and Design, Technical University of Porto. https://diogoribeiro7.github.io/machine-learning/text_preprocessing_techniques_nlp_data_science/.

BibTeX

RIS

EndNote

Related posts

Loading mathematical content