Code
nlp
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"}])