forked from nidhaloff/deep-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nidhaloff#130 from Vincent-Stragier/master
Autogenerate the a dictionary of translators to hide class inheritance inconsistancy.
- Loading branch information
Showing
8 changed files
with
104 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from .microsoft import MicrosoftTranslator | ||
from .papago import PapagoTranslator | ||
from .libre import LibreTranslator | ||
from .engine import generate_engines_dict, engine | ||
|
||
__author__ = """Nidhal Baccouri""" | ||
__email__ = '[email protected]' | ||
|
@@ -29,4 +30,8 @@ | |
"PapagoTranslator", | ||
"single_detection", | ||
"batch_detection" | ||
] | ||
] | ||
|
||
__engines__ = generate_engines_dict(__all__, locals()) | ||
del generate_engines_dict | ||
engine.translation_engines = __engines__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from .parent import BaseTranslator | ||
|
||
# BaseTranslator.register(YandexTranslator) | ||
# BaseTranslator.register(QCRI) | ||
# BaseTranslator.register(DeepL) | ||
# BaseTranslator.register(MicrosoftTranslator) | ||
# BaseTranslator.register(PapagoTranslator) | ||
|
||
|
||
def generate_engines_dict(_all: list, _locals: dict) -> dict: | ||
base_translator_type = BaseTranslator | ||
|
||
def is_translator(__object: object) -> bool: | ||
try: | ||
return issubclass(__object, base_translator_type) | ||
except TypeError: | ||
return False | ||
|
||
translation_engines = {} | ||
for _object in _all: | ||
__object = _locals.get(_object, 'failed') | ||
key_name = _object.replace('Translator', '').lower() | ||
if is_translator(__object): | ||
translation_engines.update({key_name: __object}) | ||
return translation_engines | ||
|
||
|
||
def engine(engine_name: str, *args, **kwargs) -> BaseTranslator: | ||
"""Return translation engine. | ||
Free and keyless engines are 'google', 'pons', 'linguee', 'mymemory', | ||
'libre'. | ||
Args: | ||
engine_name: the name of the engine | ||
*args: positional argument to pass to the engine | ||
**kwargs: named argument to pass to the engine | ||
Return: | ||
A translation engine | ||
""" | ||
try: | ||
return engine.translation_engines[engine_name.lower()](*args, **kwargs) | ||
except KeyError: | ||
keys = '\', \''.join(engine.translation_engines.keys()) | ||
raise(KeyError(f'Please provide a valid engine name (\'{keys}\')')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters