Skip to content

Commit efd75f8

Browse files
feat: add show_billed_characters option to translate_text()
1 parent 109bae0 commit efd75f8

7 files changed

+51
-10
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ 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+
* Added `billed_characters` to response from `translate_text()`.
11+
12+
813
## [1.18.0] - 2024-04-26
914
### Added
1015
* New language available: Arabic (`'ar'`). Add language code constants and tests.
@@ -294,6 +299,7 @@ Version increased to avoid conflicts with old packages on PyPI.
294299
Initial version.
295300

296301

302+
[Unreleased]: https://github.com/DeepLcom/deepl-python/compare/v1.18.0...HEAD
297303
[1.18.0]: https://github.com/DeepLcom/deepl-python/compare/v1.17.0...v1.18.0
298304
[1.17.0]: https://github.com/DeepLcom/deepl-python/compare/v1.16.1...v1.17.0
299305
[1.16.1]: https://github.com/DeepLcom/deepl-python/compare/v1.16.0...v1.16.1

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ There are additional optional arguments to control translation, see
9696
[Text translation options](#text-translation-options) below.
9797

9898
`translate_text()` returns a `TextResult`, or a list of `TextResult`s
99-
corresponding to your input text(s). `TextResult` has two properties: `text` is
100-
the translated text, and `detected_source_lang` is the detected source language
101-
code.
99+
corresponding to your input text(s). `TextResult` has the following properties:
100+
- `text` is the translated text,
101+
- `detected_source_lang` is the detected source language code,
102+
- `billed_characters` is the number of characters billed for the translation.
102103

103104
```python
104105
# Translate text into a target language, in this case, French:
@@ -107,12 +108,15 @@ print(result.text) # "Bonjour, le monde !"
107108

108109
# Translate multiple texts into British English
109110
result = translator.translate_text(
110-
["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB"
111+
["お元気ですか?", "¿Cómo estás?"],
112+
target_lang="EN-GB",
111113
)
112114
print(result[0].text) # "How are you?"
113115
print(result[0].detected_source_lang) # "JA" the language code for Japanese
116+
print(result[0].billed_characters) # 7 - the number of characters in the source text "お元気ですか?"
114117
print(result[1].text) # "How are you?"
115118
print(result[1].detected_source_lang) # "ES" the language code for Spanish
119+
print(result[1].billed_characters) # 12 - the number of characters in the source text "¿Cómo estás?"
116120

117121
# Translate into German with less and more Formality:
118122
print(

deepl/__main__.py

+14
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def action_document(
7878
def action_text(
7979
translator: deepl.Translator,
8080
show_detected_source: bool = False,
81+
show_billed_characters: Optional[bool] = None,
8182
**kwargs,
8283
):
8384
"""Action function for the text command."""
@@ -88,6 +89,13 @@ def action_text(
8889
for output in output_list:
8990
if show_detected_source:
9091
print(f"Detected source language: {output.detected_source_lang}")
92+
if show_billed_characters:
93+
text_value = (
94+
"unknown"
95+
if output.billed_characters is None
96+
else output.billed_characters
97+
)
98+
print(f"Billed characters: {text_value}")
9199
print(output.text)
92100

93101

@@ -311,6 +319,12 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
311319
action="store_true",
312320
help="leave original formatting unchanged during translation",
313321
)
322+
parser_text.add_argument(
323+
"--show-billed-characters",
324+
dest="show_billed_characters",
325+
action="store_true",
326+
help="print billed characters for each text",
327+
)
314328
parser_text.add_argument(
315329
"text",
316330
nargs="+",

deepl/api_data.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
class TextResult:
1313
"""Holds the result of a text translation request."""
1414

15-
def __init__(self, text: str, detected_source_lang: str):
15+
def __init__(
16+
self,
17+
text: str,
18+
detected_source_lang: str,
19+
billed_characters: int,
20+
):
1621
self.text = text
1722
self.detected_source_lang = detected_source_lang
23+
self.billed_characters = billed_characters
1824

1925
def __str__(self):
2026
return self.text

deepl/translator.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ def translate_text(
411411
)
412412
request_data["text"] = text
413413

414+
# Always send show_billed_characters=true, remove when the API default
415+
# is changed to true
416+
request_data["show_billed_characters"] = True
417+
414418
if context is not None:
415419
request_data["context"] = context
416420
if split_sentences is not None:
@@ -452,12 +456,17 @@ def join_tags(tag_argument: Union[str, Iterable[str]]) -> List[str]:
452456
output = []
453457
for translation in translations:
454458
text = translation.get("text", "") if translation else ""
455-
lang = (
459+
detected_source_language = (
456460
translation.get("detected_source_language", "")
457461
if translation
458462
else ""
459463
)
460-
output.append(TextResult(text, detected_source_lang=lang))
464+
billed_characters = util.get_int_safe(
465+
translation, "billed_characters"
466+
)
467+
output.append(
468+
TextResult(text, detected_source_language, billed_characters)
469+
)
461470

462471
return output if multi_input else output[0]
463472

tests/test_general.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ def test_example_translation(lang, translator):
3232

3333
input_text = example_text[lang]
3434
source_lang = deepl.Language.remove_regional_variant(lang)
35-
result_text = translator.translate_text(
35+
result = (translator.translate_text(
3636
input_text, source_lang=source_lang, target_lang="EN-US"
37-
).text.lower()
38-
assert "proton" in result_text
37+
))
38+
assert "proton" in result.text.lower()
39+
assert result.billed_characters == len(input_text)
3940

4041

4142
@needs_real_server

tests/test_translate_text.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def test_single_text(translator):
1414
assert example_text["DE"] == result.text
1515
assert "EN" == result.detected_source_lang
1616
assert example_text["DE"] == str(result)
17+
assert result.billed_characters == len(example_text["EN"])
1718

1819

1920
def test_string_list(translator):

0 commit comments

Comments
 (0)