ThyWoof
commited on
Commit
·
a175f0e
1
Parent(s):
ce3ad77
make interface closer to other engines
Browse files- deep_translator/deepl.py +8 -6
deep_translator/deepl.py
CHANGED
@@ -12,7 +12,7 @@ class DeepL(object):
|
|
12 |
"""
|
13 |
_languages = DEEPL_LANGUAGE_TO_CODE
|
14 |
|
15 |
-
def __init__(self, api_key=None):
|
16 |
"""
|
17 |
@param api_key: your DeepL api key.
|
18 |
Get one here: https://www.deepl.com/docs-api/accessing-the-api/
|
@@ -21,15 +21,17 @@ class DeepL(object):
|
|
21 |
raise ServerException(401)
|
22 |
self.version = 'v2'
|
23 |
self.api_key = api_key
|
|
|
|
|
24 |
self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
|
25 |
|
26 |
-
def translate(self,
|
27 |
# Create the request parameters.
|
28 |
translate_endpoint = 'translate'
|
29 |
params = {
|
30 |
"auth_key": self.api_key,
|
31 |
-
"
|
32 |
-
"
|
33 |
"text": text
|
34 |
}
|
35 |
# Do the request and check the connection.
|
@@ -49,7 +51,7 @@ class DeepL(object):
|
|
49 |
# Process and return the response.
|
50 |
return res['translations'][0]['text']
|
51 |
|
52 |
-
def translate_batch(self,
|
53 |
"""
|
54 |
translate a batch of texts
|
55 |
@param source: source language
|
@@ -57,7 +59,7 @@ class DeepL(object):
|
|
57 |
@param batch: list of texts to translate
|
58 |
@return: list of translations
|
59 |
"""
|
60 |
-
return [self.translate(
|
61 |
|
62 |
def _is_language_supported(self, lang):
|
63 |
# The language is supported when is in the dicionary.
|
|
|
12 |
"""
|
13 |
_languages = DEEPL_LANGUAGE_TO_CODE
|
14 |
|
15 |
+
def __init__(self, api_key=None, source="auto", target="en"):
|
16 |
"""
|
17 |
@param api_key: your DeepL api key.
|
18 |
Get one here: https://www.deepl.com/docs-api/accessing-the-api/
|
|
|
21 |
raise ServerException(401)
|
22 |
self.version = 'v2'
|
23 |
self.api_key = api_key
|
24 |
+
self.source = self._map_language_to_code(source)
|
25 |
+
self.target = self._map_language_to_code(target)
|
26 |
self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
|
27 |
|
28 |
+
def translate(self, text):
|
29 |
# Create the request parameters.
|
30 |
translate_endpoint = 'translate'
|
31 |
params = {
|
32 |
"auth_key": self.api_key,
|
33 |
+
"source_lang": self.source,
|
34 |
+
"target_lang": self.target,
|
35 |
"text": text
|
36 |
}
|
37 |
# Do the request and check the connection.
|
|
|
51 |
# Process and return the response.
|
52 |
return res['translations'][0]['text']
|
53 |
|
54 |
+
def translate_batch(self, batch):
|
55 |
"""
|
56 |
translate a batch of texts
|
57 |
@param source: source language
|
|
|
59 |
@param batch: list of texts to translate
|
60 |
@return: list of translations
|
61 |
"""
|
62 |
+
return [self.translate(text) for text in batch]
|
63 |
|
64 |
def _is_language_supported(self, lang):
|
65 |
# The language is supported when is in the dicionary.
|