Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `Metric` ignoring an active `torch.device` context manager on torch 2.3-2.7 ([#3448](https://github.com/Lightning-AI/torchmetrics/pull/3448))


- Fixed swapped hypothesis and reference arguments in `TER`, which made an empty hypothesis score a perfect 0.0 ([#3479](https://github.com/Lightning-AI/torchmetrics/pull/3479))


---

## [1.9.0] - 2026-03-05
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/functional/text/ter.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def _compute_sentence_statistics(pred_words: list[str], target_words: list[list[
best_num_edits = tensor(2e16)

for tgt_words in target_words:
num_edits = _translation_edit_rate(tgt_words, pred_words)
num_edits = _translation_edit_rate(pred_words, tgt_words)
tgt_lengths += len(tgt_words)
if num_edits < best_num_edits:
best_num_edits = num_edits
Expand Down
19 changes: 19 additions & 0 deletions tests/unittests/text/test_ter.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,22 @@ def test_ter_return_sentence_level_class():
targets = _inputs_single_sentence_multiple_references.target
_, sentence_ter = ter_metric(preds, targets)
isinstance(sentence_ter, Tensor)


def test_ter_empty_hypothesis_is_not_a_perfect_score():
"""Test that a hypothesis producing nothing is charged one deletion per reference word."""
assert translation_edit_rate([""], [["hello world foo"]]) == tensor(1.0)


def test_ter_matches_reference_when_shifts_are_asymmetric():
"""Test that edits are counted from hypothesis to reference, not the other way around.

The shift search is not symmetric, so swapping the two sides changes the number of edits found.

"""
preds = ["hello hello the a dog"]
targets = [["jumps dog lazy the"]]
expected = _reference_sacrebleu_ter(
preds, targets, normalized=False, no_punct=False, asian_support=False, case_sensitive=True
)
assert translation_edit_rate(preds, targets, lowercase=False) == expected