spaCy NLP Pipeline Components in Python
spaCy processes raw text through an integrated series of modular
components called a processing pipeline, transforming raw strings into
annotated Doc objects. When you call an NLP model on text
in Python, the data passes through several built-in components
sequentially, including the tokenizer, part-of-speech tagger, dependency
parser, lemmatizer, and named entity recognizer. This guide details each
standard component within the spaCy pipeline, explaining its specific
role in linguistic analysis and document processing.
The Tokenizer
The pipeline begins with the Tokenizer, which is a
unique, standalone component that always runs first. Unlike subsequent
components, the tokenizer does not modify annotations; instead, it
converts raw text strings into a Doc object containing
individual Token objects based on language-specific
punctuation and splitting rules. All downstream components depend on the
token boundaries established here.
Part-of-Speech Tagger and Morphologizer
- Tagger (
tagger): Assigns fine-grained part-of-speech (POS) tags to each token (such as identifying whether a word is a past-tense verb, proper noun, or adjective). - Morphologizer (
morphologizer): In modern spaCy models (v3+), this component often pairs with or replaces the legacy tagger to assign coarse-grained universal POS tags alongside detailed morphological features (such as gender, number, and case).
Dependency Parser
(parser)
The Dependency Parser analyzes sentence structure by
determining the grammatical relationships between tokens. It establishes
directed syntactic connections, labeling head-child relationships (such
as subject, direct object, or modifier). Crucially, the dependency
parser is also responsible for sentence boundary detection, enabling
iteration over complete sentences via the doc.sents
property.
Lemmatizer (lemmatizer)
The Lemmatizer assigns the base or dictionary form (the lemma) to each token using morphological rules or lookup tables. For instance, it normalizes words like "organized," "organizing," and "organizes" to their root form, "organize." It relies on the morphological features and POS tags generated by preceding components to resolve ambiguous word forms.
Named Entity Recognizer
(ner)
The Named Entity Recognizer (NER) detects and
classifies spans of text into predefined categories representing
real-world objects. It locates entities such as persons, organizations,
locations, dates, and monetary values, storing them in the
doc.ents collection.
Text Categorizer
(textcat / textcat_multilabel)
The Text Categorizer is an optional, trainable component used for document-level classification. While not typically included in standard pre-trained models, it can be added to assign one or more categorical labels to an entire document or individual spans, useful for tasks like sentiment analysis or topic classification.
Auxiliary and Custom Components
spaCy also supports auxiliary components that can be added or swapped depending on the use case:
- Sentencizer (
sentencizer): A lightweight rule-based sentence segmenter that operates without the computational overhead of the statistical dependency parser. - Entity Ruler (
entity_ruler): A rule-based pattern matcher that creates named entities using token patterns or regular expressions, often combined with the statistical NER. - Attribute Ruler (
attribute_ruler): Allows rule-based mapping and overrides for token attributes like tags, lemmas, and POS designations. - Custom Components: Custom functions or classes
registered via the
@Language.componentor@Language.factorydecorators, allowing developers to insert proprietary logic, external model predictions, or custom metadata anywhere in the execution chain.