warn when an unsupported/unknown key is given to the dependency matcher (#12928)

This commit is contained in:
Sofie Van Landeghem 2023-08-22 09:03:35 +02:00 committed by GitHub
parent 198488ee86
commit 869cc4ab0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -219,6 +219,7 @@ class Warnings(metaclass=ErrorsWithCodes):
W125 = ("The StaticVectors key_attr is no longer used. To set a custom " W125 = ("The StaticVectors key_attr is no longer used. To set a custom "
"key attribute for vectors, configure it through Vectors(attr=) or " "key attribute for vectors, configure it through Vectors(attr=) or "
"'spacy init vectors --attr'") "'spacy init vectors --attr'")
W126 = ("These keys are unsupported: {unsupported}")
class Errors(metaclass=ErrorsWithCodes): class Errors(metaclass=ErrorsWithCodes):

View File

@ -129,6 +129,7 @@ cdef class DependencyMatcher:
else: else:
required_keys = {"RIGHT_ID", "RIGHT_ATTRS", "REL_OP", "LEFT_ID"} required_keys = {"RIGHT_ID", "RIGHT_ATTRS", "REL_OP", "LEFT_ID"}
relation_keys = set(relation.keys()) relation_keys = set(relation.keys())
# Identify required keys that have not been specified
missing = required_keys - relation_keys missing = required_keys - relation_keys
if missing: if missing:
missing_txt = ", ".join(list(missing)) missing_txt = ", ".join(list(missing))
@ -136,6 +137,13 @@ cdef class DependencyMatcher:
required=required_keys, required=required_keys,
missing=missing_txt 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 ( if (
relation["RIGHT_ID"] in visited_nodes relation["RIGHT_ID"] in visited_nodes
or relation["LEFT_ID"] not in visited_nodes or relation["LEFT_ID"] not in visited_nodes

View File

@ -216,6 +216,11 @@ def test_dependency_matcher_pattern_validation(en_vocab):
pattern2 = copy.deepcopy(pattern) pattern2 = copy.deepcopy(pattern)
pattern2[1]["RIGHT_ID"] = "fox" pattern2[1]["RIGHT_ID"] = "fox"
matcher.add("FOUNDED", [pattern2]) 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): def test_dependency_matcher_callback(en_vocab, doc):