How to access span custom attributes with a variable name

Question:

I am using spacy to categorize custom spans in documents.
Then I create custom extension on the spans for every type of span.

The example of the documentation is:

from spacy.tokens import Span
city_getter = lambda span: any(city in span.text for city in ("New York", "Paris", "Berlin"))
Span.set_extension("has_city", getter=city_getter)
doc = nlp("I like New York in Autumn")
assert doc[1:4]._.has_city

Imagine I have several custom extensions and I need to access the extension with a variable name content.

def dostuff(name_of_extension):
    *pseudocode:
    Loop over all the spans of the doc and see if the have the extension "name_of_extension" and extract some info
    return info


dostuff(name_of_extension="has_city")

The idea is being able to use the same method for different extensions.

The more general idea here is how to access custom define attributes in spacy

doc[1:4]._.X

Where X is a variable. You can imagine that I want to see if the particular span has_city or is_in_europe or is_on_the_cost or whatever else.

Spacy Span API: https://spacy.io/api/token#attributes

Asked By: JFerro

||

Answers:

This is a generic python question about how to get the value of an attribute by name:

getattr(doc[1:4]._, "has_city")
Answered By: aab
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.