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). NER finds and labels "named entities" in text. A named entity is a real-world thing—such as a person, location, or organization—that has a specific name. For example, in “Alice went to Paris,” “Alice” is a person and “Paris” is a location.
NER is different from Part-of-Speech (POS) tagging. POS tagging labels each word’s role in the sentence (like noun, verb, or adjective). NER instead asks: is this word (or group of words) a name for a real-world thing, and what kind?
NER powers many applications. Search engines use it to find people or companies in news stories. Chatbots use it to spot product names or locations. In general, NER helps machines find the “important things with names” inside text.
Common entity examples include:
The list can vary with the application.
NER starts by splitting text into pieces called “tokens.” A token is usually a word or a punctuation mark. The NER system examines each token (and often nearby tokens) to decide if it forms part of a named entity, and if so, of what type.
Example:
Barack Obama was born in Hawaii.
Barack, Obama, was, born, in, Hawaii, .]Barack (begin person), Obama (inside person), was (O), born (O), in (O), Hawaii (begin location), . (O)]Here, "Barack Obama" is recognized together as a person. "Hawaii" is labeled as a location. “O” means that token is outside any named entity.
NER’s challenge is to get these groupings right, using the words and their context.
Most NER systems use a standard set of entity types. The most common are:
Others are possible, but these cover many use cases.
To show where each entity starts and ends in the text, NER uses an annotation scheme. The most popular is BIO:
For example:
Sentence: Steve Jobs founded Apple in California.
Annotated:
BIO tags help the system mark exactly which words belong to which entities, including names that are more than one word long.
NER systems generally fall into two types: rule-based or machine learning-based.
Rule-Based NER uses patterns, dictionaries, or manual rules. For instance, if a word is in a list of cities, mark it as a location. Regular expressions can spot certain formats (like dates or money).
Machine Learning-Based NER trains a model using labeled examples. The model learns the patterns that signal the start, end, and type of entities. Most modern NER systems—including spaCy and Hugging Face—use this approach.
Rule-based methods work if the domain is small and data is limited. For most real-world applications, machine learning is the default choice.
Let’s see NER in action using spaCy, a powerful Python NLP library.
import spacy
# Load spaCy's English NER model (download 'en_core_web_sm' first if needed)
nlp = spacy.load("en_core_web_sm")
text = "Elon Musk founded SpaceX in California in 2002."
doc = nlp(text)
for ent in doc.ents:
print(ent.text, ent.label_)
This script:
Elon Musk PERSON
SpaceX ORG
California GPE
2002 DATE
This is all it takes to extract named entities from text with spaCy’s out-of-the-box model.
NER works well much of the time, but has real limits:
Many NER models are trained on news or “clean” text. They often perform worse on tweets, technical documents, or text with lots of slang or typos.
Keep these limitations in mind. NER is a powerful tool, but not foolproof. Understanding what it does (and doesn’t do) helps you use it effectively in real applications.
Pick a few sentences from recent news articles or Wikipedia. Run them through the provided spaCy code and list all named entities detected, along with their types. For any entities that are missed or mislabeled, note what happened and think about possible reasons (such as ambiguity or unusual spelling).
Coming up on Day 9: Text Classification with Naive Bayes and Logistic Regression