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.
Classic model or transformer?
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.
Subword tokens
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.

Count the tokens
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.
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.