From 7e83783148a6c150fbee199b3e88e95259ae7ecc Mon Sep 17 00:00:00 2001 From: Stanislav Nemytov <45829297+StasNemy@users.noreply.github.com> Date: Thu, 29 May 2025 08:16:20 +0200 Subject: [PATCH 1/2] Enhance get_translations() to optionally return full contents --- reverso_context_api/client.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/reverso_context_api/client.py b/reverso_context_api/client.py index 485f4c0..60e1db7 100644 --- a/reverso_context_api/client.py +++ b/reverso_context_api/client.py @@ -21,15 +21,23 @@ def __init__(self, source_lang, target_lang, credentials=None, user_agent=None): self._source_lang, self._target_lang = source_lang, target_lang self._session = ReversoSession(credentials=credentials, user_agent=user_agent) - def get_translations(self, text, source_lang=None, target_lang=None): - """Yields found translations of word (without context) + def get_translations(self, text, source_lang=None, target_lang=None, return_contents=False): + """Yields found translations of word (without context) or returns the full contents if `return_contents` is True. For example: >>> list(Client("de", "en").get_translations("braucht")) ['needed', 'required', 'need', 'takes', 'requires', 'take', 'necessary'...] + >>> contents = Client("de", "en").get_translations("braucht", return_contents=True) + >>> contents["dictionary_entry_list"] + [{'term': 'needed'}, {'term': 'required'}, {'term': 'need'}, ...] + + :param return_contents: If True, returns the full contents instead of yielding translations. """ r = self._request_translations(text, source_lang, target_lang) - contents = r.json() + + if return_contents == True: + return contents + for entry in contents["dictionary_entry_list"]: yield entry["term"] From 83134a29ba13917d592cb382e3f156d74a507619 Mon Sep 17 00:00:00 2001 From: Stanislav Nemytov <45829297+StasNemy@users.noreply.github.com> Date: Thu, 29 May 2025 11:35:48 +0200 Subject: [PATCH 2/2] Refactor get_translations() to return a list of terms instead of yielding them --- reverso_context_api/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reverso_context_api/client.py b/reverso_context_api/client.py index 60e1db7..148d613 100644 --- a/reverso_context_api/client.py +++ b/reverso_context_api/client.py @@ -38,8 +38,8 @@ def get_translations(self, text, source_lang=None, target_lang=None, return_cont if return_contents == True: return contents - for entry in contents["dictionary_entry_list"]: - yield entry["term"] + return [entry["term"] for entry in contents["dictionary_entry_list"]] + def get_translation_samples(self, text, target_text=None, source_lang=None, target_lang=None, cleanup=True): """Yields pairs (source_text, translation) of context for passed text