1. Home
  2. Explore
  3. Computer Science
  4. Natural Language Processing with spaCy

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Natural Language Processing with spaCy

Natural Language Processing with spaCy

What is NLP?

Natural Language Processing (NLP) is the process of getting a computer system to understand, parse, and extract human language, often from raw text. It encompasses several tasks:

  • Named Entity Recognition (NER) – identifying entities like people, places, organizations

  • Part-of-Speech (POS) Tagging – labeling words with their grammatical roles

  • Syntactic Parsing – analyzing sentence structure

  • Text Categorization / Classification – assigning labels to text (e.g., spam detection)

  • Coreference Resolution – determining when different words refer to the same entity

  • Machine Translation – converting text from one language to another

NLP is the bridge between unstructured text data and structured, computable information.

NLP vs. Natural Language Understanding (NLU)

NLU is a closely related field that focuses on deeper comprehension:

  • Relation extraction

  • Semantic parsing

  • Question answering (chatbots)

  • Summarization

  • Sentiment analysis

  • Paraphrasing

While NLP handles the basic processing and extraction, NLU aims to understand meaning and intent.

Applications of NLP

Information Extraction

Information extraction converts unstructured raw text into structured data (metadata). A system extracts relevant items such as company names, stock symbols, market indexes, or people’s names. For example, financial analysts use NLP to scan news articles from Reuters, the New York Times, or the Wall Street Journal and pull out mentions of stocks and exchanges.

  • Advantage over simple list matching: NLP frameworks like spaCy provide syntactic and semantic context (POS tags, dependencies, coreference) that make extraction more accurate and flexible.

Text Classification / Text Categorization

Text classification assigns predefined categories to documents. A real-world example is spam detection: a machine learning model on the email server analyzes incoming emails and labels them as spam or not spam. In the legal industry, lawyers reduce millions of documents to a manageable set by classifying them according to case-relevant fields. Researchers also use classification to get a broad sense of large document collections without reading every page.

Why spaCy?

spaCy is a leading NLP framework for Python. It stands out for several reasons:

Off-the-Shelf Models

spaCy provides pre-trained models that benchmark very well — they offer high speed and strong accuracy metrics (precision, recall, F-score). These models work immediately without custom training.

Transformer Support

spaCy can leverage modern transformer models (often grouped under “BERT models”), enabling state-of-the-art performance for complex tasks.

Custom Training

The framework makes it relatively easy to train custom models for specific needs such as a custom dependency parser or named entity recognizer.

Scalability

spaCy was designed by Explosion AI to work at scale — processing hundreds of thousands of documents efficiently, accurately, and with minimal time overhead, especially when using rule-based components.

Installing spaCy

Steps (Using pip on CPU)

  1. Install spaCy via pip in your terminal (or in a Jupyter notebook with !pip install spacy).

  2. Download a language model using the spaCy terminal command:

     python -m spacy download en_core_web_sm
    • The _sm suffix indicates the small model. For this course, the small English model (en_core_web_sm) is sufficient.

Note: GPU installation requires CUDA and additional setup; for this course, use CPU.

  1. Verify installation by importing spaCy and loading the model:

     import spacy
     nlp = spacy.load("en_core_web_sm")

    No errors means everything is installed correctly.

spaCy Containers

spaCy organizes textual information in container objects that hold metadata about the text. The three most important containers are:

  • Doc – the main container; holds the entire processed text and all associated metadata

  • Token – an individual word, punctuation mark, or other meaningful unit within the Doc

  • Span – a slice of the Doc consisting of one or more consecutive tokens (e.g., a multi-word entity like “Martin Luther King”)

There are also Lexeme (vocabulary entry) and Language (factory for creating Doc objects) containers, but Doc, Token, and Span are used most often.

Hierarchy

  • The Doc object is at the top of the hierarchy. It can be iterated to access individual Tokens.

  • A Span is a subsequence of tokens — it exists “within and without” the Doc (can be extracted as its own object).

  • Tokens are the atomic units; multiple tokens combine to form a Span.

This structure is often visualized as a pyramid, with the Doc at the top containing all the data.

Creating a Doc

import spacy
nlp = spacy.load("en_core_web_sm")

# Read text from file
with open("data/wiki_us.txt", "r") as f:
    text = f.read()

# Create the Doc object
doc = nlp(text)

Once created, the doc object contains rich metadata: sentence boundaries (doc.sents), token attributes (POS tag, dependency, entity type, etc.), and spans can be extracted based on token indices or entity recognition.

Course Structure (from the Video)

This video series is part of a larger course that spans three parts:

  • Part 1: Using spaCy’s off-the-shelf models to solve general NLP problems

  • Part 2: Building custom rule-based components (EntityRuler, Matcher, regular expressions) for domain-specific problems not covered by pre-trained models

  • Part 3: Applying everything to a real-world information extraction task from financial documents (stocks, market indexes, stock exchanges)

Additionally, a free companion textbook is available at spacy.pythonhumanities.com to reinforce the video content. The recommended study approach is:

  1. Watch the entire video to get an overview

  2. Follow along coding the examples

  3. Attempt to replicate the tasks independently on a third pass

“NLP is a complex field, but frameworks like spaCy make the process much easier. With a few hours of study, you can achieve things you didn’t think possible.”

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent