Back to Spacy

DependencyMatcher

website/docs/api/dependencymatcher.mdx

4.0.0.dev1013.4 KB
Original Source

The DependencyMatcher follows the same API as the Matcher and PhraseMatcher and lets you match on dependency trees using Semgrex operators. It requires a pretrained DependencyParser or other component that sets the Token.dep and Token.head attributes. See the usage guide for examples.

Pattern format {id="patterns"}

python
### Example
# pattern: "[subject] ... initially founded"
[
  # anchor token: founded
  {
    "RIGHT_ID": "founded",
    "RIGHT_ATTRS": {"ORTH": "founded"}
  },
  # founded -> subject
  {
    "LEFT_ID": "founded",
    "REL_OP": ">",
    "RIGHT_ID": "subject",
    "RIGHT_ATTRS": {"DEP": "nsubj"}
  },
  # "founded" follows "initially"
  {
    "LEFT_ID": "founded",
    "REL_OP": ";",
    "RIGHT_ID": "initially",
    "RIGHT_ATTRS": {"ORTH": "initially"}
  }
]

A pattern added to the DependencyMatcher consists of a list of dictionaries, with each dictionary describing a token to match. Except for the first dictionary, which defines an anchor token using only RIGHT_ID and RIGHT_ATTRS, each pattern should have the following keys:

NameDescription
LEFT_IDThe name of the left-hand node in the relation, which has been defined in an earlier node. str
REL_OPAn operator that describes how the two nodes are related. str
RIGHT_IDA unique name for the right-hand node in the relation. str
RIGHT_ATTRSThe token attributes to match for the right-hand node in the same format as patterns provided to the regular token-based Matcher. Dict[str, Any]
<Infobox title="Designing dependency matcher patterns" emoji="📖">

For examples of how to construct dependency matcher patterns for different types of relations, see the usage guide on dependency matching.

</Infobox>

Operators {id="operators"}

The following operators are supported by the DependencyMatcher, most of which come directly from Semgrex:

SymbolDescription
A < BA is the immediate dependent of B.
A > BA is the immediate head of B.
A << BA is the dependent in a chain to B following dep → head paths.
A >> BA is the head in a chain to B following head → dep paths.
A . BA immediately precedes B, i.e. A.i == B.i - 1, and both are within the same dependency tree.
A .* BA precedes B, i.e. A.i < B.i, and both are within the same dependency tree (Semgrex counterpart: ..).
A ; BA immediately follows B, i.e. A.i == B.i + 1, and both are within the same dependency tree (Semgrex counterpart: -).
A ;* BA follows B, i.e. A.i > B.i, and both are within the same dependency tree (Semgrex counterpart: --).
A $+ BB is a right immediate sibling of A, i.e. A and B have the same parent and A.i == B.i - 1.
A $- BB is a left immediate sibling of A, i.e. A and B have the same parent and A.i == B.i + 1.
A $++ BB is a right sibling of A, i.e. A and B have the same parent and A.i < B.i.
A $-- BB is a left sibling of A, i.e. A and B have the same parent and A.i > B.i.
A >+ B <Tag variant="new">3.5.1</Tag>B is a right immediate child of A, i.e. A is a parent of B and A.i == B.i - 1 (not in Semgrex).
A >- B <Tag variant="new">3.5.1</Tag>B is a left immediate child of A, i.e. A is a parent of B and A.i == B.i + 1 (not in Semgrex).
A >++ BB is a right child of A, i.e. A is a parent of B and A.i < B.i.
A >-- BB is a left child of A, i.e. A is a parent of B and A.i > B.i.
A <+ B <Tag variant="new">3.5.1</Tag>B is a right immediate parent of A, i.e. A is a child of B and A.i == B.i - 1 (not in Semgrex).
A <- B <Tag variant="new">3.5.1</Tag>B is a left immediate parent of A, i.e. A is a child of B and A.i == B.i + 1 (not in Semgrex).
A <++ BB is a right parent of A, i.e. A is a child of B and A.i < B.i.
A <-- BB is a left parent of A, i.e. A is a child of B and A.i > B.i.

DependencyMatcher.__init__ {id="init",tag="method"}

Create a DependencyMatcher.

Example

python
from spacy.matcher import DependencyMatcher
matcher = DependencyMatcher(nlp.vocab)
NameDescription
vocabThe vocabulary object, which must be shared with the documents the matcher will operate on. Vocab
keyword-only
validateValidate all patterns added to this matcher. bool

DependencyMatcher.__call__ {id="call",tag="method"}

Find all tokens matching the supplied patterns on the Doc or Span.

Example

python
from spacy.matcher import DependencyMatcher

matcher = DependencyMatcher(nlp.vocab)
pattern = [{"RIGHT_ID": "founded_id",
  "RIGHT_ATTRS": {"ORTH": "founded"}}]
matcher.add("FOUNDED", [pattern])
doc = nlp("Bill Gates founded Microsoft.")
matches = matcher(doc)
NameDescription
doclikeThe Doc or Span to match over. Union[Doc, Span]
RETURNSA list of (match_id, token_ids) tuples, describing the matches. The match_id is the ID of the match pattern and token_ids is a list of token indices matched by the pattern, where the position of each token in the list corresponds to the position of the node specification in the pattern. List[Tuple[int, List[int]]]

DependencyMatcher.__len__ {id="len",tag="method"}

Get the number of rules added to the dependency matcher. Note that this only returns the number of rules (identical with the number of IDs), not the number of individual patterns.

Example

python
matcher = DependencyMatcher(nlp.vocab)
assert len(matcher) == 0
pattern = [{"RIGHT_ID": "founded_id",
  "RIGHT_ATTRS": {"ORTH": "founded"}}]
matcher.add("FOUNDED", [pattern])
assert len(matcher) == 1
NameDescription
RETURNSThe number of rules. int

DependencyMatcher.__contains__ {id="contains",tag="method"}

Check whether the matcher contains rules for a match ID.

Example

python
matcher = DependencyMatcher(nlp.vocab)
assert "FOUNDED" not in matcher
matcher.add("FOUNDED", [pattern])
assert "FOUNDED" in matcher
NameDescription
keyThe match ID. str
RETURNSWhether the matcher contains rules for this match ID. bool

DependencyMatcher.add {id="add",tag="method"}

Add a rule to the matcher, consisting of an ID key, one or more patterns, and an optional callback function to act on the matches. The callback function will receive the arguments matcher, doc, i and matches. If a pattern already exists for the given ID, the patterns will be extended. An on_match callback will be overwritten.

Example

python
def on_match(matcher, doc, id, matches):
    print('Matched!', matches)

matcher = DependencyMatcher(nlp.vocab)
matcher.add("FOUNDED", patterns, on_match=on_match)
NameDescription
match_idAn ID for the patterns. str
patternsA list of match patterns. A pattern consists of a list of dicts, where each dict describes a token in the tree. List[List[Dict[str, Union[str, Dict]]]]
keyword-only
on_matchCallback function to act on matches. Takes the arguments matcher, doc, i and matches. Optional[Callable[[DependencyMatcher, Doc, int, List[Tuple], Any]]

DependencyMatcher.get {id="get",tag="method"}

Retrieve the pattern stored for a key. Returns the rule as an (on_match, patterns) tuple containing the callback and available patterns.

Example

python
matcher.add("FOUNDED", patterns, on_match=on_match)
on_match, patterns = matcher.get("FOUNDED")
NameDescription
keyThe ID of the match rule. str
RETURNSThe rule, as an (on_match, patterns) tuple. Tuple[Optional[Callable], List[List[Union[Dict, Tuple]]]]

DependencyMatcher.remove {id="remove",tag="method"}

Remove a rule from the dependency matcher. A KeyError is raised if the match ID does not exist.

Example

python
matcher.add("FOUNDED", patterns)
assert "FOUNDED" in matcher
matcher.remove("FOUNDED")
assert "FOUNDED" not in matcher
NameDescription
keyThe ID of the match rule. str