Skip to content

Commit 7a6b236

Browse files
Add glossary support for document translation
Move document-related test fixtures to conftest.py
1 parent df9dffd commit 7a6b236

File tree

5 files changed

+77
-37
lines changed

5 files changed

+77
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99
### Added
10+
* Add glossary support for document translation.
1011
### Changed
1112
### Deprecated
1213
### Removed

deepl/translator.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ def translate_document_from_filepath(
739739
source_lang: Optional[str] = None,
740740
target_lang: str,
741741
formality: Union[str, Formality] = Formality.DEFAULT,
742+
glossary: Union[str, GlossaryInfo, None] = None,
742743
) -> None:
743744
"""Upload document at given input path, translate it into the target
744745
language, and download result to given output path.
@@ -751,6 +752,8 @@ def translate_document_from_filepath(
751752
"EN-US", "FR".
752753
:param formality: (Optional) Desired formality for translation, as Formality
753754
enum, "less" or "more".
755+
:param glossary: (Optional) glossary or glossary ID to use for
756+
translation. Must match specified source_lang and target_lang.
754757
755758
:raises DocumentTranslationException: If an error occurs during translation,
756759
The exception includes information about the document request.
@@ -764,6 +767,7 @@ def translate_document_from_filepath(
764767
target_lang=target_lang,
765768
source_lang=source_lang,
766769
formality=formality,
770+
glossary=glossary,
767771
)
768772
except Exception as e:
769773
out_file.close()
@@ -778,6 +782,7 @@ def translate_document(
778782
source_lang: Optional[str] = None,
779783
target_lang: str,
780784
formality: Union[str, Formality] = Formality.DEFAULT,
785+
glossary: Union[str, GlossaryInfo, None] = None,
781786
) -> None:
782787
"""Upload document, translate it into the target language, and download
783788
result.
@@ -792,6 +797,8 @@ def translate_document(
792797
example "DE", "EN-US", "FR".
793798
:param formality: (Optional) Desired formality for translation, as
794799
Formality enum, "less" or "more".
800+
:param glossary: (Optional) glossary or glossary ID to use for
801+
translation. Must match specified source_lang and target_lang.
795802
796803
:raises DocumentTranslationException: If an error occurs during
797804
translation, the exception includes the document handle.
@@ -802,6 +809,7 @@ def translate_document(
802809
target_lang=target_lang,
803810
source_lang=source_lang,
804811
formality=formality,
812+
glossary=glossary,
805813
)
806814

807815
try:
@@ -828,6 +836,7 @@ def translate_document_upload(
828836
source_lang: Optional[str] = None,
829837
target_lang: str,
830838
formality: Union[str, Formality] = Formality.DEFAULT,
839+
glossary: Union[str, GlossaryInfo, None] = None,
831840
) -> DocumentHandle:
832841
"""Upload document to be translated and return handle associated with
833842
request.
@@ -841,11 +850,13 @@ def translate_document_upload(
841850
example "DE", "EN-US", "FR".
842851
:param formality: (Optional) Desired formality for translation, as
843852
Formality enum, "less" or "more".
853+
:param glossary: (Optional) glossary or glossary ID to use for
854+
translation. Must match specified source_lang and target_lang.
844855
:return: DocumentHandle with ID and key identifying document.
845856
"""
846857

847858
request_data = self._check_language_and_formality(
848-
source_lang, target_lang, formality
859+
source_lang, target_lang, formality, glossary
849860
)
850861

851862
files = {"file": input_document}

tests/conftest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import deepl
66
import os
7+
import pathlib
78
from pydantic import BaseSettings
89
import pytest
910
from typing import Optional
@@ -164,6 +165,42 @@ def glossary_name(translator, request) -> str:
164165
return f"{glossary_name_prefix}{test_name}"
165166

166167

168+
@pytest.fixture
169+
def example_document_path(tmpdir):
170+
tmpdir = pathlib.Path(tmpdir)
171+
path = tmpdir / "input" / "example_document.txt"
172+
path.parent.mkdir()
173+
path.write_text(example_text["EN"])
174+
return path
175+
176+
177+
@pytest.fixture
178+
def example_document_translation():
179+
return example_text["DE"]
180+
181+
182+
@pytest.fixture
183+
def example_large_document_path(tmpdir):
184+
tmpdir = pathlib.Path(tmpdir)
185+
path = tmpdir / "input" / "example_document.txt"
186+
path.parent.mkdir()
187+
path.write_text((example_text["EN"] + "\n") * 1000)
188+
return path
189+
190+
191+
@pytest.fixture
192+
def example_large_document_translation():
193+
return (example_text["DE"] + "\n") * 1000
194+
195+
196+
@pytest.fixture
197+
def output_document_path(tmpdir):
198+
tmpdir = pathlib.Path(tmpdir)
199+
path = tmpdir / "output" / "example_document.txt"
200+
path.parent.mkdir()
201+
return path
202+
203+
167204
def create_glossary(
168205
translator,
169206
glossary_name,

tests/test_glossary.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ def test_glossary_translate_text_basic(translator, glossary_name):
181181
assert [r.text for r in result] == texts_en
182182

183183

184+
def test_glossary_translate_document(
185+
translator,
186+
glossary_name,
187+
example_document_path,
188+
output_document_path,
189+
):
190+
input_text = "artist\nprize"
191+
expected_output_text = "Maler\nGewinn"
192+
193+
glossary = create_glossary(
194+
translator,
195+
glossary_name,
196+
entries={"artist": "Maler", "prize": "Gewinn"},
197+
source_lang="EN",
198+
target_lang="DE",
199+
)
200+
example_document_path.write_text(input_text)
201+
translator.translate_document_from_filepath(
202+
example_document_path,
203+
output_path=output_document_path,
204+
source_lang="EN",
205+
target_lang="DE",
206+
glossary=glossary,
207+
)
208+
assert expected_output_text == output_document_path.read_text()
209+
210+
184211
def test_glossary_translate_text_invalid(translator, glossary_name):
185212
glossary_ende = create_glossary(
186213
translator, f"{glossary_name}_ende", source_lang="EN", target_lang="DE"

tests/test_translate_document.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,6 @@
1111
default_lang_args = {"target_lang": "DE", "source_lang": "EN"}
1212

1313

14-
@pytest.fixture
15-
def example_document_path(tmpdir):
16-
tmpdir = pathlib.Path(tmpdir)
17-
path = tmpdir / "input" / "example_document.txt"
18-
path.parent.mkdir()
19-
path.write_text(example_text["EN"])
20-
return path
21-
22-
23-
@pytest.fixture
24-
def example_document_translation():
25-
return example_text["DE"]
26-
27-
28-
@pytest.fixture
29-
def example_large_document_path(tmpdir):
30-
tmpdir = pathlib.Path(tmpdir)
31-
path = tmpdir / "input" / "example_document.txt"
32-
path.parent.mkdir()
33-
path.write_text((example_text["EN"] + "\n") * 1000)
34-
return path
35-
36-
37-
@pytest.fixture
38-
def example_large_document_translation():
39-
return (example_text["DE"] + "\n") * 1000
40-
41-
42-
@pytest.fixture
43-
def output_document_path(tmpdir):
44-
tmpdir = pathlib.Path(tmpdir)
45-
path = tmpdir / "output" / "example_document.txt"
46-
path.parent.mkdir()
47-
return path
48-
49-
5014
def test_translate_document_from_filepath(
5115
translator,
5216
example_document_path,

0 commit comments

Comments
 (0)