Using custom Token extensions in spaCy's Matcher #5818
-
I just added the following extension to
So, I want to check if a token has a certain specified dependency name as one of its children, so the following:
Outputs However, I don't understand how to use this extension with spaCy's Matcher. Below is what I've written. The output I expect is
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can try retokenizing and reaching to the parent instead:
|
Beta Was this translation helpful? Give feedback.
-
I think the answer provided here is the closest working solution: https://stackoverflow.com/a/63105760/461847 The relevant parts are copied for reference below, where the key points are to use import spacy
from spacy.matcher import Matcher
from spacy.tokens import Token
has_dep = lambda token: 'nsubj' in [child.dep_ for child in token.children]
Token.set_extension('HAS_DEP_NSUBJ', getter=has_dep, force=True)
nlp = spacy.load("en_core_web_md")
matcher = Matcher(nlp.vocab)
matcher.add("depnsubj", None, [{"_": {"HAS_DEP_NSUBJ": True}}]) |
Beta Was this translation helpful? Give feedback.
I think the answer provided here is the closest working solution: https://stackoverflow.com/a/63105760/461847
The relevant parts are copied for reference below, where the key points are to use
getter
rather thanmethod
and to use a hard-coded dependency label, so you have a value that can be used in theMatcher
pattern: