Lesson 1 of 5 Text Preparation, Embeddings and Vector Databases

Pre-processing and Tokenizing Text

This lesson explains how text is converted into units a model can process, how this differs for classic NLP models and transformers, and how it affects accuracy, context limits and token-based cost.

Learning outcomes

After completing this lesson, you will be able to:

  • Apply classic NLP pre-processing steps.
  • Explain subword tokenisation for transformers.
  • Relate tokens to context windows and cost.

Classic NLP pre-processing

Traditional models such as bag-of-words classifiers, TF-IDF with logistic regression and BlazingText benefit from normalisation. Typical steps are lowercasing, removing punctuation and markup, removing stop words (very common words such as "the"), stemming (cutting words to a crude root, such as "running" to "run") and lemmatisation (mapping to the dictionary form using grammar, such as "better" to "good"). N-grams keep short phrases such as "not good" together. These steps reduce vocabulary size and noise.

Sort into groups

Classic model or transformer?

Exam skills 1.2.6

Decide which text pre-processing steps suit a classic bag-of-words model and which suit a transformer.

Log in to try this activity Interactive activities are free with an account.

Tokenization for transformers

Tokenization splits text into tokens. Word tokenisation fails on rare and new words, so transformer models use subword tokenisation: Byte-Pair Encoding (BPE), WordPiece or SentencePiece. Frequent words become single tokens, and rare words are split into pieces, such as "tokenization" into "token" and "ization", so the vocabulary stays fixed while any text can be represented. Each model has its own tokeniser, and the same tokeniser must be used in training and inference.

Transformers learn from context, so heavy classic pre-processing usually hurts them: removing stop words or stemming destroys information the model uses. Clean markup and fix encoding, but otherwise feed natural text. The following table contrasts the two approaches.

Classic NLP models
LowercasingUsually yes
Stop-word removalOften
Stemming / lemmatisationOften
TokenizationWords or n-grams
Remove HTML, fix encodingYes
Transformer models and FMs
LowercasingNo (tokeniser handles case)
Stop-word removalNo
Stemming / lemmatisationNo
TokenizationModel's own subword tokeniser
Remove HTML, fix encodingYes
True or false

Subword tokens

Exam skills 1.2.6

Check how subword tokenization treats words a model has not seen.

Log in to try this activity Interactive activities are free with an account.

Tokens, context windows and cost

Foundation models have a maximum context window measured in tokens, covering the prompt and the response, and Bedrock prices input and output tokens. In English, a token is roughly three-quarters of a word on average, but code, numbers and other languages can use more tokens per word. Embedding models also have maximum input sizes, which is why long documents are chunked before embedding. Counting tokens with the model's tokeniser during data preparation avoids truncated inputs and unexpected costs.

The following diagram shows the ticket as each model receives it.

The same ticket prepared for two models. The TF-IDF baseline gets full normalisation: markup removed, lowercase, stop words removed and words stemmed. The transformer gets natural text with only the markup removed, and its subword tokeniser keeps the negation in isn't, which the baseline's preparation had destroyed.
Token boundaries depend on the model's tokeniser.
Fill in the value

Count the tokens

Exam skills 1.2.6 · 4.2.10

Estimate token counts and what they mean for context windows.

Log in to try this activity Interactive activities are free with an account.
Worked example

One dataset, two models

A team classifies support tickets with two candidates: a TF-IDF plus Linear Learner baseline and a fine-tuned transformer from SageMaker JumpStart.

Raw:          "<p>My router isn't connecting!! Tried restarting 3x.</p>"
Baseline:     "router connect tri restart 3x"                 (clean, lowercase, stop words, stems)
Transformer:  "My router isn't connecting!! Tried restarting 3x."  (markup removed only)
Tokens:       ["My", " router", " isn", "'t", " connect", "ing", "!!", ...]

Applying the baseline's stemming and stop-word removal to the transformer input removed "isn't", which carries the negative meaning, and reduced accuracy. Keeping natural text for the transformer and full normalisation for the baseline gave each model the input it was designed for.

Check yourself

Why should text for a transformer model not be stemmed or stripped of stop words?

Show answer

Transformers use their own subword tokeniser on natural text; classic pre-processing removes information they rely on.

Exam Tips

Remember for the exam

  • Classic NLP: lowercase, stop words, stemming or lemmatisation, n-grams.
  • Transformers: use the model's own subword tokeniser on natural text.
  • Subword tokenisation handles rare and new words.
  • Context windows and FM prices are measured in tokens.
  • Use the same tokeniser for training and inference.