How to set an extension attribute to a Doc object in spaCy so that it can be retrieved from a slice (Span) of the Doc?

Viewed 325

I want to add an extension attribute to a spaCy doc that spans one or more tokens, similar to the entity attribute, so that it could also be accessed when looking at a span which contains that attribute. To clarify, below I set a list containing a Span to doc.ents. Then, if I only take a slice of the doc (containing the added entity), I can still find the added entity.

import spacy

nlp = spacy.load("en_core_web_sm")

doc = nlp("This is some country. Another sentence")

doc.ents = [Span(doc, 2, 4, "GPE")] #doc[2:4] = "some country"
print(doc[1:6].ents)
#[some country]

However, that is not the case with an extension attribute:

Doc.set_extension('my_extension', default=None)
Span.set_extension('my_extension', default=None)

doc._.my_extension = [Span(doc, 2, 4, "GPE")]
print(doc[1:6]._.my_extension)
#None

What do I need to do with the extension so that it behaves like the entity property?

1 Answers

The reason you can call .ents on a span and get a result is that the value there is reconstructed from values on the tokens. If you want similar behavior for a custom extension, you'll need to create a span extension that sets token attributes, and when being read uses token attributes to calculate the return value.

Related