Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn for unsupported key in dependency matcher #12928

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions spacy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class Warnings(metaclass=ErrorsWithCodes):
W125 = ("The StaticVectors key_attr is no longer used. To set a custom "
"key attribute for vectors, configure it through Vectors(attr=) or "
"'spacy init vectors --attr'")
W126 = ("These keys are unsupported: {unsupported}")


class Errors(metaclass=ErrorsWithCodes):
Expand Down
8 changes: 8 additions & 0 deletions spacy/matcher/dependencymatcher.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,21 @@ cdef class DependencyMatcher:
else:
required_keys = {"RIGHT_ID", "RIGHT_ATTRS", "REL_OP", "LEFT_ID"}
relation_keys = set(relation.keys())
# Identify required keys that have not been specified
missing = required_keys - relation_keys
if missing:
missing_txt = ", ".join(list(missing))
raise ValueError(Errors.E100.format(
required=required_keys,
missing=missing_txt
))
# Identify additional, unsupported keys
unsupported = relation_keys - required_keys
if unsupported:
unsupported_txt = ", ".join(list(unsupported))
warnings.warn(Warnings.W126.format(
unsupported=unsupported_txt
))
if (
relation["RIGHT_ID"] in visited_nodes
or relation["LEFT_ID"] not in visited_nodes
Expand Down
5 changes: 5 additions & 0 deletions spacy/tests/matcher/test_dependency_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def test_dependency_matcher_pattern_validation(en_vocab):
pattern2 = copy.deepcopy(pattern)
pattern2[1]["RIGHT_ID"] = "fox"
matcher.add("FOUNDED", [pattern2])
# invalid key
with pytest.warns(UserWarning):
pattern2 = copy.deepcopy(pattern)
pattern2[1]["FOO"] = "BAR"
matcher.add("FOUNDED", [pattern2])


def test_dependency_matcher_callback(en_vocab, doc):
Expand Down