← Journal
Extract Names, Organizations, and Amounts from Text with spaCy

Extract Names, Organizations, and Amounts from Text with spaCy

A record of running spaCy, a comprehensive NLP library with named entity recognition (NER), while the network was disconnected. It correctly extracted a person’s name, organization name, amount, place name, and date from a single sentence.

Repository covered in this article

explosion/spaCy

💫 Industrial-strength Natural Language Processing (NLP) in Python

33.7k Python MIT License

You may need to pull specific details such as people’s names, organization names, and amounts out of a news article or internal document. Regular expressions cannot handle spelling variations and context-dependent decisions on their own. spaCy provides a pipeline for natural language processing tasks, including named entity recognition (NER). I installed it and tested whether it could extract named entities with the network disconnected.

spaCy is an NLP library that combines tokenization, part-of-speech tagging, and named entity recognition in one pipeline. The project releases its source under the MIT License (*1).

1. Install spaCy

Download the lightweight English model (en_core_web_sm) during the build, while the network is available.

FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'spacy==3.8.14' 'click==8.4.2'
RUN python -m spacy download en_core_web_sm
WORKDIR /work

Installing spacy alone did not add the click dependency required by the spacy download command, so I added click as a separate dependency.

podman build -t spacy-sandbox .

The -v option mounts the specified directory in the container with read/write access. Run this from an empty directory dedicated to the experiment.

2. Extract named entities from one sentence

I prepared one sentence containing a person’s name, organization name, amount, place name, and date, then passed it through spaCy’s pipeline. I ran it with the network disconnected.

import spacy

nlp = spacy.load("en_core_web_sm")

text = (
    "Satya Nadella announced that Microsoft will invest $10 billion in a new "
    "data center in Tokyo starting next March."
)
doc = nlp(text)

for ent in doc.ents:
    print(f"{ent.text!r:35s} -> {ent.label_}")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work spacy-sandbox \
  python ner_demo.py

The command produced the following output.

ENTITIES:
  'Satya Nadella'                     -> PERSON
  'Microsoft'                         -> ORG
  '$10 billion'                       -> MONEY
  'Tokyo'                             -> GPE
  'next March'                        -> DATE

spaCy assigned the correct category to all five entities in the sentence: the person, organization, amount, place, and date. Separate regular expressions seem sufficient for this sentence. Recognizing $10 billion as money and next March as a date requires context.

Let the pipeline handle extraction and context

Named entity extraction often depends on context as well as the form of each word. By handing the task to a pipeline such as spaCy, you can turn extraction into a repeatable process that also handles context-dependent decisions beyond what regular expressions can cover.

Sources

*1 spaCy (GitHub), https://github.com/explosion/spaCy, MIT License