Concepts / Text Data Preprocessing

Text Data Preprocessing

The important boundary is between human-readable text and numeric model input. Tokenization identifies the sequence units; vectorization gives those units numeric representations; the resulting vectors are packed into sequence tensors.

  • Programming
Interactive lab

Try it: Text Vectorization: Tokens to Vectors

The text-to-tensor pipeline: standardize, split into tokens, build a frequency-ranked vocabulary with padding and [UNK], map tokens to integer ids, pad or truncate, then one-hot or multi-hot encode and look the ids up in an embedding table.

How it works

  1. Standardize: lowercase and delete punctuation.
  2. Split on whitespace into tokens.
  3. Build the vocabulary: 0 = padding, 1 = [UNK], then words by frequency (ties: first appearance), capped at max tokens.
  4. Map each token to its id (1 if it did not make the vocabulary), then pad with 0 or truncate to the sequence length.
  5. Encode: one-hot rows per position or one multi-hot row per text; embed: id k selects row k of the embedding table.

Default run (18 steps): Raw text (59 characters): "The movie was great. The plot was great, the acting wasn't!". A model cannot read strings, so it is turned into numbers step by step. … Embedding lookup: id k selects row k of a fixed 8 x 3 table, giving a (8, 3) dense matrix instead of (8, 8) sparse one-hots. Row 0 (padding) is looked up too; a trained Embedding layer would learn these rows.

Simplified: Bounded ASCII text (up to 200 characters, 40 tokens), whitespace splitting only, and a small FIXED embedding table (a real Embedding layer learns its rows). The multi-hot vector keeps the padding slot (always 0) so its positions line up with the ids.

Educational simulation

Loading the simulation…

From Words to Model Input

A neural network does not receive raw text directly. Before a model can process a sentence, the text must be represented as numeric data. This gives sequence processing two broad stages: create a numerical representation of the sequence, then apply a model designed to process ordered data.

Raw textordered inputTokenswords, subwords, orcharactersNumeric vectorsone vector per tokenSequence tensorneural-network inputtokenizationvectorizationpack into sequence
How does raw text move through tokenization and vectorization to become input that a neural network can process?
Key point

The important boundary is between human-readable text and numeric model input. Tokenization identifies the sequence units; vectorization gives those units numeric representations; the resulting vectors are packed into sequence tensors.

Token Units and Numeric Values

Definition

Tokenization is the step that chooses and identifies the units in a sequence. For text, those units may be words, subwords, or characters. Tokenization does not by itself create the numeric vectors needed by a neural network.

Definition

Text vectorization is the step that supplies each generated token with a numeric vector. Those vectors can then be packed into sequence tensors and passed to a deep neural network.

Texttoken at position 0Vector 0numeric representationdatatoken at position 1Vector 1numeric representationflowstoken at position 2Vector 2numeric representationvectorizevectorizevectorize
How are sequence units identified and then associated with numeric values?
Worked example

A Three-Token Sequence

Illustrate the difference between tokenization and vectorization for the generated phrase "Text data flows".

  1. 1
    Tokenize

    Treat the phrase as three word tokens: "Text", "data", and "flows". Tokenization has identified the ordered units, but the result is still text.

  2. 2
    Vectorize

    Associate each token with a numeric vector. The particular numeric values are implementation choices in this illustration; the important point is that every token receives a numeric representation.

  3. 3
    Pack

    Place the vectors in their sequence order to form a sequence tensor that can be supplied to a neural network.

Result

Tokenization determines what the units are. Vectorization turns those units into numeric representations. Packing preserves their order in the model input.

Ways to Represent Tokens

Two major ways to associate vectors with tokens are one-hot encoding and token embedding. The source material presents token embedding as typically being used for words, where it is also called word embedding. Both belong to the vectorization stage because they provide numeric representations for the generated tokens.

Representation approachRole in preprocessingSource terminology
One-hot encodingAssociates a numeric representation with each tokenOne of the two major approaches
Token embeddingAssociates a vector with each tokenTypically used for words; also called word embedding

Both approaches provide numeric representations after tokenization.

Best practice

When describing a text pipeline, state both decisions explicitly: which units are tokens and which numeric representation is assigned to each token. This makes it clear how raw text becomes a sequence tensor.

Sliding Windows over Sequences

Once a sequence has become numeric input, a one-dimensional convnet applies the one-dimensional convolutional approach to sequence processing. Its broad role is to examine local regions of an ordered sequence as a sliding operation. This makes it suitable for detecting patterns that appear within nearby sequence positions.

Ordered sequencetoken positionsLocal region 1nearby positionsLocal region 2shifted positionsLocal region 3later positionsLocal patternsdetected featuresinspectslideslidedetect
How does a one-dimensional convolution examine different local regions of an ordered sequence?
Key point

The sequence remains ordered while the local region being examined moves across it. The central idea is not simply that the input is numeric; it is that the numeric input still represents an ordered sequence whose local structure can be examined.

Convolutional and Recurrent Roles

One-dimensional convnets and recurrent neural networks are presented as two fundamental deep-learning approaches for sequence processing. A one-dimensional convnet broadly scans local regions of the sequence to detect local patterns. An RNN broadly processes sequence information recurrently over time. The distinction is about the model's way of processing the ordered input, not about whether preprocessing is required: both approaches still need numeric sequence tensors rather than raw text.

Numeric sequenceordered tensorNumeric sequenceordered tensorLocal scanone-dimensional convolutionRecurrent processingover timeLocal patternsdetected featuresSequence informationprocessed recurrentlyscandetectprocesscarry through sequence
What is the broad difference between a one-dimensional convnet scanning local regions and an RNN processing sequence information recurrently over time?

Where Local Patterns Matter

A one-dimensional convnet is a reasonable sequence-processing choice when the task can benefit from detecting patterns in nearby positions of an ordered input. Text and timeseries are both examples of sequence data, and other ordered inputs can be treated in the same general way. The model choice should follow the structure of the sequence and the kind of pattern the task requires the model to process.

1D convnetlocal pattern detectorTextword or character sequenceTimeseriesordered positionsOther ordered inputsequence datacan processcan processcan process
Which sequence-processing inputs can be considered when the task benefits from detecting local patterns?
In the real world

Consider a text task in which nearby words or characters form useful patterns. After the text has been tokenized and vectorized, a one-dimensional convnet can scan the resulting sequence to detect local features. The same broad reasoning can apply to timeseries data because timeseries are also organized over ordered positions.

Common Preprocessing Mistakes

  • Common mistake

    Treating raw text as if it were already neural-network input

    What it looks like

    Describing a sentence as the final input without explaining how it becomes numeric.

    Why it fails

    A neural network needs a numeric tensor rather than raw text.

    Do this instead

    State the tokenization step, the vectorization step, and the packing of vectors into a sequence tensor.

  • Common mistake

    Calling tokenization and vectorization the same operation

    What it looks like

    Saying that splitting a sentence into words has already produced the numeric representation.

    Why it fails

    Tokenization identifies the units, while vectorization supplies each token with a numeric vector.

    Do this instead

    Describe tokenization first and vectorization second.

  • Common mistake

    Ignoring order in sequence data

    What it looks like

    Discussing text as though it were an unordered collection of values.

    Why it fails

    Text, timeseries, and other sequence data are ordered inputs.

    Do this instead

    Preserve the sequence positions when describing the numeric tensor and the model's processing.

  • Common mistake

    Describing a one-dimensional convnet as recurrent processing

    What it looks like

    Explaining a convolutional scan as though it carried information recurrently over time.

    Why it fails

    The broad roles differ: a one-dimensional convnet scans local regions, while an RNN processes sequence information recurrently over time.

    Do this instead

    Use local scanning and local-pattern detection for the convnet, and recurrent processing over time for the RNN.

Practice Check

EASY

A sentence is split into word units, and each word is then associated with a numeric vector. Explain which operation performed tokenization, which performed vectorization, and what must happen before the result can be passed to a neural network.

Hints
  • Tokenization identifies the units in the sequence.
  • Vectorization supplies a numeric vector for each generated token.
  • The vectors are packed into a sequence tensor.
EASY

Choose between these descriptions: a model scans nearby positions to detect local patterns, or a model processes sequence information recurrently over time. Which description matches a one-dimensional convnet, and which matches an RNN?

Hints
  • Look for the phrase local regions.
  • Look for the phrase recurrently over time.

Key Takeaways

  • Sequence data includes text, timeseries, and other ordered inputs.
  • A neural network needs a numeric tensor rather than raw text.
  • Tokenization identifies units such as words, subwords, or characters; vectorization assigns those units numeric vectors.
  • One-hot encoding and token embedding are two major ways to associate vectors with tokens, with token embedding typically used for words and also called word embedding.
  • A one-dimensional convnet scans local regions to detect patterns, while an RNN processes sequence information recurrently over time.

Related concepts

More concepts starting with T