Tuesday , December 1 2026
Named Entity Recognition explained: entity types, spaCy and Hugging Face transformer examples, BIO tagging, evaluation and custom entities.

Named Entity Recognition (NER) in Python with spaCy and BERT

Updated: 29 November 2026Level: IntermediateReading time: about 8 minutesspaCy · Hugging Face

Named Entity Recognition (NER) finds and labels real-world names in text – people, organisations, places, dates, amounts. It turns unstructured text such as news, invoices or resumes into structured data.

What You Will Learn

  • Common entity types
  • NER with spaCy
  • NER with a Hugging Face transformer
  • BIO tagging and evaluation
  • Custom entities and Indian-language text

Prerequisites

NLP Introduction, Tokenization.

Entity Types

Label Meaning Example
PERSON People Sundar Pichai
ORG Companies, institutions Infosys, IIT Bombay
GPE Countries, cities, states Ludhiana, Punjab, India
DATE / TIME Dates and times 15 August 2026
MONEY Monetary values ₹25,000
PRODUCT, EVENT, LAW… Other categories iPhone, IPL

NER with spaCy

spaCy (not run here)
# pip install spacy && python -m spacy download en_core_web_sm
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Infosys will open a new office in Ludhiana on 15 January 2027 "
          "and hire 500 engineers, said Salil Parekh.")
for ent in doc.ents:
    print(ent.text, "->", ent.label_)
Output
Infosys -> ORG
Ludhiana -> GPE
15 January 2027 -> DATE
500 -> CARDINAL
Salil Parekh -> PERSON

Explanation: This is the typical output of spaCy’s small English model; exact results can differ between model versions, and small models make mistakes on unfamiliar Indian names. en_core_web_trf (transformer-based) is more accurate but slower. spacy.displacy.render(doc, style="ent") highlights entities in a notebook.

NER with a Transformer

Hugging Face (not run here)
from transformers import pipeline
ner = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
print(ner("Sundar Pichai visited Google's office in Bengaluru."))

Explanation: aggregation_strategy="simple" joins sub-word pieces back into whole entities. Each result includes the entity group, the text span and a confidence score.

How NER Models Are Trained

NER is a token-classification task. Each token gets a BIO tag: B- (beginning of an entity), I- (inside) or O (outside).

Token Salil Parekh joined Infosys
Tag B-PER I-PER O B-ORG

Models are evaluated with entity-level precision, recall and F1 – an entity counts only if both its span and type are exactly right (see precision and recall).

Custom Entities

  • Rule-based: spaCy EntityRuler for fixed lists (course names, product codes).
  • Train a model: annotate a few hundred examples (Label Studio, Prodigy) and fine-tune spaCy or a BERT model.
  • LLMs: prompt for JSON output, e.g. “Extract course_name, fee and city as JSON” – fast to build, but validate the results.
  • Indian languages: try multilingual models (XLM-RoBERTa) or IndicNLP resources for Hindi and Punjabi.

Applications

  • Resume parsing: names, skills, companies, dates
  • Invoice and KYC document extraction
  • News analytics: which companies are mentioned with which events
  • Search and knowledge graphs
  • Redacting personal data before sharing documents

Series

NLP series: NLP Intro · Tokenizer · Stop Words · Stemming · Lemmatization · Bag of Words & TF-IDF · Word Embeddings · Sentiment Analysis · NER · Topic Modeling · BERT · Spam Project

Interview Questions

Q: What is NER?
A task that locates and classifies named entities such as people, organisations, places and dates in text.

Q: What is BIO tagging?
A labelling scheme marking tokens as the Beginning, Inside or Outside of an entity.

Q: How is NER evaluated?
Entity-level precision, recall and F1 with exact span and type matches.

Practice Exercises

  1. Run spaCy on five news headlines about your city and list the entities found.
  2. Add an EntityRuler pattern for your course names.

Summary

NER extracts people, organisations, places, dates and more. Use spaCy for speed, transformers for accuracy, rules or fine-tuning for custom entities, and evaluate at entity level.

Next: Topic Modeling

About Machine Learning

Check Also

Sentiment Analysis in Python: ML Model and Transformers

Sentiment analysis in Python with a tested TF-IDF and logistic regression model, why small datasets fail, word coefficients and pre-trained Hugging Face models.