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?