Sub topic 1
Loading a Pipeline and Creating a Doc
spaCy is a Python library for processing natural language. A spaCy pipeline combines several processing steps, such as tokenization, part-of-speech tagging, lemmatization, and dependency parsing. A trained language model supplies the statistical knowledge for these steps.
This chapter assumes that you know basic Python, can install packages, and understand terms such as noun, verb, subject, and object.
Prerequisites
Install spaCy and download the small English model:
python -m pip install spacy
python -m spacy download en_core_web_smThe en_core_web_sm model is suitable for learning and small experiments. Larger models can be more accurate, but they require more disk space and memory.
Loading the Pipeline
Import spaCy and load the model with spacy.load:
import spacy
nlp = spacy.load("en_core_web_sm")The variable nlp is a callable language-processing object. Calling it with text runs the text through the pipeline:
text = "The quick brown fox jumps over the lazy dog."
doc = nlp(text)The result, doc, is a Doc object. It stores the original text and the analysis produced by the pipeline. A Doc is a sequence of Token objects, one for each token recognized by the tokenizer.
The pipeline normally processes text in this order:
flowchart LR
A[Raw text] --> B[Tokenizer]
B --> C[Doc and Tokens]
C --> D[Tagger]
D --> E[Parser]
E --> F[Lemmatizer]
F --> G[Analyzed Doc]The exact components depend on the model. Inspect the active components with pipe_names:
print(nlp.pipe_names)A typical result includes components such as tok2vec, tagger, parser, attribute_ruler, lemmatizer, and ner. The tokenizer creates tokens before the named pipeline components run. Some models include an entity recognizer, while others may omit it.
Inspecting a Doc
You can loop over a Doc just as you loop over a list:
for token in doc:
print(token.text)Each Token provides information about one token. The most important attributes in this tutorial are:
token.text: the exact token texttoken.pos_: a broad part-of-speech labeltoken.tag_: a more detailed part-of-speech tagtoken.lemma_: the base form of the tokentoken.dep_: the token's dependency relationtoken.head: the token that governs it in the dependency tree
This example prints several attributes:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The quick brown fox jumps over the lazy dog.")
for token in doc:
print(
token.text,
token.pos_,
token.tag_,
token.lemma_,
token.dep_,
token.head.text,
)The trailing underscore matters. Attributes such as token.pos_ return readable strings like NOUN. Attributes without the underscore, such as token.pos, return internal integer identifiers. Use the string attributes when inspecting results or writing application logic.
spaCy preserves token order and character positions. This lets you recover spans from the original text:
for token in doc:
print(token.text, token.idx, token.idx + len(token.text))Here, token.idx is the token's starting character offset. Punctuation is usually a separate token, so "fox." becomes the tokens "fox" and ".".
Checking the Model
A loaded pipeline can process text, but its predictions are not guaranteed to be correct. Before building extraction logic, inspect representative examples. Short, clear sentences are useful for learning, but real data may contain spelling errors, unusual formatting, code, or domain-specific language.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Researchers published a dataset.")
print([(token.text, token.pos_, token.dep_) for token in doc])The model's annotations are predictions. Treat them as useful evidence, not as unquestionable facts.
Exercise
What is the difference between nlp and doc in this example: doc = nlp("A sentence.")?
Show answer
nlp is the loaded language pipeline. Calling it with text creates doc, a Doc object containing the text and the pipeline's token-level annotations.