Skip to content

Using custom Token extensions in spaCy's Matcher #5818

Discussion options

You must be logged in to vote

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 than method and to use a hard-coded dependency label, so you have a value that can be used in the Matcher pattern:

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}}])

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat / matcher Feature: Token, phrase and dependency matcher feat / doc Feature: Doc, Span and Token objects
3 participants
Converted from issue

This discussion was converted from issue #5818 on December 11, 2020 00:08.