Articles → NLP → Add A New Named Entity In Spacy
Add A New Named Entity In Spacy
Example
import spacy
from spacy.tokens import Span
nlp = spacy.load("en_core_web_sm")
doc = nlp("abc is my city name")
# Remove previous entities from doc.ents
new_ents = [ent for ent in doc.ents if not (ent.start == 0 and ent.end == 1)]
doc.ents = new_ents
# Add custom label to vocab
nlp.vocab.strings.add("CITY")
# Create new entity
new_entity = Span(doc, 0, 1, label="CITY")
doc.ents = list(doc.ents) + [new_entity]
for ent in doc.ents:
print(ent.text, ent.label_)
Output
| Posted By - | Karan Gupta |
| |
| Posted On - | Monday, December 29, 2025 |