=
commited on
Commit
·
1f09504
1
Parent(s):
77f147b
added support for qcri
Browse files- README.rst +34 -0
- deep_translator/constants.py +2 -0
- deep_translator/exceptions.py +3 -3
- deep_translator/qcri.py +43 -96
- deep_translator/yandex.py +9 -9
- docs/usage.rst +33 -0
README.rst
CHANGED
|
@@ -99,6 +99,8 @@ Features
|
|
| 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 |
* Automatic single language detection
|
| 103 |
* Batch language detection
|
| 104 |
* Translate directly from a text file
|
|
@@ -272,6 +274,38 @@ Mymemory Translator
|
|
| 272 |
|
| 273 |
translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
Linguee Translator
|
| 277 |
-------------------
|
|
|
|
| 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
|
|
|
|
| 274 |
|
| 275 |
translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
|
| 276 |
|
| 277 |
+
QCRI Translator
|
| 278 |
+
--------------------
|
| 279 |
+
|
| 280 |
+
.. note::
|
| 281 |
+
|
| 282 |
+
In order to use the QCRI translator, you need to generate a free api key. Visit https://mt.qcri.org/api/
|
| 283 |
+
for more information
|
| 284 |
+
|
| 285 |
+
- Check languages
|
| 286 |
+
|
| 287 |
+
.. code-block:: python
|
| 288 |
+
|
| 289 |
+
# as a property
|
| 290 |
+
print("language pairs: ", QCRI("your_api_key").languages)
|
| 291 |
+
|
| 292 |
+
- Check domains
|
| 293 |
+
|
| 294 |
+
.. code-block:: python
|
| 295 |
+
|
| 296 |
+
# as a property
|
| 297 |
+
print("domains: ", QCRI("your_api_key").domains)
|
| 298 |
+
|
| 299 |
+
- Text translation
|
| 300 |
+
|
| 301 |
+
.. code-block:: python
|
| 302 |
+
|
| 303 |
+
text = 'Education is great'
|
| 304 |
+
|
| 305 |
+
translated = QCRI("your_api_key").translate(source='en', target='ar', domain="news", text=text)
|
| 306 |
+
# output -> التعليم هو عظيم
|
| 307 |
+
|
| 308 |
+
# see docs for batch translation and more.
|
| 309 |
|
| 310 |
Linguee Translator
|
| 311 |
-------------------
|
deep_translator/constants.py
CHANGED
|
@@ -178,3 +178,5 @@ LINGUEE_LANGUAGES_TO_CODES = {
|
|
| 178 |
}
|
| 179 |
|
| 180 |
LINGUEE_CODE_TO_LANGUAGE = {v: k for k, v in LINGUEE_LANGUAGES_TO_CODES.items()}
|
|
|
|
|
|
|
|
|
| 178 |
}
|
| 179 |
|
| 180 |
LINGUEE_CODE_TO_LANGUAGE = {v: k for k, v in LINGUEE_LANGUAGES_TO_CODES.items()}
|
| 181 |
+
|
| 182 |
+
# "72e9e2cc7c992db4dcbdd6fb9f91a0d1"
|
deep_translator/exceptions.py
CHANGED
|
@@ -93,7 +93,7 @@ class TooManyRequests(Exception):
|
|
| 93 |
return self.message
|
| 94 |
|
| 95 |
|
| 96 |
-
class
|
| 97 |
"""
|
| 98 |
Default YandexTranslate exception from the official website
|
| 99 |
"""
|
|
@@ -109,5 +109,5 @@ class YandexDefaultException(Exception):
|
|
| 109 |
}
|
| 110 |
|
| 111 |
def __init__(self, status_code, *args):
|
| 112 |
-
message = self.errors.get(status_code)
|
| 113 |
-
super(
|
|
|
|
| 93 |
return self.message
|
| 94 |
|
| 95 |
|
| 96 |
+
class ServerException(Exception):
|
| 97 |
"""
|
| 98 |
Default YandexTranslate exception from the official website
|
| 99 |
"""
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
def __init__(self, status_code, *args):
|
| 112 |
+
message = self.errors.get(status_code, "API server error")
|
| 113 |
+
super(ServerException, self).__init__(message, *args)
|
deep_translator/qcri.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
|
| 2 |
import requests
|
|
|
|
| 3 |
from deep_translator.constants import BASE_URLS
|
| 4 |
from deep_translator.exceptions import (RequestError,
|
| 5 |
-
|
| 6 |
|
| 7 |
|
| 8 |
-
class
|
| 9 |
"""
|
| 10 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
| 11 |
"""
|
|
@@ -15,125 +16,71 @@ class YandexTranslator(object):
|
|
| 15 |
@param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
|
| 16 |
"""
|
| 17 |
|
| 18 |
-
# key: "72e9e2cc7c992db4dcbdd6fb9f91a0d1"
|
| 19 |
if not api_key:
|
| 20 |
-
raise
|
| 21 |
-
self.__base_url = BASE_URLS.get("
|
| 22 |
|
| 23 |
self.api_key = api_key
|
| 24 |
-
self.api_version = "v1.5"
|
| 25 |
self.api_endpoints = {
|
| 26 |
-
"langs": "getLangs",
|
| 27 |
-
"detect": "detect",
|
| 28 |
-
"translate": "translate",
|
| 29 |
-
}
|
| 30 |
-
endpoints = {
|
| 31 |
"get_languages": "getLanguagePairs",
|
| 32 |
"get_domains": "getDomains",
|
| 33 |
"translate": "translate",
|
| 34 |
-
|
| 35 |
}
|
| 36 |
|
| 37 |
-
params = {
|
| 38 |
-
"key": api_key
|
| 39 |
}
|
| 40 |
-
pairs = requests.get(self.__base_url.format(endpoint=endpoints["get_languages"]), params=params)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def get_supported_languages(self):
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@property
|
| 48 |
def languages(self):
|
| 49 |
return self.get_supported_languages()
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
response = requests.get(url, params={"key": self.api_key}, proxies=proxies)
|
| 58 |
-
except requests.exceptions.ConnectionError:
|
| 59 |
-
raise YandexDefaultException(503)
|
| 60 |
-
else:
|
| 61 |
-
data = response.json()
|
| 62 |
-
|
| 63 |
-
if response.status_code != 200:
|
| 64 |
-
raise YandexDefaultException(response.status_code)
|
| 65 |
-
return data.get("dirs")
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
response = None
|
| 69 |
params = {
|
| 70 |
-
"text": text,
|
| 71 |
-
"format": "plain",
|
| 72 |
"key": self.api_key,
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
|
|
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
response = requests.post(url, data=params, proxies=proxies)
|
| 77 |
-
|
| 78 |
-
except RequestError:
|
| 79 |
-
raise
|
| 80 |
-
except ConnectionError:
|
| 81 |
-
raise YandexDefaultException(503)
|
| 82 |
-
except ValueError:
|
| 83 |
-
raise YandexDefaultException(response.status_code)
|
| 84 |
-
else:
|
| 85 |
-
response = response.json()
|
| 86 |
-
language = response['lang']
|
| 87 |
-
status_code = response['code']
|
| 88 |
-
if status_code != 200:
|
| 89 |
-
raise RequestError()
|
| 90 |
-
elif not language:
|
| 91 |
-
raise YandexDefaultException(501)
|
| 92 |
-
return language
|
| 93 |
-
|
| 94 |
-
def translate(self, source, target, text, proxies=None):
|
| 95 |
-
params = {
|
| 96 |
-
"text": text,
|
| 97 |
-
"format": "plain",
|
| 98 |
-
"lang": target if source == "auto" else "{}-{}".format(source, target),
|
| 99 |
-
"key": self.api_key
|
| 100 |
-
}
|
| 101 |
-
try:
|
| 102 |
-
url = self.__base_url.format(version=self.api_version, endpoint="translate")
|
| 103 |
-
response = requests.post(url, data=params, proxies=proxies)
|
| 104 |
except ConnectionError:
|
| 105 |
-
raise
|
| 106 |
-
else:
|
| 107 |
-
response = response.json()
|
| 108 |
-
|
| 109 |
-
if response['code'] == 429:
|
| 110 |
-
raise TooManyRequests()
|
| 111 |
-
|
| 112 |
-
if response['code'] != 200:
|
| 113 |
-
raise YandexDefaultException(response['code'])
|
| 114 |
-
|
| 115 |
-
if not response['text']:
|
| 116 |
-
raise TranslationNotFound()
|
| 117 |
-
|
| 118 |
-
return response['text']
|
| 119 |
-
|
| 120 |
-
def translate_file(self, source, target, path):
|
| 121 |
-
"""
|
| 122 |
-
translate from a file
|
| 123 |
-
@param source: source language
|
| 124 |
-
@param target: target language
|
| 125 |
-
@param path: path to file
|
| 126 |
-
@return: translated text
|
| 127 |
-
"""
|
| 128 |
-
try:
|
| 129 |
-
with open(path) as f:
|
| 130 |
-
text = f.read()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
"""
|
| 138 |
translate a batch of texts
|
| 139 |
@param source: source language
|
|
@@ -141,5 +88,5 @@ class YandexTranslator(object):
|
|
| 141 |
@param batch: list of texts to translate
|
| 142 |
@return: list of translations
|
| 143 |
"""
|
| 144 |
-
return [self.translate(source, target, text) for text in batch]
|
| 145 |
|
|
|
|
| 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 QCRI(object):
|
| 10 |
"""
|
| 11 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
| 12 |
"""
|
|
|
|
| 16 |
@param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
|
| 17 |
"""
|
| 18 |
|
|
|
|
| 19 |
if not api_key:
|
| 20 |
+
raise ServerException(401)
|
| 21 |
+
self.__base_url = BASE_URLS.get("QCRI")
|
| 22 |
|
| 23 |
self.api_key = api_key
|
|
|
|
| 24 |
self.api_endpoints = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"get_languages": "getLanguagePairs",
|
| 26 |
"get_domains": "getDomains",
|
| 27 |
"translate": "translate",
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
+
self.params = {
|
| 31 |
+
"key": self.api_key
|
| 32 |
}
|
|
|
|
| 33 |
|
| 34 |
+
def _get(self, endpoint, params=None, return_text=True):
|
| 35 |
+
if not params:
|
| 36 |
+
params = self.params
|
| 37 |
+
try:
|
| 38 |
+
res = requests.get(self.__base_url.format(endpoint=self.api_endpoints[endpoint]), params=params)
|
| 39 |
+
return res.text if return_text else res
|
| 40 |
+
except Exception as e:
|
| 41 |
+
raise e
|
| 42 |
|
| 43 |
def get_supported_languages(self):
|
| 44 |
+
|
| 45 |
+
pairs = self._get("get_languages")
|
| 46 |
+
return pairs
|
| 47 |
|
| 48 |
@property
|
| 49 |
def languages(self):
|
| 50 |
return self.get_supported_languages()
|
| 51 |
|
| 52 |
+
def get_domains(self):
|
| 53 |
+
domains = self._get("get_domains")
|
| 54 |
+
return domains
|
| 55 |
|
| 56 |
+
@property
|
| 57 |
+
def domains(self):
|
| 58 |
+
return self.get_domains()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
def translate(self, source, target, domain, text):
|
|
|
|
| 61 |
params = {
|
|
|
|
|
|
|
| 62 |
"key": self.api_key,
|
| 63 |
+
"langpair": "{}-{}".format(source, target),
|
| 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:
|
| 71 |
+
raise ServerException(503)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
else:
|
| 74 |
+
if response.status_code != 200:
|
| 75 |
+
ServerException(response.status_code)
|
| 76 |
+
else:
|
| 77 |
+
res = response.json()
|
| 78 |
+
translation = res["translatedText"]
|
| 79 |
+
if not translation:
|
| 80 |
+
raise TranslationNotFound(text)
|
| 81 |
+
return translation
|
| 82 |
+
|
| 83 |
+
def translate_batch(self, source, target, domain, batch):
|
| 84 |
"""
|
| 85 |
translate a batch of texts
|
| 86 |
@param source: source language
|
|
|
|
| 88 |
@param batch: list of texts to translate
|
| 89 |
@return: list of translations
|
| 90 |
"""
|
| 91 |
+
return [self.translate(source, target, domain, text) for text in batch]
|
| 92 |
|
deep_translator/yandex.py
CHANGED
|
@@ -5,7 +5,7 @@ import requests
|
|
| 5 |
from requests import exceptions
|
| 6 |
from deep_translator.constants import BASE_URLS
|
| 7 |
from deep_translator.exceptions import (RequestError,
|
| 8 |
-
|
| 9 |
|
| 10 |
|
| 11 |
class YandexTranslator(object):
|
|
@@ -18,7 +18,7 @@ class YandexTranslator(object):
|
|
| 18 |
@param api_key: your yandex api key
|
| 19 |
"""
|
| 20 |
if not api_key:
|
| 21 |
-
raise
|
| 22 |
self.__base_url = BASE_URLS.get("YANDEX")
|
| 23 |
|
| 24 |
self.api_key = api_key
|
|
@@ -44,12 +44,12 @@ class YandexTranslator(object):
|
|
| 44 |
print("url: ", url)
|
| 45 |
response = requests.get(url, params={"key": self.api_key}, proxies=proxies)
|
| 46 |
except requests.exceptions.ConnectionError:
|
| 47 |
-
raise
|
| 48 |
else:
|
| 49 |
data = response.json()
|
| 50 |
|
| 51 |
if response.status_code != 200:
|
| 52 |
-
raise
|
| 53 |
return data.get("dirs")
|
| 54 |
|
| 55 |
def detect(self, text, proxies=None):
|
|
@@ -66,9 +66,9 @@ class YandexTranslator(object):
|
|
| 66 |
except RequestError:
|
| 67 |
raise
|
| 68 |
except ConnectionError:
|
| 69 |
-
raise
|
| 70 |
except ValueError:
|
| 71 |
-
raise
|
| 72 |
else:
|
| 73 |
response = response.json()
|
| 74 |
language = response['lang']
|
|
@@ -76,7 +76,7 @@ class YandexTranslator(object):
|
|
| 76 |
if status_code != 200:
|
| 77 |
raise RequestError()
|
| 78 |
elif not language:
|
| 79 |
-
raise
|
| 80 |
return language
|
| 81 |
|
| 82 |
def translate(self, source, target, text, proxies=None):
|
|
@@ -90,7 +90,7 @@ class YandexTranslator(object):
|
|
| 90 |
url = self.__base_url.format(version=self.api_version, endpoint="translate")
|
| 91 |
response = requests.post(url, data=params, proxies=proxies)
|
| 92 |
except ConnectionError:
|
| 93 |
-
raise
|
| 94 |
else:
|
| 95 |
response = response.json()
|
| 96 |
|
|
@@ -98,7 +98,7 @@ class YandexTranslator(object):
|
|
| 98 |
raise TooManyRequests()
|
| 99 |
|
| 100 |
if response['code'] != 200:
|
| 101 |
-
raise
|
| 102 |
|
| 103 |
if not response['text']:
|
| 104 |
raise TranslationNotFound()
|
|
|
|
| 5 |
from requests import exceptions
|
| 6 |
from deep_translator.constants import BASE_URLS
|
| 7 |
from deep_translator.exceptions import (RequestError,
|
| 8 |
+
ServerException, TranslationNotFound, TooManyRequests)
|
| 9 |
|
| 10 |
|
| 11 |
class YandexTranslator(object):
|
|
|
|
| 18 |
@param api_key: your yandex api key
|
| 19 |
"""
|
| 20 |
if not api_key:
|
| 21 |
+
raise ServerException(401)
|
| 22 |
self.__base_url = BASE_URLS.get("YANDEX")
|
| 23 |
|
| 24 |
self.api_key = api_key
|
|
|
|
| 44 |
print("url: ", url)
|
| 45 |
response = requests.get(url, params={"key": self.api_key}, proxies=proxies)
|
| 46 |
except requests.exceptions.ConnectionError:
|
| 47 |
+
raise ServerException(503)
|
| 48 |
else:
|
| 49 |
data = response.json()
|
| 50 |
|
| 51 |
if response.status_code != 200:
|
| 52 |
+
raise ServerException(response.status_code)
|
| 53 |
return data.get("dirs")
|
| 54 |
|
| 55 |
def detect(self, text, proxies=None):
|
|
|
|
| 66 |
except RequestError:
|
| 67 |
raise
|
| 68 |
except ConnectionError:
|
| 69 |
+
raise ServerException(503)
|
| 70 |
except ValueError:
|
| 71 |
+
raise ServerException(response.status_code)
|
| 72 |
else:
|
| 73 |
response = response.json()
|
| 74 |
language = response['lang']
|
|
|
|
| 76 |
if status_code != 200:
|
| 77 |
raise RequestError()
|
| 78 |
elif not language:
|
| 79 |
+
raise ServerException(501)
|
| 80 |
return language
|
| 81 |
|
| 82 |
def translate(self, source, target, text, proxies=None):
|
|
|
|
| 90 |
url = self.__base_url.format(version=self.api_version, endpoint="translate")
|
| 91 |
response = requests.post(url, data=params, proxies=proxies)
|
| 92 |
except ConnectionError:
|
| 93 |
+
raise ServerException(503)
|
| 94 |
else:
|
| 95 |
response = response.json()
|
| 96 |
|
|
|
|
| 98 |
raise TooManyRequests()
|
| 99 |
|
| 100 |
if response['code'] != 200:
|
| 101 |
+
raise ServerException(response['code'])
|
| 102 |
|
| 103 |
if not response['text']:
|
| 104 |
raise TranslationNotFound()
|
docs/usage.rst
CHANGED
|
@@ -138,6 +138,39 @@ Mymemory Translator
|
|
| 138 |
translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
|
| 139 |
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
Linguee Translator
|
| 142 |
-------------------
|
| 143 |
|
|
|
|
| 138 |
translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
|
| 139 |
|
| 140 |
|
| 141 |
+
QCRI Translator
|
| 142 |
+
--------------------
|
| 143 |
+
|
| 144 |
+
.. note::
|
| 145 |
+
|
| 146 |
+
In order to use the QCRI translator, you need to generate a free api key. Visit https://mt.qcri.org/api/
|
| 147 |
+
for more information
|
| 148 |
+
|
| 149 |
+
- Check languages
|
| 150 |
+
|
| 151 |
+
.. code-block:: python
|
| 152 |
+
|
| 153 |
+
# as a property
|
| 154 |
+
print("language pairs: ", QCRI("your_api_key").languages)
|
| 155 |
+
|
| 156 |
+
- Check domains
|
| 157 |
+
|
| 158 |
+
.. code-block:: python
|
| 159 |
+
|
| 160 |
+
# as a property
|
| 161 |
+
print("domains: ", QCRI("your_api_key").domains)
|
| 162 |
+
|
| 163 |
+
- Text translation
|
| 164 |
+
|
| 165 |
+
.. code-block:: python
|
| 166 |
+
|
| 167 |
+
text = 'Education is great'
|
| 168 |
+
|
| 169 |
+
translated = QCRI("your_api_key").translate(source='en', target='ar', domain="news", text=text)
|
| 170 |
+
# output -> التعليم هو عظيم
|
| 171 |
+
|
| 172 |
+
# see docs for batch translation and more.
|
| 173 |
+
|
| 174 |
Linguee Translator
|
| 175 |
-------------------
|
| 176 |
|