nidhal baccouri commited on
Commit
55109ff
·
1 Parent(s): ec51887

added opt params

Browse files
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, source="en", target="en", use_free_api=True):
16
  """
17
  @param api_key: your DeepL api key.
18
  Get one here: https://www.deepl.com/docs-api/accessing-the-api/
@@ -30,7 +30,7 @@ class DeepL(object):
30
  else:
31
  self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
32
 
33
- def translate(self, text):
34
  """
35
  @param text: text to translate
36
  @return: translated text
@@ -60,18 +60,18 @@ class DeepL(object):
60
  # Process and return the response.
61
  return res['translations'][0]['text']
62
 
63
- def translate_batch(self, batch):
64
  """
65
  @param batch: list of texts to translate
66
  @return: list of translations
67
  """
68
- return [self.translate(text) for text in batch]
69
 
70
- def _is_language_supported(self, lang):
71
  # The language is supported when is in the dicionary.
72
  return lang == 'auto' or lang in self._languages.keys() or lang in self._languages.values()
73
 
74
- def _map_language_to_code(self, lang):
75
  if lang in self._languages.keys():
76
  return self._languages[lang]
77
  elif lang in self._languages.values():
 
12
  """
13
  _languages = DEEPL_LANGUAGE_TO_CODE
14
 
15
+ def __init__(self, api_key=None, source="en", target="en", use_free_api=True, **kwargs):
16
  """
17
  @param api_key: your DeepL api key.
18
  Get one here: https://www.deepl.com/docs-api/accessing-the-api/
 
30
  else:
31
  self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
32
 
33
+ def translate(self, text, **kwargs):
34
  """
35
  @param text: text to translate
36
  @return: translated text
 
60
  # Process and return the response.
61
  return res['translations'][0]['text']
62
 
63
+ def translate_batch(self, batch, **kwargs):
64
  """
65
  @param batch: list of texts to translate
66
  @return: list of translations
67
  """
68
+ return [self.translate(text, **kwargs) for text in batch]
69
 
70
+ def _is_language_supported(self, lang, **kwargs):
71
  # The language is supported when is in the dicionary.
72
  return lang == 'auto' or lang in self._languages.keys() or lang in self._languages.values()
73
 
74
+ def _map_language_to_code(self, lang, **kwargs):
75
  if lang in self._languages.keys():
76
  return self._languages[lang]
77
  elif lang in self._languages.values():
deep_translator/detection.py CHANGED
@@ -6,7 +6,7 @@ from deep_translator.configs import config
6
  from requests.exceptions import HTTPError
7
 
8
 
9
- def get_request_body(text, api_key, *args):
10
  """
11
  send a request and return the response body parsed as dictionary
12
 
@@ -58,7 +58,7 @@ def single_detection(text, api_key=None, detailed=False, *args, **kwargs):
58
  return lang
59
 
60
 
61
- def batch_detection(text_list, api_key, detailed=False, *args):
62
  """
63
  function responsible for detecting the language from a text
64
 
 
6
  from requests.exceptions import HTTPError
7
 
8
 
9
+ def get_request_body(text, api_key, *args, **kwargs):
10
  """
11
  send a request and return the response body parsed as dictionary
12
 
 
58
  return lang
59
 
60
 
61
+ def batch_detection(text_list, api_key, detailed=False, *args, **kwargs):
62
  """
63
  function responsible for detecting the language from a text
64
 
deep_translator/google_trans.py CHANGED
@@ -43,7 +43,7 @@ class GoogleTranslator(BaseTranslator):
43
  self._alt_element_query = {"class": "result-container"}
44
 
45
  @staticmethod
46
- def get_supported_languages(as_dict=False):
47
  """
48
  return the supported languages by the google translator
49
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -160,7 +160,7 @@ class GoogleTranslator(BaseTranslator):
160
  except Exception as e:
161
  raise e
162
 
163
- def translate_batch(self, batch=None):
164
  """
165
  translate a list of texts
166
  @param batch: list of texts you want to translate
@@ -174,7 +174,7 @@ class GoogleTranslator(BaseTranslator):
174
  arr = []
175
  for i, text in enumerate(batch):
176
 
177
- translated = self.translate(text)
178
  arr.append(translated)
179
  print("sentence number ", i+1, " has been translated successfully")
180
  sleep(2)
 
43
  self._alt_element_query = {"class": "result-container"}
44
 
45
  @staticmethod
46
+ def get_supported_languages(as_dict=False, **kwargs):
47
  """
48
  return the supported languages by the google translator
49
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
160
  except Exception as e:
161
  raise e
162
 
163
+ def translate_batch(self, batch=None, **kwargs):
164
  """
165
  translate a list of texts
166
  @param batch: list of texts you want to translate
 
174
  arr = []
175
  for i, text in enumerate(batch):
176
 
177
+ translated = self.translate(text, **kwargs)
178
  arr.append(translated)
179
  print("sentence number ", i+1, " has been translated successfully")
180
  sleep(2)
deep_translator/linguee.py CHANGED
@@ -22,7 +22,7 @@ class LingueeTranslator(BaseTranslator):
22
  _languages = LINGUEE_LANGUAGES_TO_CODES
23
  supported_languages = list(_languages.keys())
24
 
25
- def __init__(self, source, target="en", proxies=None):
26
  """
27
  @param source: source language to translate from
28
  @param target: target language to translate to
@@ -42,7 +42,7 @@ class LingueeTranslator(BaseTranslator):
42
  )
43
 
44
  @staticmethod
45
- def get_supported_languages(as_dict=False):
46
  """
47
  return the supported languages by the linguee translator
48
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -126,6 +126,6 @@ class LingueeTranslator(BaseTranslator):
126
 
127
  translated_words = []
128
  for word in words:
129
- translated_words.append(self.translate(word=word))
130
  return translated_words
131
 
 
22
  _languages = LINGUEE_LANGUAGES_TO_CODES
23
  supported_languages = list(_languages.keys())
24
 
25
+ def __init__(self, source, target="en", proxies=None, **kwargs):
26
  """
27
  @param source: source language to translate from
28
  @param target: target language to translate to
 
42
  )
43
 
44
  @staticmethod
45
+ def get_supported_languages(as_dict=False, **kwargs):
46
  """
47
  return the supported languages by the linguee translator
48
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
126
 
127
  translated_words = []
128
  for word in words:
129
+ translated_words.append(self.translate(word=word, **kwargs))
130
  return translated_words
131
 
deep_translator/microsoft.py CHANGED
@@ -58,7 +58,7 @@ class MicrosoftTranslator:
58
  self.__base_url = BASE_URLS.get("MICROSOFT_TRANSLATE")
59
 
60
  @staticmethod
61
- def get_supported_languages(as_dict=False):
62
  """
63
  return the languages supported by the microsoft translator
64
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -66,7 +66,7 @@ class MicrosoftTranslator:
66
  """
67
  return MicrosoftTranslator.supported_languages if not as_dict else MicrosoftTranslator._languages
68
 
69
- def _map_language_to_code(self, language):
70
  """
71
  map the language to its corresponding code (abbreviation) if the language was passed by its full name by the user
72
  @param language: a string (if 1 lang) or a list (if multiple langs)
@@ -82,7 +82,7 @@ class MicrosoftTranslator:
82
  else:
83
  raise LanguageNotSupportedException(lang)
84
 
85
- def is_language_supported(self, language):
86
  """
87
  check if the language is supported by the translator
88
  @param language: a string (if 1 lang) or a list (if multiple langs)
@@ -96,7 +96,7 @@ class MicrosoftTranslator:
96
  raise LanguageNotSupportedException(lang)
97
  return True
98
 
99
- def translate(self, text):
100
  """
101
  function that uses microsoft translate to translate a text
102
  @param text: desired text to translate
@@ -124,7 +124,7 @@ class MicrosoftTranslator:
124
  all_translations = [i['text'] for i in requested.json()[0]['translations']]
125
  return "\n".join(all_translations)
126
 
127
- def translate_file(self, path):
128
  """
129
  translate from a file
130
  @param path: path to file
@@ -137,10 +137,10 @@ class MicrosoftTranslator:
137
  except Exception as e:
138
  raise e
139
 
140
- def translate_batch(self, batch):
141
  """
142
  translate a batch of texts
143
  @param batch: list of texts to translate
144
  @return: list of translations
145
  """
146
- return [self.translate(text) for text in batch]
 
58
  self.__base_url = BASE_URLS.get("MICROSOFT_TRANSLATE")
59
 
60
  @staticmethod
61
+ def get_supported_languages(as_dict=False, **kwargs):
62
  """
63
  return the languages supported by the microsoft translator
64
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
66
  """
67
  return MicrosoftTranslator.supported_languages if not as_dict else MicrosoftTranslator._languages
68
 
69
+ def _map_language_to_code(self, language, **kwargs):
70
  """
71
  map the language to its corresponding code (abbreviation) if the language was passed by its full name by the user
72
  @param language: a string (if 1 lang) or a list (if multiple langs)
 
82
  else:
83
  raise LanguageNotSupportedException(lang)
84
 
85
+ def is_language_supported(self, language, **kwargs):
86
  """
87
  check if the language is supported by the translator
88
  @param language: a string (if 1 lang) or a list (if multiple langs)
 
96
  raise LanguageNotSupportedException(lang)
97
  return True
98
 
99
+ def translate(self, text, **kwargs):
100
  """
101
  function that uses microsoft translate to translate a text
102
  @param text: desired text to translate
 
124
  all_translations = [i['text'] for i in requested.json()[0]['translations']]
125
  return "\n".join(all_translations)
126
 
127
+ def translate_file(self, path, **kwargs):
128
  """
129
  translate from a file
130
  @param path: path to file
 
137
  except Exception as e:
138
  raise e
139
 
140
+ def translate_batch(self, batch, **kwargs):
141
  """
142
  translate a batch of texts
143
  @param batch: list of texts to translate
144
  @return: list of translations
145
  """
146
+ return [self.translate(text, **kwargs) for text in batch]
deep_translator/mymemory.py CHANGED
@@ -41,7 +41,7 @@ class MyMemoryTranslator(BaseTranslator):
41
  langpair='{}|{}'.format(self._source, self._target))
42
 
43
  @staticmethod
44
- def get_supported_languages(as_dict=False):
45
  """
46
  return the supported languages by the mymemory translator
47
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -158,7 +158,7 @@ class MyMemoryTranslator(BaseTranslator):
158
  except Exception as e:
159
  raise e
160
 
161
- def translate_batch(self, batch=None):
162
  """
163
  translate a list of texts
164
  @param batch: list of texts you want to translate
@@ -169,7 +169,7 @@ class MyMemoryTranslator(BaseTranslator):
169
 
170
  arr = []
171
  for text in batch:
172
- translated = self.translate(text)
173
  arr.append(translated)
174
  sleep(2)
175
 
 
41
  langpair='{}|{}'.format(self._source, self._target))
42
 
43
  @staticmethod
44
+ def get_supported_languages(as_dict=False, **kwargs):
45
  """
46
  return the supported languages by the mymemory translator
47
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
158
  except Exception as e:
159
  raise e
160
 
161
+ def translate_batch(self, batch=None, **kwargs):
162
  """
163
  translate a list of texts
164
  @param batch: list of texts you want to translate
 
169
 
170
  arr = []
171
  for text in batch:
172
+ translated = self.translate(text, **kwargs)
173
  arr.append(translated)
174
  sleep(2)
175
 
deep_translator/papago.py CHANGED
@@ -16,7 +16,7 @@ class PapagoTranslator(object):
16
  _languages = PAPAGO_LANGUAGE_TO_CODE
17
  supported_languages = list(_languages.keys())
18
 
19
- def __init__(self, client_id=None, secret_key=None, source="auto", target="en"):
20
  """
21
  @param source: source language to translate from
22
  @param target: target language to translate to
@@ -31,7 +31,7 @@ class PapagoTranslator(object):
31
  self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
32
 
33
  @staticmethod
34
- def get_supported_languages(as_dict=False):
35
  """
36
  return the supported languages by the google translator
37
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -136,7 +136,7 @@ class PapagoTranslator(object):
136
  except Exception as e:
137
  raise e
138
 
139
- def translate_batch(self, batch=None):
140
  """
141
  translate a list of texts
142
  @param batch: list of texts you want to translate
@@ -147,7 +147,7 @@ class PapagoTranslator(object):
147
  arr = []
148
  for i, text in enumerate(batch):
149
 
150
- translated = self.translate(text)
151
  arr.append(translated)
152
  return arr
153
 
 
16
  _languages = PAPAGO_LANGUAGE_TO_CODE
17
  supported_languages = list(_languages.keys())
18
 
19
+ def __init__(self, client_id=None, secret_key=None, source="auto", target="en", **kwargs):
20
  """
21
  @param source: source language to translate from
22
  @param target: target language to translate to
 
31
  self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
32
 
33
  @staticmethod
34
+ def get_supported_languages(as_dict=False, **kwargs):
35
  """
36
  return the supported languages by the google translator
37
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
136
  except Exception as e:
137
  raise e
138
 
139
+ def translate_batch(self, batch=None, **kwargs):
140
  """
141
  translate a list of texts
142
  @param batch: list of texts you want to translate
 
147
  arr = []
148
  for i, text in enumerate(batch):
149
 
150
+ translated = self.translate(text, **kwargs)
151
  arr.append(translated)
152
  return arr
153
 
deep_translator/pons.py CHANGED
@@ -21,7 +21,7 @@ class PonsTranslator(BaseTranslator):
21
  _languages = PONS_LANGUAGES_TO_CODES
22
  supported_languages = list(_languages.keys())
23
 
24
- def __init__(self, source, target="en", proxies=None):
25
  """
26
  @param source: source language to translate from
27
  @param target: target language to translate to
@@ -40,7 +40,7 @@ class PonsTranslator(BaseTranslator):
40
  )
41
 
42
  @staticmethod
43
- def get_supported_languages(as_dict=False):
44
  """
45
  return the supported languages by the linguee translator
46
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
@@ -131,6 +131,6 @@ class PonsTranslator(BaseTranslator):
131
 
132
  translated_words = []
133
  for word in words:
134
- translated_words.append(self.translate(word=word))
135
  return translated_words
136
 
 
21
  _languages = PONS_LANGUAGES_TO_CODES
22
  supported_languages = list(_languages.keys())
23
 
24
+ def __init__(self, source, target="en", proxies=None, **kwargs):
25
  """
26
  @param source: source language to translate from
27
  @param target: target language to translate to
 
40
  )
41
 
42
  @staticmethod
43
+ def get_supported_languages(as_dict=False, **kwargs):
44
  """
45
  return the supported languages by the linguee translator
46
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
 
131
 
132
  translated_words = []
133
  for word in words:
134
+ translated_words.append(self.translate(word=word, **kwargs))
135
  return translated_words
136
 
deep_translator/qcri.py CHANGED
@@ -10,7 +10,7 @@ class QCRI(object):
10
  class that wraps functions, which use the QRCI translator under the hood to translate word(s)
11
  """
12
 
13
- def __init__(self, api_key=None, source="en", target="en"):
14
  """
15
  @param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
16
  """
@@ -57,7 +57,7 @@ class QCRI(object):
57
  def domains(self):
58
  return self.get_domains()
59
 
60
- def translate(self, text, domain):
61
  params = {
62
  "key": self.api_key,
63
  "langpair": "{}-{}".format(self.source, self.target),
@@ -79,12 +79,12 @@ class QCRI(object):
79
  raise TranslationNotFound(text)
80
  return translation
81
 
82
- def translate_batch(self, batch, domain):
83
  """
84
  translate a batch of texts
85
  @domain: domain
86
  @param batch: list of texts to translate
87
  @return: list of translations
88
  """
89
- return [self.translate(domain, text) for text in batch]
90
 
 
10
  class that wraps functions, which use the QRCI translator under the hood to translate word(s)
11
  """
12
 
13
+ def __init__(self, api_key=None, source="en", target="en", **kwargs):
14
  """
15
  @param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
16
  """
 
57
  def domains(self):
58
  return self.get_domains()
59
 
60
+ def translate(self, text, domain, **kwargs):
61
  params = {
62
  "key": self.api_key,
63
  "langpair": "{}-{}".format(self.source, self.target),
 
79
  raise TranslationNotFound(text)
80
  return translation
81
 
82
+ def translate_batch(self, batch, domain, **kwargs):
83
  """
84
  translate a batch of texts
85
  @domain: domain
86
  @param batch: list of texts to translate
87
  @return: list of translations
88
  """
89
+ return [self.translate(domain, text, **kwargs) for text in batch]
90
 
deep_translator/yandex.py CHANGED
@@ -12,7 +12,7 @@ class YandexTranslator(object):
12
  class that wraps functions, which use the yandex translator under the hood to translate word(s)
13
  """
14
 
15
- def __init__(self, api_key=None, source="en", target="de"):
16
  """
17
  @param api_key: your yandex api key
18
  """
@@ -80,7 +80,7 @@ class YandexTranslator(object):
80
  raise ServerException(501)
81
  return language
82
 
83
- def translate(self, text, proxies=None):
84
  params = {
85
  "text": text,
86
  "format": "plain",
@@ -106,7 +106,7 @@ class YandexTranslator(object):
106
 
107
  return response['text']
108
 
109
- def translate_file(self, path):
110
  """
111
  translate from a file
112
  @param path: path to file
@@ -120,10 +120,10 @@ class YandexTranslator(object):
120
  except Exception as e:
121
  raise e
122
 
123
- def translate_batch(self, batch):
124
  """
125
  translate a batch of texts
126
  @param batch: list of texts to translate
127
  @return: list of translations
128
  """
129
- return [self.translate(text) for text in batch]
 
12
  class that wraps functions, which use the yandex translator under the hood to translate word(s)
13
  """
14
 
15
+ def __init__(self, api_key=None, source="en", target="de", **kwargs):
16
  """
17
  @param api_key: your yandex api key
18
  """
 
80
  raise ServerException(501)
81
  return language
82
 
83
+ def translate(self, text, proxies=None, **kwargs):
84
  params = {
85
  "text": text,
86
  "format": "plain",
 
106
 
107
  return response['text']
108
 
109
+ def translate_file(self, path, **kwargs):
110
  """
111
  translate from a file
112
  @param path: path to file
 
120
  except Exception as e:
121
  raise e
122
 
123
+ def translate_batch(self, batch, **kwargs):
124
  """
125
  translate a batch of texts
126
  @param batch: list of texts to translate
127
  @return: list of translations
128
  """
129
+ return [self.translate(text, **kwargs) for text in batch]