Nidhal Baccouri commited on
Commit
3dffcd4
·
unverified ·
2 Parent(s): ce3ad77 ac77ea3

Merge pull request #51 from ThyWoof/master

Browse files

Update DeepL class interface to be consistent with others

Files changed (1) hide show
  1. deep_translator/deepl.py +14 -9
deep_translator/deepl.py CHANGED
@@ -12,24 +12,32 @@ 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/
 
 
19
  """
20
  if not api_key:
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, source, target, text):
 
 
 
 
27
  # Create the request parameters.
28
  translate_endpoint = 'translate'
29
  params = {
30
  "auth_key": self.api_key,
31
- "target_lang": self._map_language_to_code(target),
32
- "source_lang": self._map_language_to_code(source),
33
  "text": text
34
  }
35
  # Do the request and check the connection.
@@ -49,15 +57,12 @@ class DeepL(object):
49
  # Process and return the response.
50
  return res['translations'][0]['text']
51
 
52
- def translate_batch(self, source, target, batch):
53
  """
54
- translate a batch of texts
55
- @param source: source language
56
- @param target: target language
57
  @param batch: list of texts to translate
58
  @return: list of translations
59
  """
60
- return [self.translate(source, target, text) for text in batch]
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/
19
+ @param source: source language
20
+ @param target: target language
21
  """
22
  if not api_key:
23
  raise ServerException(401)
24
  self.version = 'v2'
25
  self.api_key = api_key
26
+ self.source = self._map_language_to_code(source)
27
+ self.target = self._map_language_to_code(target)
28
  self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
29
 
30
+ def translate(self, text):
31
+ """
32
+ @param text: text to translate
33
+ @return: translated text
34
+ """
35
  # Create the request parameters.
36
  translate_endpoint = 'translate'
37
  params = {
38
  "auth_key": self.api_key,
39
+ "source_lang": self.source,
40
+ "target_lang": self.target,
41
  "text": text
42
  }
43
  # Do the request and check the connection.
 
57
  # Process and return the response.
58
  return res['translations'][0]['text']
59
 
60
+ def translate_batch(self, batch):
61
  """
 
 
 
62
  @param batch: list of texts to translate
63
  @return: list of translations
64
  """
65
+ return [self.translate(text) for text in batch]
66
 
67
  def _is_language_supported(self, lang):
68
  # The language is supported when is in the dicionary.