diff --git a/reverso_context_api/client.py b/reverso_context_api/client.py index 485f4c0..148d613 100644 --- a/reverso_context_api/client.py +++ b/reverso_context_api/client.py @@ -21,17 +21,25 @@ 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() - for entry in contents["dictionary_entry_list"]: - yield entry["term"] + + if return_contents == True: + return contents + + 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