Chris
commited on
Commit
·
fdb2846
1
Parent(s):
1616574
minor refactoring and renamed cli.py to __main__ to reflect its true purpose
Browse files- deep_translator/__init__.py +25 -26
- deep_translator/{cli.py → __main__.py} +38 -53
- deep_translator/deepl.py +3 -3
- deep_translator/linguee.py +5 -5
- deep_translator/mymemory.py +4 -4
- deep_translator/parent.py +0 -2
- deep_translator/pons.py +5 -5
- deep_translator/yandex.py +1 -3
deep_translator/__init__.py
CHANGED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
-
"""Top-level package for
|
| 2 |
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
-
from deep_translator.cli import main
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
DeepL,
|
| 27 |
-
main,
|
| 28 |
-
single_detection,
|
| 29 |
-
batch_detection]
|
|
|
|
| 1 |
+
"""Top-level package for Deep Translator"""
|
| 2 |
|
| 3 |
+
from google_trans import GoogleTranslator
|
| 4 |
+
from pons import PonsTranslator
|
| 5 |
+
from linguee import LingueeTranslator
|
| 6 |
+
from mymemory import MyMemoryTranslator
|
| 7 |
+
from yandex import YandexTranslator
|
| 8 |
+
from qcri import QCRI
|
| 9 |
+
from deepl import DeepL
|
| 10 |
+
from detection import single_detection, batch_detection
|
| 11 |
+
from microsoft import MicrosoftTranslator
|
|
|
|
| 12 |
|
| 13 |
+
# These should be declared in setup.cfg, setting them here is redundant
|
| 14 |
+
# __author__ = """Nidhal Baccouri"""
|
| 15 |
+
# __email__ = '[email protected]'
|
| 16 |
+
# __version__ = '1.4.4'
|
| 17 |
|
| 18 |
+
# __all__ = [GoogleTranslator,
|
| 19 |
+
# PonsTranslator,
|
| 20 |
+
# LingueeTranslator,
|
| 21 |
+
# MyMemoryTranslator,
|
| 22 |
+
# YandexTranslator,
|
| 23 |
+
# MicrosoftTranslator,
|
| 24 |
+
# QCRI,
|
| 25 |
+
# DeepL,
|
| 26 |
+
# main,
|
| 27 |
+
# single_detection,
|
| 28 |
+
# batch_detection]
|
|
|
|
|
|
|
|
|
|
|
|
deep_translator/{cli.py → __main__.py}
RENAMED
|
@@ -13,63 +13,31 @@ from papago import PapagoTranslator
|
|
| 13 |
|
| 14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
| 15 |
|
| 16 |
-
|
| 17 |
-
"""
|
| 18 |
-
Use TRANSLATOR to translate source material into another language.\n
|
| 19 |
-
Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.\n
|
| 20 |
-
\n
|
| 21 |
-
\f
|
| 22 |
-
function responsible for parsing terminal arguments and provide them for
|
| 23 |
-
further use in the translation process
|
| 24 |
-
"""
|
| 25 |
-
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
| 26 |
-
if translator in api_key_required and not api_key:
|
| 27 |
-
click.echo(
|
| 28 |
-
"This translator requires an api key provided through --api-key"
|
| 29 |
-
)
|
| 30 |
-
elif languages:
|
| 31 |
-
print_supported_languages(translator, api_key)
|
| 32 |
-
else:
|
| 33 |
-
translate(translator, source, target, text, api_key)
|
| 34 |
-
# sys.exit()
|
| 35 |
-
|
| 36 |
-
@click.command(context_settings=CONTEXT_SETTINGS)
|
| 37 |
-
# @click.option(
|
| 38 |
-
# "--translator",
|
| 39 |
-
# "-trans",
|
| 40 |
-
# default="google",
|
| 41 |
-
# type=str,
|
| 42 |
-
# help="name of the translator you want to use",
|
| 43 |
-
# show_default=True,
|
| 44 |
-
# )
|
| 45 |
-
|
| 46 |
@click.argument(
|
| 47 |
-
'translator',
|
| 48 |
-
required=True,
|
| 49 |
-
default='google',
|
| 50 |
-
type=str
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
@click.option(
|
| 54 |
-
"--source",
|
| 55 |
-
"-src",
|
| 56 |
-
required=True,
|
| 57 |
-
type=str,
|
| 58 |
-
help="source language to translate from"
|
| 59 |
-
)
|
| 60 |
@click.option(
|
| 61 |
-
"--target",
|
| 62 |
-
"-tgt",
|
| 63 |
-
required=True,
|
| 64 |
-
type=str,
|
| 65 |
-
help="target language to translate to"
|
| 66 |
-
)
|
| 67 |
@click.option(
|
| 68 |
-
"--text",
|
| 69 |
-
"-txt",
|
| 70 |
type=str,
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
@click.option(
|
| 74 |
"--api-key",
|
| 75 |
type=str,
|
|
@@ -82,6 +50,23 @@ def main(translator, source, target, text, api_key, languages):
|
|
| 82 |
help="list all the languages available with the translator."
|
| 83 |
" Run with deep_translator <translator service> -lang",
|
| 84 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def translate(translator, source, target, text, api_key):
|
| 87 |
"""
|
|
@@ -171,4 +156,4 @@ def print_supported_languages(requested_translator, api_key):
|
|
| 171 |
click.echo(f"|- {k}: {v}")
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
-
|
|
|
|
| 13 |
|
| 14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
| 15 |
|
| 16 |
+
@click.command(name='Deep Translator', context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@click.argument(
|
| 18 |
+
'translator',
|
| 19 |
+
required=True,
|
| 20 |
+
default='google',
|
| 21 |
+
type=str)
|
|
|
|
|
|
|
| 22 |
@click.option(
|
| 23 |
+
"--source",
|
| 24 |
+
"-src",
|
| 25 |
+
required=True,
|
| 26 |
+
type=str,
|
| 27 |
+
help="source language to translate from")
|
|
|
|
| 28 |
@click.option(
|
| 29 |
+
"--target",
|
| 30 |
+
"-tgt",
|
| 31 |
+
required=True,
|
| 32 |
+
type=str,
|
| 33 |
+
help="target language to translate to")
|
|
|
|
| 34 |
@click.option(
|
| 35 |
+
"--text",
|
| 36 |
+
"-txt",
|
| 37 |
type=str,
|
| 38 |
+
required = True,
|
| 39 |
+
prompt="Enter the text you want to translate",
|
| 40 |
+
help="text you want to translate")
|
| 41 |
@click.option(
|
| 42 |
"--api-key",
|
| 43 |
type=str,
|
|
|
|
| 50 |
help="list all the languages available with the translator."
|
| 51 |
" Run with deep_translator <translator service> -lang",
|
| 52 |
)
|
| 53 |
+
def deep_translator(translator, source, target, text, api_key, languages):
|
| 54 |
+
"""
|
| 55 |
+
Use TRANSLATOR to translate source material into another language.\n
|
| 56 |
+
Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.\n
|
| 57 |
+
\f
|
| 58 |
+
function responsible for parsing terminal arguments and provide them for
|
| 59 |
+
further use in the translation process
|
| 60 |
+
"""
|
| 61 |
+
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
| 62 |
+
if translator in api_key_required and not api_key:
|
| 63 |
+
click.echo(
|
| 64 |
+
"This translator requires an api key provided through --api-key"
|
| 65 |
+
)
|
| 66 |
+
elif languages:
|
| 67 |
+
print_supported_languages(translator, api_key)
|
| 68 |
+
else:
|
| 69 |
+
translate(translator, source, target, text, api_key)
|
| 70 |
|
| 71 |
def translate(translator, source, target, text, api_key):
|
| 72 |
"""
|
|
|
|
| 156 |
click.echo(f"|- {k}: {v}")
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
+
deep_translator()
|
deep_translator/deepl.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import requests
|
| 2 |
from constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
| 3 |
from exceptions import (ServerException,
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
|
| 9 |
class DeepL(object):
|
|
|
|
| 1 |
import requests
|
| 2 |
from constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
| 3 |
from exceptions import (ServerException,
|
| 4 |
+
TranslationNotFound,
|
| 5 |
+
LanguageNotSupportedException,
|
| 6 |
+
AuthorizationException)
|
| 7 |
|
| 8 |
|
| 9 |
class DeepL(object):
|
deep_translator/linguee.py
CHANGED
|
@@ -4,11 +4,11 @@ linguee translator API
|
|
| 4 |
|
| 5 |
from constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES, LINGUEE_CODE_TO_LANGUAGE
|
| 6 |
from exceptions import (LanguageNotSupportedException,
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
from parent import BaseTranslator
|
| 13 |
from bs4 import BeautifulSoup
|
| 14 |
import requests
|
|
|
|
| 4 |
|
| 5 |
from constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES, LINGUEE_CODE_TO_LANGUAGE
|
| 6 |
from exceptions import (LanguageNotSupportedException,
|
| 7 |
+
TranslationNotFound,
|
| 8 |
+
NotValidPayload,
|
| 9 |
+
ElementNotFoundInGetRequest,
|
| 10 |
+
RequestError,
|
| 11 |
+
TooManyRequests)
|
| 12 |
from parent import BaseTranslator
|
| 13 |
from bs4 import BeautifulSoup
|
| 14 |
import requests
|
deep_translator/mymemory.py
CHANGED
|
@@ -6,10 +6,10 @@ import warnings
|
|
| 6 |
|
| 7 |
from constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 8 |
from exceptions import (NotValidPayload,
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
from parent import BaseTranslator
|
| 14 |
import requests
|
| 15 |
from time import sleep
|
|
|
|
| 6 |
|
| 7 |
from constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 8 |
from exceptions import (NotValidPayload,
|
| 9 |
+
TranslationNotFound,
|
| 10 |
+
LanguageNotSupportedException,
|
| 11 |
+
RequestError,
|
| 12 |
+
TooManyRequests)
|
| 13 |
from parent import BaseTranslator
|
| 14 |
import requests
|
| 15 |
from time import sleep
|
deep_translator/parent.py
CHANGED
|
@@ -3,8 +3,6 @@
|
|
| 3 |
from exceptions import NotValidPayload, NotValidLength, InvalidSourceOrTargetLanguage
|
| 4 |
from abc import ABC, abstractmethod
|
| 5 |
import string
|
| 6 |
-
|
| 7 |
-
|
| 8 |
class BaseTranslator(ABC):
|
| 9 |
"""
|
| 10 |
Abstract class that serve as a parent translator for other different translators
|
|
|
|
| 3 |
from exceptions import NotValidPayload, NotValidLength, InvalidSourceOrTargetLanguage
|
| 4 |
from abc import ABC, abstractmethod
|
| 5 |
import string
|
|
|
|
|
|
|
| 6 |
class BaseTranslator(ABC):
|
| 7 |
"""
|
| 8 |
Abstract class that serve as a parent translator for other different translators
|
deep_translator/pons.py
CHANGED
|
@@ -5,11 +5,11 @@ from bs4 import BeautifulSoup
|
|
| 5 |
import requests
|
| 6 |
from constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
| 7 |
from exceptions import (LanguageNotSupportedException,
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
from parent import BaseTranslator
|
| 14 |
from requests.utils import requote_uri
|
| 15 |
|
|
|
|
| 5 |
import requests
|
| 6 |
from constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
| 7 |
from exceptions import (LanguageNotSupportedException,
|
| 8 |
+
TranslationNotFound,
|
| 9 |
+
NotValidPayload,
|
| 10 |
+
ElementNotFoundInGetRequest,
|
| 11 |
+
RequestError,
|
| 12 |
+
TooManyRequests)
|
| 13 |
from parent import BaseTranslator
|
| 14 |
from requests.utils import requote_uri
|
| 15 |
|
deep_translator/yandex.py
CHANGED
|
@@ -3,9 +3,7 @@ Yandex translator API
|
|
| 3 |
"""
|
| 4 |
import requests
|
| 5 |
from constants import BASE_URLS
|
| 6 |
-
from exceptions import (RequestError,
|
| 7 |
-
ServerException, TranslationNotFound, TooManyRequests)
|
| 8 |
-
|
| 9 |
|
| 10 |
class YandexTranslator(object):
|
| 11 |
"""
|
|
|
|
| 3 |
"""
|
| 4 |
import requests
|
| 5 |
from constants import BASE_URLS
|
| 6 |
+
from exceptions import (RequestError, ServerException, TranslationNotFound, TooManyRequests)
|
|
|
|
|
|
|
| 7 |
|
| 8 |
class YandexTranslator(object):
|
| 9 |
"""
|