diff --git a/tests/unit/python/utils/i18n/test_repairs.py b/tests/unit/python/utils/i18n/test_repairs.py index 09aeec97d6..7534db9060 100644 --- a/tests/unit/python/utils/i18n/test_repairs.py +++ b/tests/unit/python/utils/i18n/test_repairs.py @@ -222,6 +222,32 @@ class TestWriterAndGateAgree(unittest.TestCase): "count as translated and never be retried", ) + def test_a_stuttered_word_is_reported(self) -> None: + self.assertTrue(harms("Cloud", "Cloud Cloud Cloud")) + self.assertTrue( + harms("Marketing for teams", "Marketing Marketing Marketing für Teams") + ) + + def test_a_word_the_source_repeats_is_not_a_stutter(self) -> None: + source = "very very very slow" + + self.assertFalse(harms(source, source.replace("slow", "langsam"))) + + def test_a_non_latin_catalog_that_came_back_in_english_is_reported(self) -> None: + english = "Core NGINX webserver with stream support and tuned defaults" + + self.assertTrue(harms(english, english.replace("tuned", "tunned"), "ar")) + self.assertFalse( + harms(english, english.replace("tuned", "tunned"), "de"), + "a latin-script target needs a language detector, which does not exist here", + ) + + def test_a_non_latin_translation_keeping_a_few_terms_is_not_reported(self) -> None: + source = "Core NGINX webserver with HTTP stream support and tuned defaults" + arabic = "خادم NGINX الأساسي مع دعم HTTP وإعدادات افتراضية مضبوطة للأداء" + + self.assertFalse(harms(source, arabic, "ar")) + def test_every_criterion_of_the_gate_reaches_the_client(self) -> None: named = "Deploy web-app-docs before the documentation site answers anything." for label, source, translation in ( diff --git a/utils/i18n/client.py b/utils/i18n/client.py index da44f984db..3b55387e80 100644 --- a/utils/i18n/client.py +++ b/utils/i18n/client.py @@ -158,7 +158,7 @@ class LibreTranslate: return Outcome([None], refused, 0, refusal) return merge(self._batch([text], target) for text in texts) values = [ - unmask(translated, item, text) + unmask(translated, item, text, target) for translated, item, text in zip(result, masked, texts, strict=True) ] damaged = sum(1 for text in values if text is None) diff --git a/utils/i18n/placeholders.py b/utils/i18n/placeholders.py index d64f1c942f..13f199c99f 100644 --- a/utils/i18n/placeholders.py +++ b/utils/i18n/placeholders.py @@ -80,6 +80,18 @@ STRUCTURE = "[]{}`*()" TRUNCATION_FLOOR = 120 TRUNCATION_RATIO = 0.5 ECHO_FLOOR = 30 +STUTTER = re.compile(r"\b(\w+)(?:\s+\1\b){2,}", re.IGNORECASE | re.UNICODE) +WORDS = re.compile(r"[^\W\d_]{2,}", re.UNICODE) +LATIN_FLOOR = 6 +LATIN_SHARE = 0.9 +NON_LATIN_SCRIPTS = frozenset( + { + "am", "ar", "be", "bg", "bn", "dv", "dz", "el", "fa", "gu", "he", "hi", + "hy", "ja", "ka", "km", "kn", "ko", "ky", "lo", "mk", "ml", "mn", "mr", + "my", "ne", "or", "pa", "ps", "ru", "si", "sr", "ta", "te", "th", "ti", + "ug", "uk", "ur", "yi", "zh", + } +) EMPHASIS = re.compile(r"(\*\*)[ \t]*([^\s]|[^\s].*?[^\s])[ \t]*\1", re.DOTALL) @@ -207,6 +219,43 @@ def missing_names(source: str, translation: str) -> set[str]: return {name for name in carried if name not in translation} +def untranslated(translation: str, language: str) -> bool: + """Return whether a non-Latin language came back written in Latin letters. + + The server answers with its input, lightly reworded, often enough that the + result keeps every span and every markup character while still being + English. Comparing scripts catches it wherever the target does not use the + Latin one; for a Latin-script target it would need a language detector. + + Args: + translation: what came back for the entry. + language: ISO 639-1 code of the catalog being filled. + """ + if language not in NON_LATIN_SCRIPTS: + return False + words = WORDS.findall(prose(translation)) + if len(words) < LATIN_FLOOR: + return False + latin = sum(1 for word in words if word.isascii()) + return latin / len(words) >= LATIN_SHARE + + +def stutters(source: str, translation: str) -> bool: + """Return whether ``translation`` repeats a word the source does not. + + A server that loses its way answers ``Cloud`` with ``Cloud Cloud Cloud``. + Every other rule passes it: the spans survive, the markup count survives, + it is no echo, and ``truncated`` only watches the short side. + + Args: + source: the source message. + translation: what came back for it. + """ + return bool(STUTTER.search(prose(translation))) and not STUTTER.search( + prose(source) + ) + + def truncated(source: str, translation: str) -> bool: """Return whether ``translation`` kept too little of ``source`` to be one. @@ -233,7 +282,7 @@ def structure(text: str) -> Counter: return Counter(character for character in text if character in STRUCTURE) -def harms(source: str, translation: str) -> bool: +def harms(source: str, translation: str, language: str = "") -> bool: """Return whether ``translation`` broke something ``source`` carried. Single source of truth for both ends of the pipeline: the client drops a @@ -250,12 +299,17 @@ def harms(source: str, translation: str) -> bool: Args: source: the source message. translation: what came back for it. + language: ISO 639-1 code of the catalog, when the caller knows it. The + script comparison is the one check that needs it; every other + criterion reads both strings alone. """ if translation == source: return sum(character.isalpha() for character in prose(source)) >= ECHO_FLOOR return bool( protected_spans(translation) != protected_spans(source) or missing_names(source, translation) + or stutters(source, translation) + or untranslated(translation, language) or truncated(source, translation) or structure(translation) != structure(source) or tighten(translation) != translation @@ -304,13 +358,16 @@ def mask(text: str) -> Masked: return Masked("".join(parts), tuple(spans)) -def unmask(translated: str, masked: Masked, source: str) -> str | None: +def unmask( + translated: str, masked: Masked, source: str, language: str = "" +) -> str | None: """Return the plain translation, or ``None`` when it damaged a protected span. Args: translated: the HTML the translator returned for ``masked``. masked: the request built by ``mask``. source: the source message. + language: ISO 639-1 code of the catalog being filled. """ seen: list[int] = [] pieces: list[str] = [] @@ -328,6 +385,6 @@ def unmask(translated: str, masked: Masked, source: str) -> str | None: restored = recapitalise(terminate(collapse(restored, source), source), source) if sorted(seen) != list(range(len(masked.spans))): return None - if not restored or harms(source, restored): + if not restored or harms(source, restored, language): return None return restored diff --git a/utils/i18n/translate.py b/utils/i18n/translate.py index 72e0810fce..1fb5405531 100644 --- a/utils/i18n/translate.py +++ b/utils/i18n/translate.py @@ -38,13 +38,14 @@ def damaged(catalog: Catalog) -> list[Message]: Args: catalog: a language catalog. """ + language = str(catalog.locale or "").split("_")[0] return [ message for message in catalog if isinstance(message.id, str) and message.id and message.string - and harms(message.id, str(message.string)) + and harms(message.id, str(message.string), language) ]