Skip to content

Commit c512702

Browse files
committed
feat: Add style and tones to write API
1 parent 4107220 commit c512702

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

deepl/api_data.py

+46
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,49 @@ class ModelType(Enum):
452452

453453
def __str__(self):
454454
return self.value
455+
456+
457+
class WritingStyle(Enum):
458+
"""Options for the `style` parameter of the Write API.
459+
Sets the style the improved text should be in. Note that currently, only
460+
a style OR a tone is supported.
461+
462+
When using a style starting with `prefer_`, the style will only be used
463+
if the chosen or detected language supports it.
464+
"""
465+
466+
ACADEMIC = "academic"
467+
BUSINESS = "business"
468+
CASUAL = "casual"
469+
DEFAULT = "default"
470+
PREFER_ACADEMIC = "prefer_academic"
471+
PREFER_BUSINESS = "prefer_business"
472+
PREFER_CASUAL = "prefer_casual"
473+
PREFER_SIMPLE = "prefer_simple"
474+
SIMPLE = "simple"
475+
476+
def __str__(self):
477+
return self.value
478+
479+
480+
class WritingTone(Enum):
481+
"""Options for the `tone` parameter of the Write API.
482+
Sets the tone the improved text should be in. Note that currently, only
483+
a style OR a tone is supported.
484+
485+
When using a tone starting with `prefer_`, the tone will only be used
486+
if the chosen or detected language supports it.
487+
"""
488+
489+
CONFIDENT = "confident"
490+
DEFAULT = "default"
491+
DIPLOMATIC = "diplomatic"
492+
ENTHUSIASTIC = "enthusiastic"
493+
FRIENDLY = "friendly"
494+
PREFER_CONFIDENT = "prefer_confident"
495+
PREFER_DIPLOMATIC = "prefer_diplomatic"
496+
PREFER_ENTHUSIASTIC = "prefer_enthusiastic"
497+
PREFER_FRIENDLY = "prefer_friendly"
498+
499+
def __str__(self):
500+
return self.value

deepl/deepl_client.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def rephrase_text(
4040
self,
4141
text: Union[str, Iterable[str]],
4242
*,
43-
target_lang: Union[str, Language],
43+
target_lang: Union[None, str, Language] = None,
44+
style: Optional[str] = None,
45+
tone: Optional[str] = None,
4446
) -> Union[WriteResult, List[WriteResult]]:
4547
"""Improve the text(s) and optionally convert them to the variant of
4648
the `target_lang` (requires source lang to match target_lang, excluding
@@ -51,6 +53,10 @@ def rephrase_text(
5153
generator)
5254
:param target_lang: language code the final text should be in, for
5355
example "DE", "EN-US", "FR".
56+
:param style: Writing style to be used for the improvement. Either
57+
style OR tone can be used.
58+
:param tone: Tone to be used for the improvement. Either style OR tone
59+
can be used.
5460
:return: List of WriteResult objects containing results, unless input
5561
text was one string, then a single WriteResult object is returned.
5662
"""
@@ -68,7 +74,13 @@ def rephrase_text(
6874
"text parameter must be a string or an iterable of strings"
6975
)
7076

71-
request_data = {"target_lang": target_lang, "text": text}
77+
request_data: dict = {"text": text}
78+
if target_lang:
79+
request_data["target_lang"] = target_lang
80+
if style:
81+
request_data["writing_style"] = style
82+
if tone:
83+
request_data["tone"] = tone
7284

7385
status, content, json = self._api_call(
7486
"v2/write/rephrase", json=request_data

0 commit comments

Comments
 (0)