Advanced NLP with SpaCy: Python Library Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to SpaCy

SpaCy is a cutting-edge library for Natural Language Processing (NLP), built from the ground up for production use. Developed by Explosion AI, it has become the de facto standard for industrial-grade NLP solutions thanks to its high performance, accuracy, and ease of use.

Key Advantages of SpaCy

Architecture and Performance

SpaCy is written in Cython, delivering exceptional speed when processing text data. The library uses optimized algorithms and data structures that can handle millions of tokens per minute on standard hardware.

Multilingual Support

The library supports over 20 languages, including English, Chinese, Japanese, Arabic, and many more. Each language comes with specialized pre-trained models fine-tuned for its unique linguistic features.

Ready-to-Use Models

SpaCy provides pre-trained models in different sizes (small, medium, large), letting you choose the ideal balance between speed and accuracy for your specific task.

Installing and Setting Up SpaCy

Installing the Base Library

pip install spacy

Downloading Language Models

For English:

python -m spacy download en_core_web_sm
python -m spacy download en_core_web_md
python -m spacy download en_core_web_lg

For Russian:

python -m spacy download ru_core_news_sm
python -m spacy download ru_core_news_md
python -m spacy download ru_core_news_lg

Loading and Initializing

import spacy

# Load the model
nlp = spacy.load("en_core_web_sm")

# Process text
doc = nlp("Apple is looking at buying a startup in the UK.")

Core Components of the SpaCy Pipeline

Tokenization

SpaCy automatically splits text into tokens, handling language-specific rules, punctuation, and special characters:

doc = nlp("Don't worry, it's working!")
for token in doc:
    print(token.text, token.is_alpha, token.is_punct, token.like_num)

Morphological Analysis

Each token carries rich morphological information:

for token in doc:
    print(f"Word: {token.text}")
    print(f"Lemma: {token.lemma_}")
    print(f"Part of Speech: {token.pos_}")
    print(f"Tag: {token.tag_}")
    print(f"Morphological Features: {token.morph}")

Dependency Parsing

SpaCy builds a dependency tree for each sentence:

for token in doc:
    print(f"{token.text} <- {token.dep_} <- {token.head.text}")
    print(f"Children: {[child.text for child in token.children]}")

Lemmatization and Stemming

Lemmatization in SpaCy

SpaCy provides high-quality lemmatization out of the box:

doc = nlp("The cats are running and jumping")
for token in doc:
    print(f"{token.text} -> {token.lemma_}")

Stemming Alternatives

While SpaCy doesn't include stemming directly, you can integrate it with other libraries:

from nltk.stem import PorterStemmer
stemmer = PorterStemmer()

def add_stemming(doc):
    for token in doc:
        token._.stem = stemmer.stem(token.text)
    return doc

# Add as a custom attribute
spacy.tokens.Token.set_extension("stem", default=None)

Part-of-Speech Tagging

Universal POS Tags

SpaCy uses a universal tagging

Recommendations