Spacy: PhraseMatcher does not execute callback function on matches

Created on 8 Oct 2019  路  2Comments  路  Source: explosion/spaCy

How to reproduce the behaviour

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

My Environment

  • Operating System: MacOS 10.13.3
  • Python Version Used: 3.7.0
  • spaCy Version Used: 2.2.1
  • Environment Information:
bug feat / matcher

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!)

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TropComplique picture TropComplique  路  3Comments

notnami picture notnami  路  3Comments

smartinsightsfromdata picture smartinsightsfromdata  路  3Comments

melanietosik picture melanietosik  路  3Comments

nadachaabani1 picture nadachaabani1  路  3Comments