Skip to content
NLP

Named Entity Recognition

Extract entities with spaCy and transformers.

By EZ4Code Team
nerspacytransformers

Code

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple was founded by Steve Jobs in California in 1976 for $1.")

for ent in doc.ents:
    print(ent.text, ent.label_, ent.start_char, ent.end_char)

# Visualize entities (in Jupyter)
# from spacy import displacy
# displacy.render(doc, style="ent")

# HuggingFace pipeline for NER
from transformers import pipeline
ner_pipe = pipeline("ner", aggregation_strategy="simple",
                    model="dslim/bert-base-NER")
results = ner_pipe("Tim Cook is the CEO of Apple in Cupertino.")
for r in results:
    print(r["entity_group"], r["word"], round(r["score"], 3))

# Add a custom EntityRuler for domain terms
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns([{"label": "PRODUCT", "pattern": "iPhone"}])

Explanation

NER identifies spans of text as entities such as persons, organizations, dates, and locations. spaCy ships a built-in NER component accessible through doc.ents, while a HuggingFace token-classification pipeline offers transformer accuracy. The EntityRuler lets you add domain-specific patterns on top of the statistical model.

More NLP Snippets