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

Bugfixes/review pr 1709 #1746

Merged
merged 1 commit into from Oct 4, 2022
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
38 changes: 27 additions & 11 deletions src/rubrix/utils/span_utils.py
Expand Up @@ -83,22 +83,38 @@ def validate(self, spans: List[Tuple[str, int, int]]):
Raises:
ValueError: If a span is invalid, or if a span is not aligned with the tokens.
"""
not_valid_spans, misaligned_spans = [], []
not_valid_spans_errors, misaligned_spans_errors = [], []

for span in spans:
if span[2] - span[1] < 1:
not_valid_spans.append(span)
char_start, char_end = span[1], span[2]
if char_end - char_start < 1:
not_valid_spans_errors.append(span)
elif None in (
self._start_to_token_idx.get(span[1]),
self._end_to_token_idx.get(span[2]),
self._start_to_token_idx.get(char_start),
self._end_to_token_idx.get(char_end),
):
misaligned_spans.append(self.text[span[1] : span[2]])
message = f"- [{self.text[char_start:char_end]}] defined in "
if char_start - 5 > 0:
message += "..."
message += self.text[max(char_start - 5, 0) : char_end + 5]
if char_end + 5 < len(self.text):
message += "..."

misaligned_spans_errors.append(message)

if not_valid_spans or misaligned_spans:
if not_valid_spans_errors or misaligned_spans_errors:
message = ""
if not_valid_spans:
message += f"Following entity spans are not valid: {not_valid_spans}\n"
if misaligned_spans:
message += f"The entity spans {misaligned_spans} are not aligned with following tokens: {self.tokens}"
if not_valid_spans_errors:
message += (
f"Following entity spans are not valid: {not_valid_spans_errors}\n"
)

if misaligned_spans_errors:
spans = "\n".join(misaligned_spans_errors)
message += f"Following entity spans are not aligned with provided tokenization\n"
message += f"Spans:\n{spans}\n"
message += f"Tokens:\n{self.tokens}"

raise ValueError(message)

def correct(self, spans: List[Tuple[str, int, int]]) -> List[Tuple[str, int, int]]:
Expand Down
5 changes: 4 additions & 1 deletion tests/client/test_api.py
Expand Up @@ -551,7 +551,10 @@ def test_token_classification_spans(span, valid):
)
else:
with pytest.raises(
ValueError, match="The entity spans \['s'\] are not aligned"
ValueError,
match="Following entity spans are not aligned with provided tokenization\n"
r"Spans:\n- \[s\] defined in Esto es...\n"
r"Tokens:\n\['Esto', 'es', 'una', 'prueba'\]",
):
rb.TokenClassificationRecord(
text=texto,
Expand Down
10 changes: 7 additions & 3 deletions tests/utils/test_span_utils.py
Expand Up @@ -67,7 +67,9 @@ def test_validate_misaligned_spans():
span_utils = SpanUtils("test this.", ["test", "this", "."])
with pytest.raises(
ValueError,
match="The entity spans \['test '\] are not aligned with following tokens: \['test', 'this', '.'\]",
match="Following entity spans are not aligned with provided tokenization\n"
r"Spans:\n- \[test \] defined in test this.\n"
r"Tokens:\n\['test', 'this', '.'\]",
):
span_utils.validate([("mock", 0, 5)])

Expand All @@ -76,8 +78,10 @@ def test_validate_not_valid_and_misaligned_spans():
span_utils = SpanUtils("test this.", ["test", "this", "."])
with pytest.raises(
ValueError,
match="Following entity spans are not valid: \[\('mock', 2, 1\)\]\n"
"The entity spans \['test '\] are not aligned with following tokens: \['test', 'this', '.'\]",
match=r"Following entity spans are not valid: \[\('mock', 2, 1\)\]\n"
"Following entity spans are not aligned with provided tokenization\n"
r"Spans:\n- \[test \] defined in test this.\n"
r"Tokens:\n\['test', 'this', '.'\]",
):
span_utils.validate([("mock", 2, 1), ("mock", 0, 5)])

Expand Down