Coverage for app/venv/lib/python3.14/site-packages/weblate/memory/machine.py: 42%
23 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
4from __future__ import annotations
6from typing import TYPE_CHECKING
8from django.urls import reverse
10from weblate.machinery.base import InternalMachineTranslation
11from weblate.memory.models import Memory
13if TYPE_CHECKING: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true
14 from weblate.machinery.base import DownloadTranslations
16PENDING_MEMORY_PENALTY_FACTOR = 0.7
17DIFFERENT_CONTEXT_PENALTY_FACTOR = 0.95
20class WeblateMemory(InternalMachineTranslation):
21 """Translation service using strings already translated in Weblate."""
23 name = "Weblate Translation Memory"
24 rank_boost = 2
25 same_languages = True
27 def download_translations(
28 self,
29 source_language,
30 target_language,
31 text: str,
32 unit,
33 user,
34 threshold: int = 75,
35 ) -> DownloadTranslations:
36 """Download list of possible translations from a service."""
37 for result in Memory.objects.lookup(
38 source_language,
39 target_language,
40 text,
41 user,
42 unit.translation.component.project,
43 unit.translation.component.project.use_shared_tm,
44 threshold=threshold,
45 ):
46 quality = self.comparer.similarity(text, result.source)
47 if result.status == Memory.STATUS_PENDING:
48 quality = round(quality * PENDING_MEMORY_PENALTY_FACTOR)
49 if unit.context != result.context and not (
50 result.from_file and not result.context
51 ):
52 quality = round(quality * DIFFERENT_CONTEXT_PENALTY_FACTOR)
54 if quality < threshold:
55 continue
56 yield {
57 "text": result.target,
58 "quality": quality,
59 "service": self.name,
60 "origin": result.get_origin_display(),
61 "source": result.source,
62 "show_quality": True,
63 "delete_url": reverse("api:memory-detail", kwargs={"pk": result.id})
64 if user is not None and user.has_perm("memory.delete", result)
65 else None,
66 }