=
commited on
Commit
·
a6888ed
1
Parent(s):
d196c2c
work on deepL
Browse files- README.rst +5 -5
- deep_translator/__init__.py +2 -0
- deep_translator/constants.py +2 -1
- deep_translator/deepl.py +55 -0
- deep_translator/qcri.py +1 -2
README.rst
CHANGED
|
@@ -94,13 +94,13 @@ Why you should use it
|
|
| 94 |
Features
|
| 95 |
========
|
| 96 |
|
| 97 |
-
* Support for google translate
|
| 98 |
-
* Support for `Pons translator <https://de.pons.com
|
| 99 |
-
* Support for the `Linguee translator <https://www.linguee.com
|
| 100 |
-
* Support for the `Mymemory translator <https://mymemory.translated.net
|
| 101 |
* Support for the `Yandex translator <https://yandex.com/>`_ (version >= 1.2.1)
|
| 102 |
* Support for the `QCRI translator <https://mt.qcri.org/api/>`_ (version >= 1.2.4)
|
| 103 |
-
|
| 104 |
* Automatic single language detection
|
| 105 |
* Batch language detection
|
| 106 |
* Translate directly from a text file
|
|
|
|
| 94 |
Features
|
| 95 |
========
|
| 96 |
|
| 97 |
+
* Support for `google translate <https://translate.google.com/>`_
|
| 98 |
+
* Support for `Pons translator <https://de.pons.com/>`_
|
| 99 |
+
* Support for the `Linguee translator <https://www.linguee.com/>`_
|
| 100 |
+
* Support for the `Mymemory translator <https://mymemory.translated.net/>`_
|
| 101 |
* Support for the `Yandex translator <https://yandex.com/>`_ (version >= 1.2.1)
|
| 102 |
* Support for the `QCRI translator <https://mt.qcri.org/api/>`_ (version >= 1.2.4)
|
| 103 |
+
* Support for the `DeepL translator <https://www.deepl.com/en/translator/>`_ (version >= 1.2.4)
|
| 104 |
* Automatic single language detection
|
| 105 |
* Batch language detection
|
| 106 |
* Translate directly from a text file
|
deep_translator/__init__.py
CHANGED
|
@@ -6,6 +6,7 @@ from .linguee import LingueeTranslator
|
|
| 6 |
from .mymemory import MyMemoryTranslator
|
| 7 |
from .yandex import YandexTranslator
|
| 8 |
from .qcri import QCRI
|
|
|
|
| 9 |
from .detection import single_detection, batch_detection
|
| 10 |
|
| 11 |
|
|
@@ -19,5 +20,6 @@ __all__ = [GoogleTranslator,
|
|
| 19 |
MyMemoryTranslator,
|
| 20 |
YandexTranslator,
|
| 21 |
QCRI,
|
|
|
|
| 22 |
single_detection,
|
| 23 |
batch_detection]
|
|
|
|
| 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 |
|
| 12 |
|
|
|
|
| 20 |
MyMemoryTranslator,
|
| 21 |
YandexTranslator,
|
| 22 |
QCRI,
|
| 23 |
+
DeepL,
|
| 24 |
single_detection,
|
| 25 |
batch_detection]
|
deep_translator/constants.py
CHANGED
|
@@ -6,7 +6,8 @@ BASE_URLS = {
|
|
| 6 |
"YANDEX": "https://translate.yandex.net/api/{version}/tr.json/{endpoint}",
|
| 7 |
"LINGUEE": "https://www.linguee.com/",
|
| 8 |
"MYMEMORY": "http://api.mymemory.translated.net/get",
|
| 9 |
-
"QCRI": "https://mt.qcri.org/api/v1/{endpoint}?"
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
GOOGLE_CODES_TO_LANGUAGES = {
|
|
|
|
| 6 |
"YANDEX": "https://translate.yandex.net/api/{version}/tr.json/{endpoint}",
|
| 7 |
"LINGUEE": "https://www.linguee.com/",
|
| 8 |
"MYMEMORY": "http://api.mymemory.translated.net/get",
|
| 9 |
+
"QCRI": "https://mt.qcri.org/api/v1/{endpoint}?",
|
| 10 |
+
"DEEPL": "https://api.deepl.com/{}/"
|
| 11 |
}
|
| 12 |
|
| 13 |
GOOGLE_CODES_TO_LANGUAGES = {
|
deep_translator/deepl.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import requests
|
| 3 |
+
from requests.utils import requote_uri
|
| 4 |
+
from deep_translator.constants import BASE_URLS
|
| 5 |
+
from deep_translator.exceptions import (RequestError,
|
| 6 |
+
ServerException, TranslationNotFound, TooManyRequests)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class DeepL(object):
|
| 10 |
+
"""
|
| 11 |
+
class that wraps functions, which use the DeepL translator under the hood to translate word(s)
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def __init__(self, api_key=None):
|
| 15 |
+
"""
|
| 16 |
+
@param api_key: your DeepL api key. Get one here: https://www.deepl.com/docs-api/accessing-the-api/
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
if not api_key:
|
| 20 |
+
raise ServerException(401)
|
| 21 |
+
self.version = 'v2'
|
| 22 |
+
self.api_key = api_key
|
| 23 |
+
self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
|
| 24 |
+
|
| 25 |
+
def translate(self, source, target, text):
|
| 26 |
+
params = {
|
| 27 |
+
"auth_key": self.api_key,
|
| 28 |
+
"target_lang": target,
|
| 29 |
+
"source_lang": source,
|
| 30 |
+
"text": text
|
| 31 |
+
}
|
| 32 |
+
try:
|
| 33 |
+
response = requests.get(self.__base_url, params=params)
|
| 34 |
+
except ConnectionError:
|
| 35 |
+
raise ServerException(503)
|
| 36 |
+
|
| 37 |
+
else:
|
| 38 |
+
if response.status_code != 200:
|
| 39 |
+
ServerException(response.status_code)
|
| 40 |
+
else:
|
| 41 |
+
res = response.json()
|
| 42 |
+
if not res:
|
| 43 |
+
raise TranslationNotFound(text)
|
| 44 |
+
return res
|
| 45 |
+
|
| 46 |
+
def translate_batch(self, source, target, batch):
|
| 47 |
+
"""
|
| 48 |
+
translate a batch of texts
|
| 49 |
+
@param source: source language
|
| 50 |
+
@param target: target language
|
| 51 |
+
@param batch: list of texts to translate
|
| 52 |
+
@return: list of translations
|
| 53 |
+
"""
|
| 54 |
+
return [self.translate(source, target, text) for text in batch]
|
| 55 |
+
|
deep_translator/qcri.py
CHANGED
|
@@ -8,7 +8,7 @@ from deep_translator.exceptions import (RequestError,
|
|
| 8 |
|
| 9 |
class QCRI(object):
|
| 10 |
"""
|
| 11 |
-
class that wraps functions, which use the
|
| 12 |
"""
|
| 13 |
|
| 14 |
def __init__(self, api_key=None):
|
|
@@ -64,7 +64,6 @@ class QCRI(object):
|
|
| 64 |
"domain": domain,
|
| 65 |
"text": text
|
| 66 |
}
|
| 67 |
-
print("params: ", params)
|
| 68 |
try:
|
| 69 |
response = self._get("translate", params=params, return_text=False)
|
| 70 |
except ConnectionError:
|
|
|
|
| 8 |
|
| 9 |
class QCRI(object):
|
| 10 |
"""
|
| 11 |
+
class that wraps functions, which use the QRCI translator under the hood to translate word(s)
|
| 12 |
"""
|
| 13 |
|
| 14 |
def __init__(self, api_key=None):
|
|
|
|
| 64 |
"domain": domain,
|
| 65 |
"text": text
|
| 66 |
}
|
|
|
|
| 67 |
try:
|
| 68 |
response = self._get("translate", params=params, return_text=False)
|
| 69 |
except ConnectionError:
|