Previously, on Day 7: Explained part-of-speech (POS) tagging, its importance in NLP, different approaches to tagging, common tagsets, and typical challenges such as ambiguity and unknown words.
Named Entity Recognition (NER) is a key task in natural language processing (NLP). Its job is to automatically find and label “named entities” in text.
A named entity is a word or phrase referring to a specific person, place, organization, date, value, or other real-world item. These are usually proper nouns or names, such as “London,” “Microsoft,” or “January 1, 2022.” NER systems identify these spans of text and label what kind of entity each one is.
Some common categories for entities (called entity types) include:
NER is important because most of the useful information in text involves knowing what is mentioned, not just which words appear. For example, extracting all company names, people, or places from a document lets you answer questions and organize information in ways that raw text cannot.
NER is one building block among others in NLP. It focuses on extracting “things” mentioned in text, rather than analyzing grammar.
[Mr., ., Smith, went, to, Paris, .].NER builds on these steps. It looks at the tokens and phrases to find meaningful real-world entities, and labels their type (person, place, organization, etc.). For instance, “Steve Jobs” is a noun phrase—but NER identifies it specifically as a PERSON entity.
Most NER tools use a handful of carefully chosen types:
Different tools or datasets might include more or fewer categories, but these are the most common.
NER systems have developed over time, moving from simple pattern matching to advanced machine learning:
In every approach, NER takes a chunk of text and returns a list of entity spans, with a label for each one.
For example, with the sentence “Tim Cook runs Apple in Cupertino.” an NER system might return:
Before NER processes text, it’s usually prepared in a couple of steps:
NER usually labels each token using the BIO tag format (also called IOB):
Example with the sentence:
“Steve Jobs founded Apple in California.”
| Token | Tag |
|---|---|
| Steve | B-PERSON |
| Jobs | I-PERSON |
| founded | O |
| Apple | B-ORG |
| in | O |
| California | B-GPE |
| . | O |
Some NER tools also output the “span” (start and end token indices):
The core idea: the system identifies which part of the text is an entity, and what kind it is.
You don’t need to build NER from scratch to see it work. Tools like spaCy offer pre-trained English NER models.
Here’s a minimal code example. Given this sentence:
“Apple was founded by Steve Jobs in California.”
Let’s run NER with spaCy:
import spacy
# Load a small English model with NER support
nlp = spacy.load("en_core_web_sm")
text = "Apple was founded by Steve Jobs in California."
doc = nlp(text)
for ent in doc.ents:
print(ent.text, ent.label_)
Output:
Apple ORG
Steve Jobs PERSON
California GPE
You can run this if you have spaCy installed (pip install spacy and python -m spacy download en_core_web_sm). The important point is that NER gives back both the exact words (“spans”) and a label for each, showing which things are mentioned in text.
Knowing who, what, and where is foundational for building search engines, news apps, chatbots, and more. NER takes the raw stream of words and tells you what real-world things are there.
Write a short Python script using spaCy to analyze three sentences you invent. For each sentence, extract all named entities and print both the entity text and its predicted label. Experiment with different kinds of names (people, places, dates, etc.) to see which types are detected.
Coming up on Day 9: Text Classification with Naive Bayes and Logistic Regression