|
| 1 | +# Copyright 2025 DeepL SE (https://www.deepl.com) |
| 2 | +# Use of this source code is governed by an MIT |
| 3 | +# license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +from deepl.api_data import ( |
| 6 | + Language, |
| 7 | + WriteResult, |
| 8 | +) |
| 9 | +from deepl.translator import Translator |
| 10 | +from typing import ( |
| 11 | + Dict, |
| 12 | + Iterable, |
| 13 | + List, |
| 14 | + Optional, |
| 15 | + Union, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class DeepLClient(Translator): |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + auth_key: str, |
| 23 | + *, |
| 24 | + server_url: Optional[str] = None, |
| 25 | + proxy: Union[Dict, str, None] = None, |
| 26 | + send_platform_info: bool = True, |
| 27 | + verify_ssl: Union[bool, str, None] = None, |
| 28 | + skip_language_check: bool = False, |
| 29 | + ): |
| 30 | + super().__init__( |
| 31 | + auth_key, |
| 32 | + server_url=server_url, |
| 33 | + proxy=proxy, |
| 34 | + send_platform_info=send_platform_info, |
| 35 | + verify_ssl=verify_ssl, |
| 36 | + skip_language_check=skip_language_check, |
| 37 | + ) |
| 38 | + |
| 39 | + def rephrase_text( |
| 40 | + self, |
| 41 | + text: Union[str, Iterable[str]], |
| 42 | + *, |
| 43 | + target_lang: Union[str, Language], |
| 44 | + ) -> Union[WriteResult, List[WriteResult]]: |
| 45 | + """Improve the text(s) and optionally convert them to the variant of |
| 46 | + the `target_lang` (requires source lang to match target_lang, excluding |
| 47 | + variants). |
| 48 | +
|
| 49 | + :param text: Text to improve. |
| 50 | + :type text: UTF-8 :class:`str`; string sequence (list, tuple, iterator, |
| 51 | + generator) |
| 52 | + :param target_lang: language code the final text should be in, for |
| 53 | + example "DE", "EN-US", "FR". |
| 54 | + :return: List of WriteResult objects containing results, unless input |
| 55 | + text was one string, then a single WriteResult object is returned. |
| 56 | + """ |
| 57 | + |
| 58 | + if isinstance(text, str): |
| 59 | + if len(text) == 0: |
| 60 | + raise ValueError("text must not be empty") |
| 61 | + text = [text] |
| 62 | + multi_input = False |
| 63 | + elif hasattr(text, "__iter__"): |
| 64 | + multi_input = True |
| 65 | + text = list(text) |
| 66 | + else: |
| 67 | + raise TypeError( |
| 68 | + "text parameter must be a string or an iterable of strings" |
| 69 | + ) |
| 70 | + |
| 71 | + request_data = {"target_lang": target_lang, "text": text} |
| 72 | + |
| 73 | + status, content, json = self._api_call( |
| 74 | + "v2/write/rephrase", json=request_data |
| 75 | + ) |
| 76 | + |
| 77 | + self._raise_for_status(status, content, json) |
| 78 | + |
| 79 | + improvements = ( |
| 80 | + json.get("improvements", []) |
| 81 | + if (json and isinstance(json, dict)) |
| 82 | + else [] |
| 83 | + ) |
| 84 | + output = [] |
| 85 | + for improvement in improvements: |
| 86 | + text = improvement.get("text", "") if improvement else "" |
| 87 | + detected_source_language = ( |
| 88 | + improvement.get("detected_source_language", "") |
| 89 | + if improvement |
| 90 | + else "" |
| 91 | + ) |
| 92 | + target_language = ( |
| 93 | + improvement.get("target_language", "") if improvement else "" |
| 94 | + ) |
| 95 | + output.append( |
| 96 | + WriteResult(text, detected_source_language, target_language) |
| 97 | + ) |
| 98 | + |
| 99 | + return output if multi_input else output[0] |
0 commit comments