Skip to content

[Feature]: Normalized translation return object/dict #139

Description

@jbscout

Expect to happened

It would be very useful if there was a way to get a normalized return object/dict from a translation request.

I like that translate_text() returns a dict of the raw data from the translator.
The problem is that each translator returns a dict that is vastly different from every other translator's dict format.

Bing returns:

{'detectedLanguage': {'language': 'da', 'score': 1.0}, 'translations': [{'text': 'Letter from the Danish Environmental Protection Agency (2023_05)', 'to': 'en', 'sentLen': {'srcSentLen': [33], 'transSentLen': [64]}}]}

Google returns:

{'data': [[None, None, 'da', [[[0, [[[None, 33]], [True]]]], 33], [['Brev fra Miljøstyrelsen (2023_05)', None, None, 33]], None, ['Brev fra Miljøstyrelsen (2023_05)', 'auto', 'en', True]], [[[None, None, None, None, None, [['Letter from the Danish Environmental Protection Agency (2023_05)', None, None, None, [['Letter from the Danish Environmental Protection Agency (2023_05)', [5], []], ['Letter from the Danish Environmental Protection Agency (2023_05)', [11], []]]]], None, None, None, []]], 'en', 1, 'da', ['Brev fra Miljøstyrelsen (2023_05)', 'auto', 'en', True]], 'da']}

This makes it very difficult to parse the returned value of translate_text() if you switch translators, or if they decide to change their return format.

As the project's goal is to make translation agnostic of which translator I use, it would be nice if the the project's API provided me with a consistently formatted return value (either via translate_text() or a new function ). The API could parse and map the translator's returned dict into a normalized dict.

What I am looking for is translate_text() to return a dict, regardless of what translation engine was used, with the following keys

  • detectedLanguage (the detected, not "auto")
  • detectedLanguage_score
  • targetLanguage
  • originalText
  • translatedText
  • translatorUsed
  • rawReturnedDict (maybe, which would be the dict currently returned from translate_text() )

Maybe make a new function call that does this. So, that translate_text() remains backwards compatible. Or, put in an input parameter in the **kwargs (e.g., :param if_normalize_dict: bool, default False) that changes the returned dict from the current type to the normalized type.

Another option is a host of functions that provide the same information atomically from the last time translate_text() was run.

For example, detectLanugage(), translatedText(), translatorUsed(), etc.

Thank you

Expected APP Version

next newest version

Expected Python Version

=3.8 (Default)

Expected Runtime Environment

NoArch (Default)

Country/Region

Denmark

Expected Output

{'detectedLanguage': 'da', 'detectedLanguage_score': 1.0, 'targetLanguage': 'en', 'originalText':'Brev fra Miljøstyrelsen (2023_05)', 'translatedText':'Letter from the Danish Environmental Protection Agency (2023_05)', 'translatorUsed':'bing', 'rawReturnedDict': {'detectedLanguage': {'language': 'da', 'score': 1.0}, 'translations': [{'text': 'Letter from the Danish Environmental Protection Agency (2023_05)', 'to': 'en', 'sentLen': {'srcSentLen': [33], 'transSentLen': [64]}}]}}

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions