I'm trying to create a method to use on_match in DependencyMatcher. I've done this successfully for Matcher and follow much of the same logic.
When I get the matches using something like this:
matches = dep_matcher(doc)
for match in matches:
print(match)
If I understand correctly match is a tuple with match_id (the rule that was triggered) and a list of nodes that match my pattern.
When defining an on_match method, I would expect matches[i] to return the same tuple, but that is not the case. Am I missing something??
I created the following script to help illustrate the problem
from spacy.matcher import DependencyMatcher
from spacy import load
def my_on_match(matcher, doc, i, matches):
print('inside my_on_match')
match = matches[i]
print(match)
print('================')
nlp = load('en_core_web_sm')
doc = nlp('The quick brown fox jumped over the lazy dog.')
dep_matcher = DependencyMatcher(nlp.vocab)
dep_pattern = [
{"SPEC": {"NODE_NAME": "jumped"}, "PATTERN": {"ORTH": "jumped"}},
{"SPEC": {"NODE_NAME": "fox", "NBOR_RELOP": ">", "NBOR_NAME": "jumped"}, "PATTERN": {"ORTH": "fox"}},
{"SPEC": {"NODE_NAME": "quick", "NBOR_RELOP": ".", "NBOR_NAME": "jumped"}, "PATTERN": {"ORTH": "fox"}},
]
dep_matcher.add('no_on_match', None, dep_pattern)
dep_matcher.add('with_on_match', my_on_match, dep_pattern)
matches = dep_matcher(doc)
print('all matches')
for match in matches:
print(match)
I was reading the code for DependencyMatcher and found these lines on dependencymatcher.pyx (start at line 240 on master branch and line 237 for tag 2.1.8):
for i, (ent_id, nodes) in enumerate(matched_key_trees):
on_match = self._callbacks.get(ent_id)
if on_match is not None:
on_match(self, doc, i, matches)
So, if my line of thought is correct, on the last line of the excerpt (243 on master branch), should it be matched_key_trees instead of matches?
I'd appreciate any light on this issue. :-)
That looks like the correct analysis! (We definitely need to add documentation and more tests for this feature.)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
That looks like the correct analysis! (We definitely need to add documentation and more tests for this feature.)