From 2db232d70a2b58215ecb8cd27de6189484f2b967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 13 May 2026 09:52:46 +0200 Subject: [PATCH] chore(machinery): consolidate default values Avoid mixing 75 and 80 as default values randomly across the codebase and move that to a single constant. --- weblate/api/views.py | 3 ++- weblate/examples/mt_service.py | 4 ++-- weblate/machinery/alibaba.py | 8 +++++-- weblate/machinery/apertium.py | 3 ++- weblate/machinery/aws.py | 3 ++- weblate/machinery/baidu.py | 3 ++- weblate/machinery/base.py | 22 ++++++++++--------- weblate/machinery/cyrtranslit.py | 4 ++-- weblate/machinery/deepl.py | 3 ++- weblate/machinery/dummy.py | 5 +++-- weblate/machinery/glosbe.py | 4 ++-- weblate/machinery/google.py | 8 +++++-- weblate/machinery/googlev3.py | 3 ++- weblate/machinery/libretranslate.py | 8 +++++-- weblate/machinery/llm.py | 11 +++++----- weblate/machinery/microsoft.py | 3 ++- weblate/machinery/modernmt.py | 3 ++- weblate/machinery/mymemory.py | 4 ++-- weblate/machinery/netease.py | 8 +++++-- weblate/machinery/saptranslationhub.py | 4 ++-- weblate/machinery/systran.py | 8 +++++-- weblate/machinery/tests.py | 3 ++- weblate/machinery/tmserver.py | 4 ++-- weblate/machinery/yandex.py | 8 +++++-- weblate/machinery/yandexv2.py | 8 +++++-- weblate/machinery/youdao.py | 8 +++++-- weblate/memory/machine.py | 7 ++++-- weblate/memory/models.py | 6 ++--- weblate/trans/forms.py | 6 ++++- .../management/commands/auto_translate.py | 3 ++- weblate/trans/tests/test_selenium.py | 3 ++- 31 files changed, 116 insertions(+), 62 deletions(-) diff --git a/weblate/api/views.py b/weblate/api/views.py index f2918811e396..e931aa3c8e62 100644 --- a/weblate/api/views.py +++ b/weblate/api/views.py @@ -123,6 +123,7 @@ from weblate.auth.utils import validate_team_assignable_user from weblate.formats.models import EXPORTERS from weblate.lang.models import Language +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD from weblate.machinery.models import validate_service_configuration from weblate.memory.models import MEMORY_LOOKUP_LIMIT, Memory from weblate.screenshots.models import Screenshot @@ -2424,7 +2425,7 @@ def get_fuzzy_match( source_language: Language, target_language: Language, text: str, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ): base = queryset.filter( source_language=source_language, diff --git a/weblate/examples/mt_service.py b/weblate/examples/mt_service.py index 171b870ecb78..6559742c42a2 100644 --- a/weblate/examples/mt_service.py +++ b/weblate/examples/mt_service.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING -from weblate.machinery.base import MachineTranslation +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation if TYPE_CHECKING: from weblate.auth.models import User @@ -34,7 +34,7 @@ def download_translations( text: str, unit: Unit | None, user: User | None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Return tuple with translations.""" response = self.request( diff --git a/weblate/machinery/alibaba.py b/weblate/machinery/alibaba.py index ee5ee23904cc..f35d264a07cb 100644 --- a/weblate/machinery/alibaba.py +++ b/weblate/machinery/alibaba.py @@ -13,7 +13,11 @@ from requests.exceptions import JSONDecodeError -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import AlibabaMachineryForm if TYPE_CHECKING: @@ -339,7 +343,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" body_params = { diff --git a/weblate/machinery/apertium.py b/weblate/machinery/apertium.py index 98dcc5ceed6d..f5519da1f1c3 100644 --- a/weblate/machinery/apertium.py +++ b/weblate/machinery/apertium.py @@ -10,6 +10,7 @@ from django.utils.translation import gettext from .base import ( + MACHINERY_DEFAULT_THRESHOLD, ResponseStatusMachineTranslation, ) from .forms import URLMachineryForm @@ -141,7 +142,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from Apertium.""" args = { diff --git a/weblate/machinery/aws.py b/weblate/machinery/aws.py index 2c00be605f05..dad6b5dda8a6 100644 --- a/weblate/machinery/aws.py +++ b/weblate/machinery/aws.py @@ -9,6 +9,7 @@ from django.utils.functional import cached_property from .base import ( + MACHINERY_DEFAULT_THRESHOLD, GlossaryDoesNotExistError, GlossaryMachineTranslationMixin, ) @@ -70,7 +71,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: params = { "Text": text, diff --git a/weblate/machinery/baidu.py b/weblate/machinery/baidu.py index 45d15ec7f845..92144896eec8 100644 --- a/weblate/machinery/baidu.py +++ b/weblate/machinery/baidu.py @@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, ClassVar from .base import ( + MACHINERY_DEFAULT_THRESHOLD, MachineryRateLimitError, MachineTranslation, MachineTranslationError, @@ -103,7 +104,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" salt, sign = self.signed_salt( diff --git a/weblate/machinery/base.py b/weblate/machinery/base.py index a6303599ba5c..70e31af1913c 100644 --- a/weblate/machinery/base.py +++ b/weblate/machinery/base.py @@ -44,6 +44,8 @@ SourceLanguageChoices, ) +MACHINERY_DEFAULT_THRESHOLD = 80 + if TYPE_CHECKING: from collections.abc import Iterable, Iterator @@ -148,7 +150,7 @@ def validate_settings(self) -> None: self.validate_target_language, [("test", None)], None, - 75, + MACHINERY_DEFAULT_THRESHOLD, ) except Exception as error: raise ValidationError( @@ -701,7 +703,7 @@ def translate( self, unit: Unit, user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, *, source_language: Language | None = None, ): @@ -751,7 +753,7 @@ def download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: """ Download dictionary of a lists of possible translations from a service. @@ -771,7 +773,7 @@ def _translate( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: return { text: result @@ -790,7 +792,7 @@ def _translate_sources( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> list[list[TranslationResultDict]]: output: list[list[TranslationResultDict]] = [[] for _source in sources] pending: dict[str, list[tuple[int, Unit | None, str, dict[str, str]]]] = ( @@ -880,7 +882,7 @@ def _download_pending_translations( source_language, target_language, user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> None: remaining_keys = list(pending) while remaining_keys: @@ -945,7 +947,7 @@ def download_pending_translations( target_language, sources: list[tuple[str, Unit | None, int]], user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: return self.download_multiple_translations( source_language, @@ -976,7 +978,7 @@ def batch_translate( self, units: list[Unit] | UnitQuerySet, user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, *, source_language: Language | None = None, ) -> None: @@ -1058,7 +1060,7 @@ def download_translations( text: str, unit: Unit | None, user: User | None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """ Download list of possible translations from a service. @@ -1078,7 +1080,7 @@ def download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: return { text: list( diff --git a/weblate/machinery/cyrtranslit.py b/weblate/machinery/cyrtranslit.py index ca1f409931fc..61441781782a 100644 --- a/weblate/machinery/cyrtranslit.py +++ b/weblate/machinery/cyrtranslit.py @@ -8,7 +8,7 @@ from weblate.lang.models import Language -from .base import MachineTranslation +from .base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation from .types import TranslationResultDict if TYPE_CHECKING: @@ -97,7 +97,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ): """Download list of possible translations from a service.""" import cyrtranslit # noqa: PLC0415 diff --git a/weblate/machinery/deepl.py b/weblate/machinery/deepl.py index de9c3bbd93e0..a40a81ca98b8 100644 --- a/weblate/machinery/deepl.py +++ b/weblate/machinery/deepl.py @@ -11,6 +11,7 @@ from requests.exceptions import HTTPError, RequestException from .base import ( + MACHINERY_DEFAULT_THRESHOLD, BatchMachineTranslation, GlossaryDoesNotExistError, GlossaryMachineTranslationMixin, @@ -149,7 +150,7 @@ def download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user: User | None = None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: """Download list of possible translations from a service.""" texts = [text for text, _unit in sources] diff --git a/weblate/machinery/dummy.py b/weblate/machinery/dummy.py index 9ab50b1601c0..7dd62ef50bfa 100644 --- a/weblate/machinery/dummy.py +++ b/weblate/machinery/dummy.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING from .base import ( + MACHINERY_DEFAULT_THRESHOLD, GlossaryMachineTranslationMixin, MachineTranslation, ) @@ -37,7 +38,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """ Download translations. @@ -92,7 +93,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Translate with glossary.""" self.get_glossary_id(source_language, target_language, unit) diff --git a/weblate/machinery/glosbe.py b/weblate/machinery/glosbe.py index bc461a20fe2d..3f114fdf4f5a 100644 --- a/weblate/machinery/glosbe.py +++ b/weblate/machinery/glosbe.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING -from .base import MachineTranslation +from .base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation if TYPE_CHECKING: from .base import DownloadTranslations @@ -33,7 +33,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" params = { diff --git a/weblate/machinery/google.py b/weblate/machinery/google.py index d5f12d0ee1a2..69a903da5151 100644 --- a/weblate/machinery/google.py +++ b/weblate/machinery/google.py @@ -7,7 +7,11 @@ from requests.exceptions import RequestException -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeyMachineryForm if TYPE_CHECKING: @@ -73,7 +77,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" response = self.request( diff --git a/weblate/machinery/googlev3.py b/weblate/machinery/googlev3.py index ae7655f1b480..c1b882864ae7 100644 --- a/weblate/machinery/googlev3.py +++ b/weblate/machinery/googlev3.py @@ -22,6 +22,7 @@ from google.oauth2 import service_account from .base import ( + MACHINERY_DEFAULT_THRESHOLD, GlossaryAlreadyExistsError, GlossaryDoesNotExistError, GlossaryMachineTranslationMixin, @@ -102,7 +103,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" request = { diff --git a/weblate/machinery/libretranslate.py b/weblate/machinery/libretranslate.py index b5687143558e..f2ada149f682 100644 --- a/weblate/machinery/libretranslate.py +++ b/weblate/machinery/libretranslate.py @@ -8,7 +8,11 @@ from typing import TYPE_CHECKING, ClassVar -from .base import BatchMachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + BatchMachineTranslation, + MachineTranslationError, +) from .forms import LibreTranslateMachineryForm if TYPE_CHECKING: @@ -106,7 +110,7 @@ def download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: """Download list of possible translations from a service.""" texts = [text for text, _unit in sources] diff --git a/weblate/machinery/llm.py b/weblate/machinery/llm.py index 4460775d1091..93ef6f6ca4b6 100644 --- a/weblate/machinery/llm.py +++ b/weblate/machinery/llm.py @@ -24,6 +24,7 @@ get_glossary_tuples, ) from weblate.machinery.base import ( + MACHINERY_DEFAULT_THRESHOLD, BatchMachineTranslation, MachineTranslationError, ) @@ -239,7 +240,7 @@ def _translate_sources( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> list[list[TranslationResultDict]]: started_cache = self._ensure_secondary_context_cache() try: @@ -1051,7 +1052,7 @@ def download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: return self._download_multiple_translations( source_language, target_language, sources, user, threshold @@ -1063,7 +1064,7 @@ def download_pending_translations( target_language, sources: list[tuple[str, Unit | None, int]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadMultipleTranslations: return self._download_multiple_translations( source_language, @@ -1082,7 +1083,7 @@ def _download_multiple_translations( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, *, source_occurrences: list[int] | None = None, ) -> DownloadMultipleTranslations: @@ -1105,7 +1106,7 @@ def _download_multiple_translations_with_context_cache( target_language, sources: list[tuple[str, Unit | None]], user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, *, source_occurrences: list[int] | None = None, ) -> DownloadMultipleTranslations: diff --git a/weblate/machinery/microsoft.py b/weblate/machinery/microsoft.py index 7e5fc8691b77..97151f9648a6 100644 --- a/weblate/machinery/microsoft.py +++ b/weblate/machinery/microsoft.py @@ -13,6 +13,7 @@ from weblate.glossary.models import get_glossary_terms from .base import ( + MACHINERY_DEFAULT_THRESHOLD, MachineTranslation, MachineTranslationError, XMLMachineTranslationMixin, @@ -152,7 +153,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" args = { diff --git a/weblate/machinery/modernmt.py b/weblate/machinery/modernmt.py index 3941f90c15ae..f2d45472169b 100644 --- a/weblate/machinery/modernmt.py +++ b/weblate/machinery/modernmt.py @@ -15,6 +15,7 @@ import weblate.utils.version from .base import ( + MACHINERY_DEFAULT_THRESHOLD, GlossaryDoesNotExistError, GlossaryMachineTranslationMixin, MachineTranslationError, @@ -85,7 +86,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" params = {"q": text, "source": source_language, "target": target_language} diff --git a/weblate/machinery/mymemory.py b/weblate/machinery/mymemory.py index a6f3223001d8..49422f3b52d9 100644 --- a/weblate/machinery/mymemory.py +++ b/weblate/machinery/mymemory.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING -from .base import ResponseStatusMachineTranslation +from .base import MACHINERY_DEFAULT_THRESHOLD, ResponseStatusMachineTranslation from .forms import MyMemoryMachineryForm if TYPE_CHECKING: @@ -62,7 +62,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from MyMemory.""" args = { diff --git a/weblate/machinery/netease.py b/weblate/machinery/netease.py index a79800ac8885..4de0b7d71a3a 100644 --- a/weblate/machinery/netease.py +++ b/weblate/machinery/netease.py @@ -9,7 +9,11 @@ from hashlib import sha1 from typing import TYPE_CHECKING, ClassVar -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeySecretMachineryForm if TYPE_CHECKING: @@ -55,7 +59,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" response = self.request( diff --git a/weblate/machinery/saptranslationhub.py b/weblate/machinery/saptranslationhub.py index 565c412b27a5..0db532e995b9 100644 --- a/weblate/machinery/saptranslationhub.py +++ b/weblate/machinery/saptranslationhub.py @@ -7,7 +7,7 @@ from typing import TYPE_CHECKING -from .base import MachineTranslation +from .base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation from .forms import SAPMachineryForm if TYPE_CHECKING: @@ -58,7 +58,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" # should the machine translation service be used? diff --git a/weblate/machinery/systran.py b/weblate/machinery/systran.py index 0eeddadebc79..dd1f77814ac3 100644 --- a/weblate/machinery/systran.py +++ b/weblate/machinery/systran.py @@ -2,7 +2,11 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeyMachineryForm @@ -40,7 +44,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ): """Download list of possible translations from a service.""" response = self.request( diff --git a/weblate/machinery/tests.py b/weblate/machinery/tests.py index 26cb527921c7..a27b29ac76d1 100644 --- a/weblate/machinery/tests.py +++ b/weblate/machinery/tests.py @@ -46,6 +46,7 @@ from weblate.machinery.aws import AWSTranslation from weblate.machinery.baidu import BAIDU_API, BaiduTranslation from weblate.machinery.base import ( + MACHINERY_DEFAULT_THRESHOLD, MachineryRateLimitError, MachineTranslationError, ) @@ -551,7 +552,7 @@ def download_multiple_translations( target_language, sources, user=None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ): self.batches.append(sources) return { diff --git a/weblate/machinery/tmserver.py b/weblate/machinery/tmserver.py index cf4fface1ab0..362e099545d9 100644 --- a/weblate/machinery/tmserver.py +++ b/weblate/machinery/tmserver.py @@ -8,7 +8,7 @@ from requests.exceptions import HTTPError -from .base import MachineTranslation +from .base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation from .forms import URLMachineryForm if TYPE_CHECKING: @@ -57,7 +57,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" url = self.get_api_url( diff --git a/weblate/machinery/yandex.py b/weblate/machinery/yandex.py index c4015cc47d5b..f6004119babf 100644 --- a/weblate/machinery/yandex.py +++ b/weblate/machinery/yandex.py @@ -7,7 +7,11 @@ from requests.exceptions import RequestException -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeyMachineryForm if TYPE_CHECKING: @@ -52,7 +56,7 @@ def download_translations( text: str, unit: Unit | None, user: User | None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" response = self.request( diff --git a/weblate/machinery/yandexv2.py b/weblate/machinery/yandexv2.py index ccc809e470e7..8c08b6877c83 100644 --- a/weblate/machinery/yandexv2.py +++ b/weblate/machinery/yandexv2.py @@ -9,7 +9,11 @@ from requests.exceptions import RequestException -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeyMachineryForm if TYPE_CHECKING: @@ -57,7 +61,7 @@ def download_translations( text: str, unit: Unit | None, user: User | None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" key = self.settings["key"] diff --git a/weblate/machinery/youdao.py b/weblate/machinery/youdao.py index f909f7abf7ee..d27b33b4f5f7 100644 --- a/weblate/machinery/youdao.py +++ b/weblate/machinery/youdao.py @@ -6,7 +6,11 @@ from typing import TYPE_CHECKING, ClassVar -from .base import MachineTranslation, MachineTranslationError +from .base import ( + MACHINERY_DEFAULT_THRESHOLD, + MachineTranslation, + MachineTranslationError, +) from .forms import KeySecretMachineryForm if TYPE_CHECKING: @@ -65,7 +69,7 @@ def download_translations( text: str, unit: Unit | None, user: User | None, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" salt, sign = self.signed_salt( diff --git a/weblate/memory/machine.py b/weblate/memory/machine.py index 8c36db6e939e..3aba0be1b731 100644 --- a/weblate/memory/machine.py +++ b/weblate/memory/machine.py @@ -7,7 +7,10 @@ from django.urls import reverse -from weblate.machinery.base import InternalMachineTranslation +from weblate.machinery.base import ( + MACHINERY_DEFAULT_THRESHOLD, + InternalMachineTranslation, +) from weblate.memory.models import Memory if TYPE_CHECKING: @@ -31,7 +34,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: """Download list of possible translations from a service.""" for result in Memory.objects.lookup( diff --git a/weblate/memory/models.py b/weblate/memory/models.py index 873b62335880..140bd4eabc21 100644 --- a/weblate/memory/models.py +++ b/weblate/memory/models.py @@ -22,6 +22,7 @@ from weblate_schemas import load_schema from weblate.lang.models import Language +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD from weblate.memory.utils import ( CATEGORY_FILE, CATEGORY_PRIVATE_OFFSET, @@ -142,8 +143,7 @@ def threshold_to_similarity(self, text: str, threshold: int) -> float: Machinery threshold typical values: - - 75 machinery - - 80 automatic translation (default value) + - 80 machinery and automatic translation (default value) - 10 search PostgreSQL similarity threshold needs to be higher to avoid too slow @@ -223,7 +223,7 @@ def lookup( user, project, use_shared, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ): base = self.prefetch_project().filter_type( user=user, diff --git a/weblate/trans/forms.py b/weblate/trans/forms.py index 1aaf647dcf2b..2c1404c6b99c 100644 --- a/weblate/trans/forms.py +++ b/weblate/trans/forms.py @@ -50,6 +50,7 @@ from weblate.formats.base import BilingualUpdateMixin from weblate.formats.models import EXPORTERS, FILE_FORMATS from weblate.lang.models import Language +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD from weblate.machinery.models import MACHINERY from weblate.trans.actions import ActionEvents from weblate.trans.backups import ProjectBackup @@ -1037,7 +1038,10 @@ class AutoForm(forms.Form): label=gettext_lazy("Machine translation engines"), choices=[], required=False ) threshold = forms.IntegerField( - label=gettext_lazy("Score threshold"), initial=80, min_value=1, max_value=100 + label=gettext_lazy("Score threshold"), + initial=MACHINERY_DEFAULT_THRESHOLD, + min_value=1, + max_value=100, ) def __init__( diff --git a/weblate/trans/management/commands/auto_translate.py b/weblate/trans/management/commands/auto_translate.py index 75d651f1e1a3..b4f37d323367 100644 --- a/weblate/trans/management/commands/auto_translate.py +++ b/weblate/trans/management/commands/auto_translate.py @@ -9,6 +9,7 @@ from django.core.management.base import CommandError from weblate.auth.models import User +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD from weblate.machinery.models import MACHINERY from weblate.trans.autotranslate import AutoTranslate from weblate.trans.models import Component @@ -54,7 +55,7 @@ def add_arguments(self, parser: CommandParser) -> None: ) parser.add_argument( "--threshold", - default=80, + default=MACHINERY_DEFAULT_THRESHOLD, type=int, help=("Set machine translation threshold"), ) diff --git a/weblate/trans/tests/test_selenium.py b/weblate/trans/tests/test_selenium.py index 45f0bb1ccda6..f9afe4ee8198 100644 --- a/weblate/trans/tests/test_selenium.py +++ b/weblate/trans/tests/test_selenium.py @@ -39,6 +39,7 @@ from weblate.auth.models import User from weblate.fonts.tests.utils import FONT, FONT_SOURCE from weblate.lang.models import Language +from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD from weblate.machinery.dummy import DummyTranslation from weblate.screenshots.views import ensure_tesseract_language from weblate.trans.actions import ActionEvents @@ -77,7 +78,7 @@ def download_translations( text: str, unit, user, - threshold: int = 75, + threshold: int = MACHINERY_DEFAULT_THRESHOLD, ) -> DownloadTranslations: _ = (source_language, target_language, unit, user, threshold) yield {