= commited on
Commit
1600811
·
1 Parent(s): 1d8f0c1

updated readme

Browse files
README.rst CHANGED
@@ -148,7 +148,7 @@ Imports
148
  PonsTranslator,
149
  LingueeTranslator,
150
  MyMemoryTranslator,
151
- YandexTranslator
152
  single_detection,
153
  batch_detection)
154
 
@@ -207,31 +207,29 @@ Google Translate
207
 
208
  translated = GoogleTranslator(source='auto', target='de').translate(text=text)
209
 
210
- - You can pass languages by name:
211
 
212
  .. code-block:: python
213
 
214
  translated = GoogleTranslator(source='auto', target='german').translate(text=text)
215
 
216
- - Alternatively, you can pass languages by their abbreviation:
217
-
218
- .. code-block:: python
219
-
220
  translated = GoogleTranslator(source='en', target='de').translate(text=text)
221
 
222
- - Translate from a file:
223
 
224
  .. code-block:: python
225
 
226
- translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
227
 
228
- - Automate translation by detecting the source language and translate it automatically to the desired language
 
229
 
230
- .. code-block:: python
231
 
232
- # this is useful if you have many sentences in different languages and want to automate the translation process
233
- translated = GoogleTranslator(source='auto', target='de').translate_sentences([your_list_of_sentences])
234
 
 
235
 
236
  Mymemory Translator
237
  --------------------
@@ -250,12 +248,14 @@ Mymemory Translator
250
 
251
  translated = MyMemoryTranslator(source='auto', target='french').translate(text)
252
 
253
- - Translate a list of sentences or paragraphs
254
 
255
  .. code-block:: python
256
 
257
  texts = ["hallo welt", "guten morgen"]
258
- translated = MyMemoryTranslator('de', 'english').translate_sentences(texts)
 
 
259
 
260
  - Translate from file
261
 
@@ -266,11 +266,9 @@ Mymemory Translator
266
  translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
267
 
268
 
269
-
270
  Linguee Translator
271
  -------------------
272
 
273
-
274
  .. code-block:: python
275
 
276
  word = 'good'
@@ -351,13 +349,26 @@ Yandex Translator
351
  .. code-block:: python
352
 
353
  # with auto detection | meaning provide only the target language and let yandex detect the source
354
- translated = YandexTranslator('your_api_key').translate('Hallo, Welt', 'en')
355
  print(f"translated text: {translated}") # output -> translated text: Hello world
356
 
357
  # provide source and target language explicitly
358
- translated = YandexTranslator('your_api_key').translate('Hallo, Welt', 'de-en')
359
  print(f"translated text: {translated}") # output -> translated text: Hello world
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  Usage from Terminal
362
  --------------------
363
 
 
148
  PonsTranslator,
149
  LingueeTranslator,
150
  MyMemoryTranslator,
151
+ YandexTranslator,
152
  single_detection,
153
  batch_detection)
154
 
 
207
 
208
  translated = GoogleTranslator(source='auto', target='de').translate(text=text)
209
 
210
+ - You can pass languages by name or by abbreviation:
211
 
212
  .. code-block:: python
213
 
214
  translated = GoogleTranslator(source='auto', target='german').translate(text=text)
215
 
216
+ # Alternatively, you can pass languages by their abbreviation:
 
 
 
217
  translated = GoogleTranslator(source='en', target='de').translate(text=text)
218
 
219
+ - Translate batch of texts
220
 
221
  .. code-block:: python
222
 
223
+ texts = ["hallo welt", "guten morgen"]
224
 
225
+ # the translate_sentences function is deprecated, use the translate_batch function instead
226
+ translated = MyMemoryTranslator('de', 'en').translate_batch(texts)
227
 
228
+ - Translate from a file:
229
 
230
+ .. code-block:: python
 
231
 
232
+ translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
233
 
234
  Mymemory Translator
235
  --------------------
 
248
 
249
  translated = MyMemoryTranslator(source='auto', target='french').translate(text)
250
 
251
+ - Translate batch of texts
252
 
253
  .. code-block:: python
254
 
255
  texts = ["hallo welt", "guten morgen"]
256
+
257
+ # the translate_sentences function is deprecated, use the translate_batch function instead
258
+ translated = MyMemoryTranslator('de', 'en').translate_batch(texts)
259
 
260
  - Translate from file
261
 
 
266
  translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
267
 
268
 
 
269
  Linguee Translator
270
  -------------------
271
 
 
272
  .. code-block:: python
273
 
274
  word = 'good'
 
349
  .. code-block:: python
350
 
351
  # with auto detection | meaning provide only the target language and let yandex detect the source
352
+ translated = YandexTranslator('your_api_key').translate(source="auto", target="en", text='Hallo, Welt')
353
  print(f"translated text: {translated}") # output -> translated text: Hello world
354
 
355
  # provide source and target language explicitly
356
+ translated = YandexTranslator('your_api_key').translate(source="de", target="en", text='Hallo, Welt')
357
  print(f"translated text: {translated}") # output -> translated text: Hello world
358
 
359
+ - File translation
360
+
361
+ .. code-block:: python
362
+
363
+ translated = YandexTranslator('your_api_key').translate_file(source="auto", target="en", path="path_to_your_file")
364
+
365
+ - Batch translation
366
+
367
+ .. code-block:: python
368
+
369
+ translated = YandexTranslator('your_api_key').translate_batch(source="auto", target="de", batch=["hello world", "happy coding"])
370
+
371
+
372
  Usage from Terminal
373
  --------------------
374
 
deep_translator/google_trans.py CHANGED
@@ -8,6 +8,8 @@ from deep_translator.parent import BaseTranslator
8
  from bs4 import BeautifulSoup
9
  import requests
10
  from time import sleep
 
 
11
 
12
 
13
  class GoogleTranslator(BaseTranslator):
@@ -127,6 +129,8 @@ class GoogleTranslator(BaseTranslator):
127
  @param sentences: list of sentences to translate
128
  @return: list of all translated sentences
129
  """
 
 
130
  if not sentences:
131
  raise NotValidPayload(sentences)
132
 
@@ -158,7 +162,3 @@ class GoogleTranslator(BaseTranslator):
158
 
159
  return arr
160
 
161
-
162
- if __name__ == '__main__':
163
- res = GoogleTranslator(target="fr").translate("good")
164
- print(res)
 
8
  from bs4 import BeautifulSoup
9
  import requests
10
  from time import sleep
11
+ import warnings
12
+ import logging
13
 
14
 
15
  class GoogleTranslator(BaseTranslator):
 
129
  @param sentences: list of sentences to translate
130
  @return: list of all translated sentences
131
  """
132
+ warnings.warn("deprecated. Use the translate_batch function instead", DeprecationWarning, stacklevel=2)
133
+ logging.warning("deprecated. Use the translate_batch function instead")
134
  if not sentences:
135
  raise NotValidPayload(sentences)
136
 
 
162
 
163
  return arr
164
 
 
 
 
 
deep_translator/mymemory.py CHANGED
@@ -1,6 +1,9 @@
1
  """
2
  mymemory translator API
3
  """
 
 
 
4
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
5
  from deep_translator.exceptions import (NotValidPayload,
6
  TranslationNotFound,
@@ -120,6 +123,9 @@ class MyMemoryTranslator(BaseTranslator):
120
  @param sentences: list of sentences to translate
121
  @return: list of all translated sentences
122
  """
 
 
 
123
  if not sentences:
124
  raise NotValidPayload(sentences)
125
 
 
1
  """
2
  mymemory translator API
3
  """
4
+ import logging
5
+ import warnings
6
+
7
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
8
  from deep_translator.exceptions import (NotValidPayload,
9
  TranslationNotFound,
 
123
  @param sentences: list of sentences to translate
124
  @return: list of all translated sentences
125
  """
126
+ warn_msg = "deprecated. Use the translate_batch function instead"
127
+ warnings.warn(warn_msg, DeprecationWarning, stacklevel=2)
128
+ logging.warning(warn_msg)
129
  if not sentences:
130
  raise NotValidPayload(sentences)
131
 
deep_translator/yandex.py CHANGED
@@ -52,57 +52,81 @@ class YandexTranslator(object):
52
  raise YandexDefaultException(response.status_code)
53
  return data.get("dirs")
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- def detect(self, text, proxies=None):
57
- response = None
58
- params = {
59
- "text": text,
60
- "format": "plain",
61
- "key": self.api_key,
62
- }
63
- try:
64
- url = self.__base_url.format(version=self.api_version, endpoint="detect")
65
- response = requests.post(url, data=params, proxies=proxies)
66
-
67
- except RequestError:
68
- raise
69
- except ConnectionError:
70
- raise YandexDefaultException(503)
71
- except ValueError:
72
- raise YandexDefaultException(response.status_code)
73
- else:
74
- response = response.json()
75
- language = response['lang']
76
- status_code = response['code']
77
- if status_code != 200:
78
- raise RequestError()
79
- elif not language:
80
- raise YandexDefaultException(501)
81
- return language
82
-
83
-
84
- def translate(self, text, lang, proxies=None):
85
- params = {
86
- "text": text,
87
- "format": "plain",
88
- "lang": lang,
89
- "key": self.api_key
90
- }
91
- try:
92
- url = self.__base_url.format(version=self.api_version, endpoint="translate")
93
- response = requests.post(url, data=params, proxies=proxies)
94
- except ConnectionError:
95
- raise YandexDefaultException(503)
96
- else:
97
- response = response.json()
98
-
99
- if response['code'] == 429:
100
- raise TooManyRequests()
101
-
102
- if response['code'] != 200:
103
- raise YandexDefaultException(response['code'])
104
-
105
- if not response['text']:
106
- raise TranslationNotFound()
107
-
108
- return response['text']
 
52
  raise YandexDefaultException(response.status_code)
53
  return data.get("dirs")
54
 
55
+ def detect(self, text, proxies=None):
56
+ response = None
57
+ params = {
58
+ "text": text,
59
+ "format": "plain",
60
+ "key": self.api_key,
61
+ }
62
+ try:
63
+ url = self.__base_url.format(version=self.api_version, endpoint="detect")
64
+ response = requests.post(url, data=params, proxies=proxies)
65
+
66
+ except RequestError:
67
+ raise
68
+ except ConnectionError:
69
+ raise YandexDefaultException(503)
70
+ except ValueError:
71
+ raise YandexDefaultException(response.status_code)
72
+ else:
73
+ response = response.json()
74
+ language = response['lang']
75
+ status_code = response['code']
76
+ if status_code != 200:
77
+ raise RequestError()
78
+ elif not language:
79
+ raise YandexDefaultException(501)
80
+ return language
81
+
82
+ def translate(self, source, target, text, proxies=None):
83
+ params = {
84
+ "text": text,
85
+ "format": "plain",
86
+ "lang": target if source == "auto" else "{}-{}".format(source, target),
87
+ "key": self.api_key
88
+ }
89
+ try:
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 YandexDefaultException(503)
94
+ else:
95
+ response = response.json()
96
+
97
+ if response['code'] == 429:
98
+ raise TooManyRequests()
99
 
100
+ if response['code'] != 200:
101
+ raise YandexDefaultException(response['code'])
102
+
103
+ if not response['text']:
104
+ raise TranslationNotFound()
105
+
106
+ return response['text']
107
+
108
+ def translate_file(self, source, target, path):
109
+ """
110
+ translate from a file
111
+ @param source: source language
112
+ @param target: target language
113
+ @param path: path to file
114
+ @return: translated text
115
+ """
116
+ try:
117
+ with open(path) as f:
118
+ text = f.read()
119
+
120
+ return self.translate(source, target, text)
121
+ except Exception as e:
122
+ raise e
123
+
124
+ def translate_batch(self, source, target, batch):
125
+ """
126
+ translate a batch of texts
127
+ @param source: source language
128
+ @param target: target language
129
+ @param batch: list of texts to translate
130
+ @return: list of translations
131
+ """
132
+ return [self.translate(source, target, text) for text in batch]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
docs/usage.rst CHANGED
@@ -20,6 +20,7 @@ Imports
20
  PonsTranslator,
21
  LingueeTranslator,
22
  MyMemoryTranslator,
 
23
  single_detection,
24
  batch_detection)
25
 
@@ -78,31 +79,29 @@ Google Translate
78
 
79
  translated = GoogleTranslator(source='auto', target='de').translate(text=text)
80
 
81
- - You can pass languages by name:
82
 
83
  .. code-block:: python
84
 
85
  translated = GoogleTranslator(source='auto', target='german').translate(text=text)
86
 
87
- - Alternatively, you can pass languages by their abbreviation:
88
-
89
- .. code-block:: python
90
-
91
  translated = GoogleTranslator(source='en', target='de').translate(text=text)
92
 
93
- - Translate from a file:
94
 
95
  .. code-block:: python
96
 
97
- translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
98
 
99
- - Automate translation by detecting the source language and translate it automatically to the desired language
 
100
 
101
- .. code-block:: python
102
 
103
- # this is useful if you have many sentences in different languages and want to automate the translation process
104
- translated = GoogleTranslator(source='auto', target='de').translate_sentences([your_list_of_sentences])
105
 
 
106
 
107
  Mymemory Translator
108
  --------------------
@@ -121,12 +120,14 @@ Mymemory Translator
121
 
122
  translated = MyMemoryTranslator(source='auto', target='french').translate(text)
123
 
124
- - Translate a list of sentences or paragraphs
125
 
126
  .. code-block:: python
127
 
128
  texts = ["hallo welt", "guten morgen"]
129
- translated = MyMemoryTranslator('de', 'english').translate_sentences(texts)
 
 
130
 
131
  - Translate from file
132
 
@@ -137,11 +138,9 @@ Mymemory Translator
137
  translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
138
 
139
 
140
-
141
  Linguee Translator
142
  -------------------
143
 
144
-
145
  .. code-block:: python
146
 
147
  word = 'good'
@@ -222,13 +221,26 @@ Yandex Translator
222
  .. code-block:: python
223
 
224
  # with auto detection | meaning provide only the target language and let yandex detect the source
225
- translated = YandexTranslator('your_api_key').translate('Hallo, Welt', 'en')
226
  print(f"translated text: {translated}") # output -> translated text: Hello world
227
 
228
  # provide source and target language explicitly
229
- translated = YandexTranslator('your_api_key').translate('Hallo, Welt', 'de-en')
230
  print(f"translated text: {translated}") # output -> translated text: Hello world
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  Usage from Terminal
233
  --------------------
234
 
 
20
  PonsTranslator,
21
  LingueeTranslator,
22
  MyMemoryTranslator,
23
+ YandexTranslator,
24
  single_detection,
25
  batch_detection)
26
 
 
79
 
80
  translated = GoogleTranslator(source='auto', target='de').translate(text=text)
81
 
82
+ - You can pass languages by name or by abbreviation:
83
 
84
  .. code-block:: python
85
 
86
  translated = GoogleTranslator(source='auto', target='german').translate(text=text)
87
 
88
+ # Alternatively, you can pass languages by their abbreviation:
 
 
 
89
  translated = GoogleTranslator(source='en', target='de').translate(text=text)
90
 
91
+ - Translate batch of texts
92
 
93
  .. code-block:: python
94
 
95
+ texts = ["hallo welt", "guten morgen"]
96
 
97
+ # the translate_sentences function is deprecated, use the translate_batch function instead
98
+ translated = MyMemoryTranslator('de', 'en').translate_batch(texts)
99
 
100
+ - Translate from a file:
101
 
102
+ .. code-block:: python
 
103
 
104
+ translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
105
 
106
  Mymemory Translator
107
  --------------------
 
120
 
121
  translated = MyMemoryTranslator(source='auto', target='french').translate(text)
122
 
123
+ - Translate batch of texts
124
 
125
  .. code-block:: python
126
 
127
  texts = ["hallo welt", "guten morgen"]
128
+
129
+ # the translate_sentences function is deprecated, use the translate_batch function instead
130
+ translated = MyMemoryTranslator('de', 'en').translate_batch(texts)
131
 
132
  - Translate from file
133
 
 
138
  translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
139
 
140
 
 
141
  Linguee Translator
142
  -------------------
143
 
 
144
  .. code-block:: python
145
 
146
  word = 'good'
 
221
  .. code-block:: python
222
 
223
  # with auto detection | meaning provide only the target language and let yandex detect the source
224
+ translated = YandexTranslator('your_api_key').translate(source="auto", target="en", text='Hallo, Welt')
225
  print(f"translated text: {translated}") # output -> translated text: Hello world
226
 
227
  # provide source and target language explicitly
228
+ translated = YandexTranslator('your_api_key').translate(source="de", target="en", text='Hallo, Welt')
229
  print(f"translated text: {translated}") # output -> translated text: Hello world
230
 
231
+ - File translation
232
+
233
+ .. code-block:: python
234
+
235
+ translated = YandexTranslator('your_api_key').translate_file(source="auto", target="en", path="path_to_your_file")
236
+
237
+ - Batch translation
238
+
239
+ .. code-block:: python
240
+
241
+ translated = YandexTranslator('your_api_key').translate_batch(source="auto", target="de", batch=["hello world", "happy coding"])
242
+
243
+
244
  Usage from Terminal
245
  --------------------
246