Skip to content

Commit 8fe38cf

Browse files
committed
chore(machinery): consolidate default values
Avoid mixing 75 and 80 as default values randomly across the codebase and move that to a single constant.
1 parent bfbd466 commit 8fe38cf

31 files changed

Lines changed: 116 additions & 62 deletions

weblate/api/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
from weblate.auth.utils import validate_team_assignable_user
125125
from weblate.formats.models import EXPORTERS
126126
from weblate.lang.models import Language
127+
from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD
127128
from weblate.machinery.models import validate_service_configuration
128129
from weblate.memory.models import MEMORY_LOOKUP_LIMIT, Memory
129130
from weblate.screenshots.models import Screenshot
@@ -2425,7 +2426,7 @@ def get_fuzzy_match(
24252426
source_language: Language,
24262427
target_language: Language,
24272428
text: str,
2428-
threshold: int = 75,
2429+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
24292430
):
24302431
base = queryset.filter(
24312432
source_language=source_language,

weblate/examples/mt_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from typing import TYPE_CHECKING
1111

12-
from weblate.machinery.base import MachineTranslation
12+
from weblate.machinery.base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation
1313

1414
if TYPE_CHECKING:
1515
from weblate.auth.models import User
@@ -34,7 +34,7 @@ def download_translations(
3434
text: str,
3535
unit: Unit | None,
3636
user: User | None,
37-
threshold: int = 75,
37+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
3838
) -> DownloadTranslations:
3939
"""Return tuple with translations."""
4040
response = self.request(

weblate/machinery/alibaba.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
from requests.exceptions import JSONDecodeError
1515

16-
from .base import MachineTranslation, MachineTranslationError
16+
from .base import (
17+
MACHINERY_DEFAULT_THRESHOLD,
18+
MachineTranslation,
19+
MachineTranslationError,
20+
)
1721
from .forms import AlibabaMachineryForm
1822

1923
if TYPE_CHECKING:
@@ -339,7 +343,7 @@ def download_translations(
339343
text: str,
340344
unit,
341345
user,
342-
threshold: int = 75,
346+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
343347
) -> DownloadTranslations:
344348
"""Download list of possible translations from a service."""
345349
body_params = {

weblate/machinery/apertium.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.utils.translation import gettext
1111

1212
from .base import (
13+
MACHINERY_DEFAULT_THRESHOLD,
1314
ResponseStatusMachineTranslation,
1415
)
1516
from .forms import URLMachineryForm
@@ -141,7 +142,7 @@ def download_translations(
141142
text: str,
142143
unit,
143144
user,
144-
threshold: int = 75,
145+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
145146
) -> DownloadTranslations:
146147
"""Download list of possible translations from Apertium."""
147148
args = {

weblate/machinery/aws.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.utils.functional import cached_property
1010

1111
from .base import (
12+
MACHINERY_DEFAULT_THRESHOLD,
1213
GlossaryDoesNotExistError,
1314
GlossaryMachineTranslationMixin,
1415
)
@@ -70,7 +71,7 @@ def download_translations(
7071
text: str,
7172
unit,
7273
user,
73-
threshold: int = 75,
74+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
7475
) -> DownloadTranslations:
7576
params = {
7677
"Text": text,

weblate/machinery/baidu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import TYPE_CHECKING, ClassVar
88

99
from .base import (
10+
MACHINERY_DEFAULT_THRESHOLD,
1011
MachineryRateLimitError,
1112
MachineTranslation,
1213
MachineTranslationError,
@@ -103,7 +104,7 @@ def download_translations(
103104
text: str,
104105
unit,
105106
user,
106-
threshold: int = 75,
107+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
107108
) -> DownloadTranslations:
108109
"""Download list of possible translations from a service."""
109110
salt, sign = self.signed_salt(

weblate/machinery/base.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
SourceLanguageChoices,
4545
)
4646

47+
MACHINERY_DEFAULT_THRESHOLD = 80
48+
4749
if TYPE_CHECKING:
4850
from collections.abc import Iterable, Iterator
4951

@@ -148,7 +150,7 @@ def validate_settings(self) -> None:
148150
self.validate_target_language,
149151
[("test", None)],
150152
None,
151-
75,
153+
MACHINERY_DEFAULT_THRESHOLD,
152154
)
153155
except Exception as error:
154156
raise ValidationError(
@@ -701,7 +703,7 @@ def translate(
701703
self,
702704
unit: Unit,
703705
user: User | None = None,
704-
threshold: int = 75,
706+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
705707
*,
706708
source_language: Language | None = None,
707709
):
@@ -751,7 +753,7 @@ def download_multiple_translations(
751753
target_language,
752754
sources: list[tuple[str, Unit | None]],
753755
user: User | None = None,
754-
threshold: int = 75,
756+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
755757
) -> DownloadMultipleTranslations:
756758
"""
757759
Download dictionary of a lists of possible translations from a service.
@@ -771,7 +773,7 @@ def _translate(
771773
target_language,
772774
sources: list[tuple[str, Unit | None]],
773775
user=None,
774-
threshold: int = 75,
776+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
775777
) -> DownloadMultipleTranslations:
776778
return {
777779
text: result
@@ -790,7 +792,7 @@ def _translate_sources(
790792
target_language,
791793
sources: list[tuple[str, Unit | None]],
792794
user=None,
793-
threshold: int = 75,
795+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
794796
) -> list[list[TranslationResultDict]]:
795797
output: list[list[TranslationResultDict]] = [[] for _source in sources]
796798
pending: dict[str, list[tuple[int, Unit | None, str, dict[str, str]]]] = (
@@ -880,7 +882,7 @@ def _download_pending_translations(
880882
source_language,
881883
target_language,
882884
user=None,
883-
threshold: int = 75,
885+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
884886
) -> None:
885887
remaining_keys = list(pending)
886888
while remaining_keys:
@@ -945,7 +947,7 @@ def download_pending_translations(
945947
target_language,
946948
sources: list[tuple[str, Unit | None, int]],
947949
user: User | None = None,
948-
threshold: int = 75,
950+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
949951
) -> DownloadMultipleTranslations:
950952
return self.download_multiple_translations(
951953
source_language,
@@ -976,7 +978,7 @@ def batch_translate(
976978
self,
977979
units: list[Unit] | UnitQuerySet,
978980
user: User | None = None,
979-
threshold: int = 75,
981+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
980982
*,
981983
source_language: Language | None = None,
982984
) -> None:
@@ -1058,7 +1060,7 @@ def download_translations(
10581060
text: str,
10591061
unit: Unit | None,
10601062
user: User | None,
1061-
threshold: int = 75,
1063+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
10621064
) -> DownloadTranslations:
10631065
"""
10641066
Download list of possible translations from a service.
@@ -1078,7 +1080,7 @@ def download_multiple_translations(
10781080
target_language,
10791081
sources: list[tuple[str, Unit | None]],
10801082
user: User | None = None,
1081-
threshold: int = 75,
1083+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
10821084
) -> DownloadMultipleTranslations:
10831085
return {
10841086
text: list(

weblate/machinery/cyrtranslit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from weblate.lang.models import Language
1010

11-
from .base import MachineTranslation
11+
from .base import MACHINERY_DEFAULT_THRESHOLD, MachineTranslation
1212
from .types import TranslationResultDict
1313

1414
if TYPE_CHECKING:
@@ -97,7 +97,7 @@ def download_translations(
9797
text: str,
9898
unit,
9999
user,
100-
threshold: int = 75,
100+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
101101
):
102102
"""Download list of possible translations from a service."""
103103
import cyrtranslit # noqa: PLC0415

weblate/machinery/deepl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from requests.exceptions import HTTPError, RequestException
1212

1313
from .base import (
14+
MACHINERY_DEFAULT_THRESHOLD,
1415
BatchMachineTranslation,
1516
GlossaryDoesNotExistError,
1617
GlossaryMachineTranslationMixin,
@@ -149,7 +150,7 @@ def download_multiple_translations(
149150
target_language,
150151
sources: list[tuple[str, Unit | None]],
151152
user: User | None = None,
152-
threshold: int = 75,
153+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
153154
) -> DownloadMultipleTranslations:
154155
"""Download list of possible translations from a service."""
155156
texts = [text for text, _unit in sources]

weblate/machinery/dummy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING
77

88
from .base import (
9+
MACHINERY_DEFAULT_THRESHOLD,
910
GlossaryMachineTranslationMixin,
1011
MachineTranslation,
1112
)
@@ -37,7 +38,7 @@ def download_translations(
3738
text: str,
3839
unit,
3940
user,
40-
threshold: int = 75,
41+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
4142
) -> DownloadTranslations:
4243
"""
4344
Download translations.
@@ -92,7 +93,7 @@ def download_translations(
9293
text: str,
9394
unit,
9495
user,
95-
threshold: int = 75,
96+
threshold: int = MACHINERY_DEFAULT_THRESHOLD,
9697
) -> DownloadTranslations:
9798
"""Translate with glossary."""
9899
self.get_glossary_id(source_language, target_language, unit)

0 commit comments

Comments
 (0)