Skip to content

Commit a68c8fd

Browse files
feat: add new Formality options: PREFER_MORE and PREFER_LESS
1 parent 5f44f98 commit a68c8fd

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [Unreleased]
9+
### Added
10+
* Add formality options `'prefer_less'` and `'prefer_more'`.
11+
12+
813
## [1.10.0] - 2022-09-09
914
### Added
1015
* New language available: Ukrainian (`'uk'`). Add language code constant and tests.
1116

1217
Note: older library versions also support new languages, this update only adds new code constant.
18+
* Add new `Formality` options: `PREFER_MORE` and `PREFER_LESS`.
1319
### Changed
1420
* Add note and workaround to README about Poetry error on Ubuntu 22.04.
1521
* Pull request [#48](https://github.com/DeepLcom/deepl-python/pull/48)
@@ -194,6 +200,7 @@ Version increased to avoid conflicts with old packages on PyPI.
194200
Initial version.
195201

196202

203+
[Unreleased]: https://github.com/DeepLcom/deepl-python/compare/v1.10.0...HEAD
197204
[1.10.0]: https://github.com/DeepLcom/deepl-python/compare/v1.9.0...v1.10.0
198205
[1.9.0]: https://github.com/DeepLcom/deepl-python/compare/v1.8.0...v1.9.0
199206
[1.8.0]: https://github.com/DeepLcom/deepl-python/compare/v1.7.0...v1.8.0

deepl/translator.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,21 @@ class Formality(Enum):
399399
"""Options for formality parameter."""
400400

401401
LESS = "less"
402+
"""Translate using informal language."""
403+
402404
DEFAULT = "default"
405+
"""Translate using the default formality."""
406+
403407
MORE = "more"
408+
"""Translate using formal language."""
409+
410+
PREFER_MORE = "prefer_more"
411+
"""Translate using formal language if the target language supports
412+
formality, otherwise use default formality."""
413+
414+
PREFER_LESS = "prefer_less"
415+
"""Translate using informal language if the target language supports
416+
formality, otherwise use default formality."""
404417

405418
def __str__(self):
406419
return self.value
@@ -701,7 +714,8 @@ def translate_text(
701714
translation engine from correcting some formatting aspects, and
702715
instead leave the formatting unchanged.
703716
:param formality: (Optional) Desired formality for translation, as
704-
Formality enum, "less" or "more".
717+
Formality enum, "less", "more", "prefer_less", "prefer_more", or
718+
"default".
705719
:param glossary: (Optional) glossary or glossary ID to use for
706720
translation. Must match specified source_lang and target_lang.
707721
:param tag_handling: (Optional) Type of tags to parse before
@@ -846,7 +860,7 @@ def translate_document_from_filepath(
846860
:param target_lang: Language code to translate document into, for
847861
example "DE", "EN-US", "FR".
848862
:param formality: (Optional) Desired formality for translation, as
849-
Formality enum, "less" or "more".
863+
Formality enum, "less", "more", "prefer_less", or "prefer_more".
850864
:param glossary: (Optional) glossary or glossary ID to use for
851865
translation. Must match specified source_lang and target_lang.
852866
:return: DocumentStatus when document translation completed, this
@@ -896,7 +910,7 @@ def translate_document(
896910
:param target_lang: Language code to translate document into, for
897911
example "DE", "EN-US", "FR".
898912
:param formality: (Optional) Desired formality for translation, as
899-
Formality enum, "less" or "more".
913+
Formality enum, "less", "more", "prefer_less", or "prefer_more".
900914
:param glossary: (Optional) glossary or glossary ID to use for
901915
translation. Must match specified source_lang and target_lang.
902916
:param filename: (Optional) Filename including extension, only required
@@ -953,7 +967,7 @@ def translate_document_upload(
953967
:param target_lang: Language code to translate document into, for
954968
example "DE", "EN-US", "FR".
955969
:param formality: (Optional) Desired formality for translation, as
956-
Formality enum, "less" or "more".
970+
Formality enum, "less", "more", "prefer_less", or "prefer_more".
957971
:param glossary: (Optional) glossary or glossary ID to use for
958972
translation. Must match specified source_lang and target_lang.
959973
:param filename: (Optional) Filename including extension, only required

tests/test_cli.py

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def test_text(runner):
9797
# Test text options
9898
extra_options = [
9999
("--formality more", "'formality': 'more'"),
100+
("--formality prefer_less", "'formality': 'prefer_less'"),
100101
("--split-sentences 0", "'split_sentences': '0'"),
101102
("--preserve-formatting", "'preserve_formatting': '1'"),
102103
("--tag-handling xml", "'tag_handling': 'xml'"),

tests/test_translate_document.py

+15
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ def test_translate_document_formality(
108108
**default_lang_args,
109109
)
110110
assert "Wie geht es dir?" == output_document_path.read_text()
111+
example_document_path.write_text("How are you?")
112+
translator.translate_document_from_filepath(
113+
example_document_path,
114+
output_document_path,
115+
formality=deepl.Formality.PREFER_MORE,
116+
**default_lang_args,
117+
)
118+
assert "Wie geht es Ihnen?" == output_document_path.read_text()
119+
translator.translate_document_from_filepath(
120+
example_document_path,
121+
output_document_path,
122+
formality=deepl.Formality.PREFER_LESS,
123+
**default_lang_args,
124+
)
125+
assert "Wie geht es dir?" == output_document_path.read_text()
111126

112127

113128
def test_document_failure_during_translation(

tests/test_translate_text.py

+24
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,30 @@ def test_formality(translator, server):
200200
"Test", target_lang="EN-US", formality="more"
201201
)
202202

203+
result = translator.translate_text(
204+
input_text, target_lang="DE", formality=deepl.Formality.PREFER_LESS
205+
)
206+
if not server.is_mock_server:
207+
assert informal == result.text
208+
209+
result = translator.translate_text(
210+
input_text, target_lang="DE", formality=deepl.Formality.PREFER_MORE
211+
)
212+
if not server.is_mock_server:
213+
assert formal == result.text
214+
215+
# Using prefer_ * with a language that does not support formality is not
216+
# an error
217+
translator.translate_text(
218+
input_text, target_lang="TR", formality=deepl.Formality.PREFER_MORE
219+
)
220+
with pytest.raises(
221+
deepl.DeepLException, match=r".*formality.*target_lang.*"
222+
):
223+
_ = translator.translate_text(
224+
input_text, target_lang="TR", formality="more"
225+
)
226+
203227

204228
def test_preserve_formatting(translator):
205229
# Note: this test may use the mock server that will not translate the text,

0 commit comments

Comments
 (0)