Skip to content

Hooks Return Predicates

melisa-qordoba edited this page Sep 23, 2020 · 1 revision

Hooks Return Predicates

To be a match hook, a Python function must take 1 or 0 arguments, and return a predicate (function which returns a boolean) with inputs (doc, start, end). If you read about the spaCy Matcher, you will understand why the arguments are doc, start, end. The reason match hooks RETURN a predicate, rather than BEING a predicate is for flexibility. It allows us to have preceded_by_pos instead of preceded_by_noun, preceded_by_adj, etc.

The structure of a match hook is:

  • name, the name of the Python function
  • (optional) args - the argument of the function. Yes, argument, singular - match hooks take one or zero arguments. If you need more than one argument, have the hook accept a dict or list.
  • match_if_predicate_is - a boolean which flips the behavior from "if this predicate is true, then match" or "if this predicate is false, then match". This is just to make naming functions easier. For example, we have preceded_by_pos as a hook, with arg: "NOUN", and match_if_predicate_is set to true. This hook is much more sensible than not_preceded_by_pos, with args [every, pos, but, NOUN].

To use your own match hooks, instantiate the replace matcher with a module containing them, e.g.

    from replacy import ReplaceMatcher
    from replacy.db import load_json
    import spacy

    # import the module with your hooks
    # the name doesn't matter, it just needs to be a python module
    import my.custom_hooks as ch


    nlp = spacy.load("en_core_web_sm")
    rmatch_dict = load_json("./resources/match_dict.json")
    # pass replaCy your custom hooks here, and then they are usable in your match_dict.json
    rmatcher = ReplaceMatcher(nlp, rmatch_dict, custom_match_hooks=ch)
    span = rmatcher("She excepts her fate.")[0]
    span._.suggestions
    # >>> ['acccepts']
Clone this wiki locally