Skip to content

Commit

Permalink
Add glossary support for document translation
Browse files Browse the repository at this point in the history
Move document-related test fixtures to conftest.py
  • Loading branch information
daniel-jones-dev committed Nov 9, 2021
1 parent df9dffd commit 7a6b236
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
* Add glossary support for document translation.
### Changed
### Deprecated
### Removed
Expand Down
13 changes: 12 additions & 1 deletion deepl/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 739,7 @@ def translate_document_from_filepath(
source_lang: Optional[str] = None,
target_lang: str,
formality: Union[str, Formality] = Formality.DEFAULT,
glossary: Union[str, GlossaryInfo, None] = None,
) -> None:
"""Upload document at given input path, translate it into the target
language, and download result to given output path.
Expand All @@ -751,6 752,8 @@ def translate_document_from_filepath(
"EN-US", "FR".
:param formality: (Optional) Desired formality for translation, as Formality
enum, "less" or "more".
:param glossary: (Optional) glossary or glossary ID to use for
translation. Must match specified source_lang and target_lang.
:raises DocumentTranslationException: If an error occurs during translation,
The exception includes information about the document request.
Expand All @@ -764,6 767,7 @@ def translate_document_from_filepath(
target_lang=target_lang,
source_lang=source_lang,
formality=formality,
glossary=glossary,
)
except Exception as e:
out_file.close()
Expand All @@ -778,6 782,7 @@ def translate_document(
source_lang: Optional[str] = None,
target_lang: str,
formality: Union[str, Formality] = Formality.DEFAULT,
glossary: Union[str, GlossaryInfo, None] = None,
) -> None:
"""Upload document, translate it into the target language, and download
result.
Expand All @@ -792,6 797,8 @@ def translate_document(
example "DE", "EN-US", "FR".
:param formality: (Optional) Desired formality for translation, as
Formality enum, "less" or "more".
:param glossary: (Optional) glossary or glossary ID to use for
translation. Must match specified source_lang and target_lang.
:raises DocumentTranslationException: If an error occurs during
translation, the exception includes the document handle.
Expand All @@ -802,6 809,7 @@ def translate_document(
target_lang=target_lang,
source_lang=source_lang,
formality=formality,
glossary=glossary,
)

try:
Expand All @@ -828,6 836,7 @@ def translate_document_upload(
source_lang: Optional[str] = None,
target_lang: str,
formality: Union[str, Formality] = Formality.DEFAULT,
glossary: Union[str, GlossaryInfo, None] = None,
) -> DocumentHandle:
"""Upload document to be translated and return handle associated with
request.
Expand All @@ -841,11 850,13 @@ def translate_document_upload(
example "DE", "EN-US", "FR".
:param formality: (Optional) Desired formality for translation, as
Formality enum, "less" or "more".
:param glossary: (Optional) glossary or glossary ID to use for
translation. Must match specified source_lang and target_lang.
:return: DocumentHandle with ID and key identifying document.
"""

request_data = self._check_language_and_formality(
source_lang, target_lang, formality
source_lang, target_lang, formality, glossary
)

files = {"file": input_document}
Expand Down
37 changes: 37 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@

import deepl
import os
import pathlib
from pydantic import BaseSettings
import pytest
from typing import Optional
Expand Down Expand Up @@ -164,6 165,42 @@ def glossary_name(translator, request) -> str:
return f"{glossary_name_prefix}{test_name}"


@pytest.fixture
def example_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "input" / "example_document.txt"
path.parent.mkdir()
path.write_text(example_text["EN"])
return path


@pytest.fixture
def example_document_translation():
return example_text["DE"]


@pytest.fixture
def example_large_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "input" / "example_document.txt"
path.parent.mkdir()
path.write_text((example_text["EN"] "\n") * 1000)
return path


@pytest.fixture
def example_large_document_translation():
return (example_text["DE"] "\n") * 1000


@pytest.fixture
def output_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "output" / "example_document.txt"
path.parent.mkdir()
return path


def create_glossary(
translator,
glossary_name,
Expand Down
27 changes: 27 additions & 0 deletions tests/test_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 181,33 @@ def test_glossary_translate_text_basic(translator, glossary_name):
assert [r.text for r in result] == texts_en


def test_glossary_translate_document(
translator,
glossary_name,
example_document_path,
output_document_path,
):
input_text = "artist\nprize"
expected_output_text = "Maler\nGewinn"

glossary = create_glossary(
translator,
glossary_name,
entries={"artist": "Maler", "prize": "Gewinn"},
source_lang="EN",
target_lang="DE",
)
example_document_path.write_text(input_text)
translator.translate_document_from_filepath(
example_document_path,
output_path=output_document_path,
source_lang="EN",
target_lang="DE",
glossary=glossary,
)
assert expected_output_text == output_document_path.read_text()


def test_glossary_translate_text_invalid(translator, glossary_name):
glossary_ende = create_glossary(
translator, f"{glossary_name}_ende", source_lang="EN", target_lang="DE"
Expand Down
36 changes: 0 additions & 36 deletions tests/test_translate_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 11,6 @@
default_lang_args = {"target_lang": "DE", "source_lang": "EN"}


@pytest.fixture
def example_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "input" / "example_document.txt"
path.parent.mkdir()
path.write_text(example_text["EN"])
return path


@pytest.fixture
def example_document_translation():
return example_text["DE"]


@pytest.fixture
def example_large_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "input" / "example_document.txt"
path.parent.mkdir()
path.write_text((example_text["EN"] "\n") * 1000)
return path


@pytest.fixture
def example_large_document_translation():
return (example_text["DE"] "\n") * 1000


@pytest.fixture
def output_document_path(tmpdir):
tmpdir = pathlib.Path(tmpdir)
path = tmpdir / "output" / "example_document.txt"
path.parent.mkdir()
return path


def test_translate_document_from_filepath(
translator,
example_document_path,
Expand Down

0 comments on commit 7a6b236

Please sign in to comment.