fix(i18n): repair the three defects a translation reliably comes back with

Measured over the entries no rule reported, a machine translation loses the
sentence's full stop in 12417 cases, the capital of its opening word in 2139,
and opens a run of spaces around a protected span in 610. None of them touches
the meaning, so the answer is worth keeping and worth repairing rather than
discarding.

The full stop goes missing when a closing bracket moves in front of a protected
span and takes the punctuation behind it along, which leaves the sentence
running into the next one. resegment already puts back a boundary that went
missing mid-text; terminate does the same at the end.

collapse leaves a run the source carries itself alone, because an aligned block
in a code sample means its spacing.

Truncation cannot be repaired, only reported, and joins the damaged set with a
floor of 120 characters: `Situation` becomes `Lage` and halves its length while
saying the same thing, so a shorter entry proves nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-24 22:42:37 +02:00
1 parent 2aa7325eca
commit da4dba4618
3 files changed
+190

No files matched your search

@@ -0,0 +1,122 @@
"""Unit tests for the repairs a restored translation goes through.
Every case is a shape measured in the catalogs: a bracket that moved in front of
a protected span and took the full stop with it, an opening word the translator
lower-cased, and a run of spaces it opened around a span.
"""
from __future__ import annotations
import unittest
from utils.i18n.placeholders import collapse, mask, recapitalise, terminate, unmask
from utils.i18n.translate import truncated
def _round_trip(source: str, translated: str) -> str | None:
"""Send ``translated`` back through the unmasking a real answer goes through.
Args:
source: the source message.
translated: what the translator returned for the masked request.
"""
return unmask(translated, mask(source), source)
class TestCollapse(unittest.TestCase):
def test_a_run_the_source_lacks_is_closed(self) -> None:
self.assertEqual(
collapse("Works with the helper", "Works with it"), "Works with the helper"
)
def test_a_run_the_source_carries_is_left_alone(self) -> None:
source = "column aligned"
self.assertEqual(
collapse("Spalte ausgerichtet", source), "Spalte ausgerichtet"
)
def test_a_tab_in_the_source_disables_the_repair(self) -> None:
self.assertEqual(collapse("a b", "a\tb"), "a b")
class TestTerminate(unittest.TestCase):
def test_a_lost_full_stop_comes_back(self) -> None:
source = "Return whether the paths match (as os.path.samefile() does)."
self.assertEqual(
terminate("Ob die Pfade gleich sind (wie) os.path.samefile()", source),
"Ob die Pfade gleich sind (wie) os.path.samefile().",
)
def test_a_question_mark_comes_back(self) -> None:
self.assertEqual(terminate("Ist es bereit", "Is it ready?"), "Ist es bereit?")
def test_a_translation_that_kept_one_is_untouched(self) -> None:
self.assertEqual(terminate("Fertig!", "Done."), "Fertig!")
def test_a_source_without_one_adds_nothing(self) -> None:
self.assertEqual(terminate("Backup", "Backup"), "Backup")
class TestRecapitalise(unittest.TestCase):
def test_a_lower_cased_opening_is_restored(self) -> None:
self.assertEqual(
recapitalise("alle `alpha` Kriterien.", "All `alpha` criteria."),
"Alle `alpha` Kriterien.",
)
def test_a_lowercase_source_keeps_its_case(self) -> None:
self.assertEqual(
recapitalise("alle Kriterien", "all criteria"), "alle Kriterien"
)
def test_an_already_capital_opening_is_untouched(self) -> None:
self.assertEqual(
recapitalise("Alle Kriterien", "All criteria"), "Alle Kriterien"
)
def test_an_opening_protected_span_is_untouched(self) -> None:
self.assertEqual(
recapitalise("`alpha` zuerst", "`alpha` first"), "`alpha` zuerst"
)
class TestUnmaskAppliesTheRepairs(unittest.TestCase):
def test_a_lower_cased_opening_is_repaired_on_the_way_back(self) -> None:
restored = _round_trip(
"All `alpha` criteria.", 'alle <x id="0"></x> Kriterien.'
)
self.assertEqual(restored, "Alle `alpha` Kriterien.")
def test_a_missing_full_stop_is_repaired_on_the_way_back(self) -> None:
restored = _round_trip("All `alpha` criteria.", 'Alle <x id="0"></x> Kriterien')
self.assertEqual(restored, "Alle `alpha` Kriterien.")
def test_a_space_run_is_repaired_on_the_way_back(self) -> None:
restored = _round_trip(
"All `alpha` criteria.", 'Alle <x id="0"></x> Kriterien.'
)
self.assertEqual(restored, "Alle `alpha` Kriterien.")
class TestTruncated(unittest.TestCase):
def test_a_short_entry_may_shrink(self) -> None:
self.assertFalse(truncated("Situation", "Lage"))
def test_a_long_passage_that_halved_is_reported(self) -> None:
source = "A" * 200
self.assertTrue(truncated(source, "B" * 80))
def test_a_long_passage_that_kept_its_body_is_not(self) -> None:
source = "A" * 200
self.assertFalse(truncated(source, "B" * 180))
if __name__ == "__main__":
unittest.main()