Skip to content

Commit 77d698d

Browse files
authored
Fix check for RIGHT_ATTRS in dep matcher (#8807)
* Fix check for RIGHT_ATTRs in dep matcher If a non-anchor node does not have RIGHT_ATTRS, the dep matcher throws an E100, which says that non-anchor nodes must have LEFT_ID, REL_OP, and RIGHT_ID. It specifically does not say RIGHT_ATTRS is required. A blank RIGHT_ATTRS is also valid, and patterns with one will be excepted. While not normal, sometimes a REL_OP is enough to specify a non-anchor node - maybe you just want the head of another node unconditionally, for example. This change just sets RIGHT_ATTRS to {} if not present. Alternatively changing E100 to state RIGHT_ATTRS is required could also be reasonable. * Fix test This test was written on the assumption that if `RIGHT_ATTRS` isn't present an error will be raised. Since the proposed changes make it so an error won't be raised this is no longer necessary. * Revert test, update error message Error message now lists missing keys, and RIGHT_ATTRS is required. * Use list of required keys in error message Also removes unused key param arg.
1 parent 941a591 commit 77d698d

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

spacy/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ class Errors:
356356
E098 = ("Invalid pattern: expected both RIGHT_ID and RIGHT_ATTRS.")
357357
E099 = ("Invalid pattern: the first node of pattern should be an anchor "
358358
"node. The node should only contain RIGHT_ID and RIGHT_ATTRS.")
359-
E100 = ("Nodes other than the anchor node should all contain LEFT_ID, "
360-
"REL_OP and RIGHT_ID.")
359+
E100 = ("Nodes other than the anchor node should all contain {required}, "
360+
"but these are missing: {missing}")
361361
E101 = ("RIGHT_ID should be a new node and LEFT_ID should already have "
362362
"have been declared in previous edges.")
363363
E102 = ("Can't merge non-disjoint spans. '{token}' is already part of "

spacy/matcher/dependencymatcher.pyx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ cdef class DependencyMatcher:
122122
raise ValueError(Errors.E099.format(key=key))
123123
visited_nodes[relation["RIGHT_ID"]] = True
124124
else:
125-
if not(
126-
"RIGHT_ID" in relation
127-
and "RIGHT_ATTRS" in relation
128-
and "REL_OP" in relation
129-
and "LEFT_ID" in relation
130-
):
131-
raise ValueError(Errors.E100.format(key=key))
125+
required_keys = set(
126+
("RIGHT_ID", "RIGHT_ATTRS", "REL_OP", "LEFT_ID")
127+
)
128+
relation_keys = set(relation.keys())
129+
missing = required_keys - relation_keys
130+
if missing:
131+
missing_txt = ", ".join(list(missing))
132+
raise ValueError(Errors.E100.format(
133+
required=required_keys,
134+
missing=missing_txt
135+
))
132136
if (
133137
relation["RIGHT_ID"] in visited_nodes
134138
or relation["LEFT_ID"] not in visited_nodes

0 commit comments

Comments
 (0)