In the example from the documentation https://spacy.io/api/phrasematcher
the callback function is not executed:
import spacy
nlp = spacy.load("en_core_web_sm")
def on_match(matcher, doc, id, matches):
print('Matched!', matches)
matcher = spacy.matcher.PhraseMatcher(nlp.vocab)
matcher.add("OBAMA", on_match, nlp("Barack Obama"))
matcher.add("HEALTH", on_match, nlp("health care reform"),
nlp("healthcare reform"))
doc = nlp("Barack Obama urges Congress to find courage to defend his healthcare reforms")
matches = matcher(doc)
for match in matches:
print("Match: ", match, nlp.vocab.strings[match[0]])
And another example:
import spacy
def on_match(matcher, doc, id, matches):
print('Callback executes on matches!', matches)
nlp = spacy.load("en_core_web_sm")
matcher = spacy.matcher.PhraseMatcher(nlp.vocab)
matcher.add("OBAMA", on_match, nlp("Barack Obama"))
matcher.add("HEALTH", on_match, nlp("health care reform"), nlp("healthcare reform"))
doc = nlp("Barack Obama urges Congress to find courage to defend his healthcare reform")
print("Start matching. Print in on_match should come after this.")
matches = matcher(doc)
for match in matches:
print("Match: ", match, nlp.vocab.strings[match[0]])
Thanks for the report! This is a bug we should have caught before releasing 2.2. I'll fix it and update the test suite so similar issues are detected better in the future.
(You can always edit the issue instead of opening a new one, it's no problem!)
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
Thanks for the report! This is a bug we should have caught before releasing 2.2. I'll fix it and update the test suite so similar issues are detected better in the future.
(You can always edit the issue instead of opening a new one, it's no problem!)