use custom NER webservice to set_entities in doc #9535
-
Hi, I have a webservice for NER that gives me: How can i use doc.set_ents to set the entity_label from my webservice to tokens in spacy document? I want to do this so that i can access them with token.ent_type_ for my downstream tasks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @aakashb95 ! You can use import spacy
data = [
("John Snow", "PERSON", (0, 9)), # last character after the span
]
nlp = spacy.blank("en")
for text, label, (start, end) in data:
doc = nlp(text)
span = doc.char_span(start, end, label=label)
doc.ents = [span] Usually, you'd have multiple entities in a given document, that's why we pass a list to |
Beta Was this translation helpful? Give feedback.
-
I just checked this discussion: How can i incorporate this for my example? |
Beta Was this translation helpful? Give feedback.
Hi @aakashb95 !
You can use
char_span
so that you can slice through character indices.Also, the character offsets are incorrect: for
start
it should be the index of the first character, but forend
, it must be the last character after the span:Usually, you'd have multiple entities in a given document, that's why we pass a list to
doc.ents
. You can adapt the snippet above to fit your use-case 👍 Also, you can check thechar_span
docs for mo…