Sub topic 13
Pipeline Components, `nlp.pipe`, and Disabling
Assume you know basic Python, can install spaCy models, and understand nouns, verbs, subjects, and objects. This chapter focuses on controlling a trained pipeline for speed and predictable output.
What a spaCy pipeline does
A spaCy pipeline is an ordered list of processing components. Each component reads a Doc object and adds information to it. A typical English pipeline might contain:
tok2vec, which creates token representations.tagger, which predicts fine-grained part-of-speech tags.parser, which predicts dependency relations and sentence boundaries.ner, which finds named entities.lemmatizer, which assigns base forms to words.
The exact components depend on the model. Inspect them instead of assuming their names or order.
import spacy
nlp = spacy.load("en_core_web_sm")
print(nlp.pipe_names)
doc = nlp("The research team published the results.")
print([(token.text, token.pos_, token.dep_) for token in doc])
print([(ent.text, ent.label_) for ent in doc.ents])The call nlp(text) runs the text through every enabled component in order. The resulting Doc contains token attributes, dependency information, and entities produced by those components.
Disabling components
Disable a component when you do not need its output. For example, if you only need part-of-speech tags and dependencies, named entity recognition may be unnecessary.
import spacy
nlp = spacy.load("en_core_web_sm", disable=["ner"])
print(nlp.pipe_names)
doc = nlp("Acme hired three engineers.")
print([(token.text, token.pos_, token.dep_) for token in doc])
print(doc.ents) # Empty because NER is disabledDisabling a component reduces processing work, but it also removes that component's annotations. If you disable parser, token.dep_, token.head, and dependency-based sentence boundaries will not contain the normal parser output. If you disable tagger, token part-of-speech predictions may be unavailable or may not be produced correctly by the remaining components.
A component can also depend on information from earlier components. Therefore, disabling a component can affect later components, even when those later components remain enabled.
Processing many texts with `nlp.pipe`
Calling nlp repeatedly works, but nlp.pipe processes texts in batches. Batching reduces Python overhead and usually improves throughput.
import spacy
texts = [
"The cat sat on the mat.",
"Dogs chase balls in the park.",
"A bird flew across the garden.",
]
nlp = spacy.load("en_core_web_sm", disable=["ner"])
for doc in nlp.pipe(texts, batch_size=50):
print(doc.text)
print([(token.text, token.pos_, token.dep_) for token in doc])batch_size controls how many texts spaCy groups during processing. The best value depends on text length, available memory, and the model. A large batch can improve speed but use more memory.
For production work, measure several values rather than selecting one by guesswork. Keep the input order in mind: nlp.pipe yields Doc objects in the same order as the input texts.
Checking the configuration
Before relying on output, inspect nlp.pipe_names and verify that every attribute your code needs is generated. A fast pipeline with missing annotations is less useful than a slower, complete one.
Exercise
You need dependency relations for 10,000 texts but do not need named entities. What configuration should you use?
Show answer
Load the model with disable=["ner"] and process the texts with nlp.pipe, while keeping the parser enabled.