Fix swapped hypothesis and reference in TER - #3479
Open
Kayvan-Zahiri wants to merge 2 commits into
Open
Conversation
_compute_sentence_statistics called _translation_edit_rate(tgt_words, pred_words), but that function treats its second argument as the reference: it builds _LevenshteinEditDistance(reference_tokens=...) from it and shifts the first argument's words against it. Levenshtein distance is symmetric so most scores were unaffected, but TER's shift search is not, and the empty-input guard fired on the prediction, so an empty hypothesis scored 0.0, a perfect TER.
Kayvan-Zahiri
requested review from
SkafteNicki and
justusschock
as code owners
August 27, 2026 19:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #3478
_compute_sentence_statisticscalled_translation_edit_rate(tgt_words, pred_words), but that function is declared(pred_words, target_words)and treats argument two as the reference: it builds_LevenshteinEditDistance(target_words)(parameter namereference_tokens) and shifts argument one against it. So TER was computed in the wrong direction.Levenshtein distance is symmetric, so most scores were unaffected and the existing tests passed. Two things are not symmetric:
hello hello the a dogagainstjumps dog lazy thescored 1.0; sacrebleu gives 1.25.if len(target_words) == 0: return tensor(0.0)was testing the prediction, so a system that transcribed nothing scored 0.0, the best possible TER.The fix is the argument order alone.
Verification
sacrebleu is the reference implementation named in the metric's docstring, and
_reference_sacrebleu_terin the existing tests already uses it as ground truth.unittests/text/test_ter.py: 26 passed, 6 skipped. All four pre-existing empty-input tests pass unchanged. Both new tests fail on master withassert tensor(0.) == tensor(1.)andassert tensor(1.) == tensor(1.2500).Not included, deliberately
An empty reference (
[[""]]) returnstensor(0.)where sacrebleu returns 1.0, and_compute_ter_score_from_statistics's docstring says "1 if reference_length == 0". That comes from thenum_edits > 0conditions in that function rather than the argument order; correcting the order just moves which input reaches it. I left it out to keep this a one-line correctness fix, and can send it separately if you want it changed.I have not added a CHANGELOG entry yet since the number was not known; happy to push one.