The NLP pipeline is the order in which text is cleaned, labelled, and turned into numbers. Every technique in this folder sits at exactly one layer of it, and the layer tells you what the technique consumes and what it produces.

The Five Layers

LayerWhat is this layer about?Techniques
0. ToolsThe tools we can use for this pipelineRegular Expression (RegEx), SpaCy
1. PreprocessingThe preprocessing techniques we can useTokenization, Normalization, Stop Word Removal, Stemming, Lemmatization
2. Linguistic AnnotationHow we can understand each token and how they relate to one anotherPart-of-Speech (POS) Tagging, Dependency Parsing, Named Entity Recognition (NER), Word Sense Disambiguation (WSD)
3. RepresentationChanging words into numbersVectorization, N-gram Language Models
4. ApplicationWhat we are trying to doInformation Extraction

Two of these are not techniques at all:

  • Preprocessing Techniques is a category name that covers the whole of Layer 1
  • SpaCy is a library that implements Layers 1 and 2 for you

The Pipeline

graph TD
    RAW["Raw Text"] --> RE["Regular Expression"]
    RE --> TOK["Tokenization"]

    subgraph L2["Layer 2: Linguistic Annotation"]
        POS["POS Tagging"]
        DEP["Dependency Parsing"]
        NER["Named Entity Recognition"]
        WSD["Word Sense Disambiguation"]
        POS --> DEP
        POS --> WSD
    end

    subgraph L1["Layer 1: Preprocessing"]
        NORM["Normalization"]
        STOP["Stop Word Removal"]
        STEM["Stemming"]
        LEM["Lemmatization"]
        NORM --> STOP
        NORM --> STEM
    end

    subgraph L3["Layer 3: Representation"]
        VEC["Vectorization"]
        NGRAM["N-gram Language Model"]
    end

    TOK --> POS
    TOK --> NER
    TOK --> NORM
    TOK --> NGRAM
    POS --> LEM
    STOP --> VEC
    STEM --> VEC
    LEM --> VEC

    DEP --> IE["Information Extraction"]
    NER --> IE
    WSD --> IE
    RE -.-> IE
    VEC --> ML["Downstream Model"]
    NGRAM --> ML

There are two routes through the pipeline. The left route strips the sentence down for a bag-of-words model. The right route keeps the sentence intact so a parser can read its grammar.

What Needs What

TechniqueRequirementReason
Everything below Layer 0Tokenizationyou need to split before being able to label anything
LemmatizationPOS Taggingthe lemma of saw depends on whether it is a verb or a noun
Dependency ParsingPOS Taggingthe parser uses tags as input features
Word Sense DisambiguationPOS Tagging and a context windowLesk compares the dictionary gloss against the neighbouring words
VectorizationTokenization, optionally stems or lemmasstems and lemmas shrink the vocabulary and the matrix
N-gram Language ModelTokenization onlyit works directly on the raw token sequence
Stemmingnothingit is pure suffix rules, with no dictionary and no POS

That last row is the whole point of stemming which is fast and crude. Lemmatization on the otherhand is slow but more accurate.

Order Traps

These are the sequencing errors that break the pipeline:

  • stop word removal before dependency parsing destroys the tree, because the parser needs is, by and the
  • stemming or lemmatization before POS tagging or NER breaks both taggers, because they read the surface form and the capitalisation
  • lemmatization before POS tagging is backwards, since the lemma depends on the tag
  • stop word removal before an N-gram model or sentiment work deletes not, and the meaning flips

The safe default order:

regex clean --> tokenize --> POS tag --> parse / NER / WSD
            --> normalize --> stop words --> lemmatize --> vectorize

Aggressive preprocessing belongs to the bag-of-words route only. The linguistic annotation route wants the sentence intact. Picking the wrong route for the task is the most common mistake.

What SpaCy Covers

TaskIn spaCy?Note
TokenizationYesrules plus exception lists, not a plain regex split
POS TaggingYestoken.pos_ for coarse tags, token.tag_ for fine tags
Dependency ParsingYestoken.dep_, token.head, token.children
NERYesdoc.ents
LemmatizationYestoken.lemma_, uses the POS tag
Stop Word RemovalYestoken.is_stop
VectorizationYesword vectors in the medium and large models
StemmingNospaCy has no stemmer by design, use NLTK PorterStemmer

The last row is easy to get wrong. For stemming the answer is NLTK, never spaCy.

Layer 3 ends with a matrix of counts, and the course carries on from there with Term Weighting Schemes.