Paul
Paul

Reputation: 1757

Using spacy visualizer with custom data

I want to visualize a sentence using Spacy's named entity visualizer. I have a sentence with some user defined labels over the tokens, and I want to visualize them using the NER rendering API.

I don't want to train and produce a predictive model, I have all needed labels from an external source, just need the visualization without messing too much with front-end libraries.

Any ideas how?
Thank you

Upvotes: 1

Views: 778

Answers (1)

aab
aab

Reputation: 11484

You can manually modify the list of entities (doc.ents) and add new spans using token offsets. Be aware that entities can't overlap at all.

import spacy
from spacy.tokens import Span
nlp = spacy.load('en', disable=['ner'])
doc = nlp("I see an XYZ.")
doc.ents = list(doc.ents) + [Span(doc, 3, 4, "NEWENTITYTYPE")]
print(doc.ents[0], doc.ents[0].label_)

Output:

XYZ NEWENTITYTYPE

Upvotes: 2

Related Questions