Named Entity Recognition › Practical NER with HuggingFace
Language AI HandbookAll chapters Named Entity Recognition › Practical NER with HuggingFaceOne section · Sections in this chapter11 / 17 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
from transformers import pipeline
# Load a NER pipeline backed by a fine-tuned BERT model
# This downloads the model weights on first run (~400 MB)
ner_pipeline = pipeline(
"ner",
model="dslim/bert-base-NER",
aggregation_strategy="simple", # merge consecutive same-type tokens
)
test_texts = [
"Sundar Pichai, CEO of Alphabet, announced new AI features at Google I/O in San Francisco.",
"The European Central Bank, led by Christine Lagarde, raised rates by 25 basis points.",
"MIT researchers published a study in Nature on the treatment of Alzheimer's disease.",
]Text: Sundar Pichai, CEO of Alphabet, announced new AI features at Google I/O in San Francisco. Entity Type Confidence ------------------------------------------------------------ Sundar Pichai PER 0.997 Alphabet ORG 0.999 AI MISC 0.989 Google I / O ORG 0.991 San Francisco LOC 0.999 Text: The European Central Bank, led by Christine Lagarde, raised rates by 25 basis points. Entity Type Confidence ------------------------------------------------------------ European Central Bank ORG 0.999 Christine Lagarde PER 0.942 Text: MIT researchers published a study in Nature on the treatment of Alzheimer's disease. Entity Type Confidence ------------------------------------------------------------ MIT ORG 0.999 Nature ORG 0.993 Alzheimer MISC 0.513
The aggregation_strategy="simple" parameter merges consecutive tokens of the same entity type into a single span, combining their scores by averaging. This is essential for multi-token entities like "Christine Lagarde" that the model labels token by token.
Interpreting Confidence Scores
BERT-based NER models produce a softmax probability over entity types for each token. The scores reported by the pipeline are these probabilities for the predicted class. A score of 0.99 indicates the model is very confident; a score of 0.65 indicates uncertainty.
Low-confidence predictions deserve scrutiny. They often correspond to:
- Ambiguous entity names that could be multiple types (is "Apple" the company or the fruit?)
- Unusual or rare entities not well represented in training data
- Entities at the boundary of entity spans (the last token of a long entity often has lower confidence)
- Domain mismatch between training data and the input text
In production systems, you can use confidence thresholds to accept or reject predictions. A high-precision mode might only accept predictions above 0.95, while a high-recall mode might accept anything above 0.7.
# Demonstrate confidence-based filtering
threshold_high = 0.95
threshold_low = 0.70
ambiguous_text = (
"Apple sued Samsung over patent violations in Delaware. "
"Amazon and Sprint also filed separate suits in California."
)
raw_predictions = ner_pipeline(ambiguous_text)All predictions: Apple ORG 0.999 Samsung ORG 0.998 Delaware LOC 0.999 Amazon ORG 0.999 Sprint ORG 0.998 California LOC 1.000 High-precision mode (>= 0.95): 6 accepted High-recall mode (>= 0.7): 6 accepted