=
commited on
Commit
·
a06172d
1
Parent(s):
f6bfd56
added support for mymemory
Browse files- README.rst +5 -5
- deep_translator/__init__.py +7 -2
- deep_translator/constants.py +2 -1
- deep_translator/exceptions.py +2 -2
- deep_translator/google_trans.py +9 -9
- deep_translator/linguee.py +1 -1
- deep_translator/mymemory.py +86 -0
- deep_translator/parent.py +4 -2
- deep_translator/pons.py +3 -3
- deep_translator/tests/test_deep_translator.py +3 -3
- docs/usage.rst +5 -5
- examples/trans.py +2 -2
- setup.cfg +1 -1
- setup.py +1 -1
README.rst
CHANGED
@@ -51,20 +51,20 @@ Usage
|
|
51 |
|
52 |
.. code-block:: python
|
53 |
|
54 |
-
from deep_translator import
|
55 |
|
56 |
english_text = 'happy coding'
|
57 |
|
58 |
-
result_german =
|
59 |
|
60 |
# Alternatively, you can pass languages by their name:
|
61 |
-
translated =
|
62 |
|
63 |
# or maybe you want to translate a text file ?
|
64 |
-
translated =
|
65 |
|
66 |
# or maybe you have many sentences in different languages and want to automate the translation process
|
67 |
-
translated =
|
68 |
|
69 |
|
70 |
or maybe you would like to use the Pons translator: Pons.com
|
|
|
51 |
|
52 |
.. code-block:: python
|
53 |
|
54 |
+
from deep_translator import MyMemoryTranslator, PonsTranslator, LingueeTranslator
|
55 |
|
56 |
english_text = 'happy coding'
|
57 |
|
58 |
+
result_german = MyMemoryTranslator(source='auto', target='de').translate(payload=english_text)
|
59 |
|
60 |
# Alternatively, you can pass languages by their name:
|
61 |
+
translated = MyMemoryTranslator(source='english', target='german').translate(payload=english_text)
|
62 |
|
63 |
# or maybe you want to translate a text file ?
|
64 |
+
translated = MyMemoryTranslator(source='auto', target='german').translate_file('path/to/file')
|
65 |
|
66 |
# or maybe you have many sentences in different languages and want to automate the translation process
|
67 |
+
translated = MyMemoryTranslator(source='auto', target='de').translate_sentences(your_list_of_sentences)
|
68 |
|
69 |
|
70 |
or maybe you would like to use the Pons translator: Pons.com
|
deep_translator/__init__.py
CHANGED
@@ -3,9 +3,14 @@
|
|
3 |
from .google_trans import GoogleTranslator
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
|
|
|
|
6 |
|
7 |
__author__ = """Nidhal Baccouri"""
|
8 |
__email__ = '[email protected]'
|
9 |
-
__version__ = '1.0.
|
10 |
|
11 |
-
__all__ = [GoogleTranslator,
|
|
|
|
|
|
|
|
3 |
from .google_trans import GoogleTranslator
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
6 |
+
from .mymemory import MyMemoryTranslator
|
7 |
+
|
8 |
|
9 |
__author__ = """Nidhal Baccouri"""
|
10 |
__email__ = '[email protected]'
|
11 |
+
__version__ = '1.0.1'
|
12 |
|
13 |
+
__all__ = [GoogleTranslator,
|
14 |
+
PonsTranslator,
|
15 |
+
LingueeTranslator,
|
16 |
+
MyMemoryTranslator]
|
deep_translator/constants.py
CHANGED
@@ -4,7 +4,8 @@ BASE_URLS = {
|
|
4 |
"GOOGLE_TRANSLATE": "https://translate.google.com/m",
|
5 |
"PONS": "https://en.pons.com/translate/",
|
6 |
"YANDEX": "https://translate.yandex.com/",
|
7 |
-
"LINGUEE": "https://www.linguee.com/"
|
|
|
8 |
}
|
9 |
|
10 |
GOOGLE_CODES_TO_LANGUAGES = {
|
|
|
4 |
"GOOGLE_TRANSLATE": "https://translate.google.com/m",
|
5 |
"PONS": "https://en.pons.com/translate/",
|
6 |
"YANDEX": "https://translate.yandex.com/",
|
7 |
+
"LINGUEE": "https://www.linguee.com/",
|
8 |
+
"MYMEMORY": "http://api.mymemory.translated.net/get"
|
9 |
}
|
10 |
|
11 |
GOOGLE_CODES_TO_LANGUAGES = {
|
deep_translator/exceptions.py
CHANGED
@@ -17,7 +17,7 @@ class LanguageNotSupportedException(BaseError):
|
|
17 |
class NotValidPayload(BaseError):
|
18 |
def __init__(self,
|
19 |
val,
|
20 |
-
message='
|
21 |
super(NotValidPayload, self).__init__(val, message)
|
22 |
|
23 |
|
@@ -29,5 +29,5 @@ class ElementNotFoundInGetRequest(BaseError):
|
|
29 |
|
30 |
|
31 |
class NotValidLength(BaseError):
|
32 |
-
def __init__(self, val, message="Length of
|
33 |
super(NotValidLength, self).__init__(val, message)
|
|
|
17 |
class NotValidPayload(BaseError):
|
18 |
def __init__(self,
|
19 |
val,
|
20 |
+
message='text must be a valid text with maximum 5000 character, otherwise it cannot be translated'):
|
21 |
super(NotValidPayload, self).__init__(val, message)
|
22 |
|
23 |
|
|
|
29 |
|
30 |
|
31 |
class NotValidLength(BaseError):
|
32 |
+
def __init__(self, val, message="Length of text need to be between 0 and 5000"):
|
33 |
super(NotValidLength, self).__init__(val, message)
|
deep_translator/google_trans.py
CHANGED
@@ -27,7 +27,7 @@ class GoogleTranslator(BaseTranslator):
|
|
27 |
target=self._target,
|
28 |
element_tag='div',
|
29 |
element_query={"class": "t0"},
|
30 |
-
payload_key='q', # key of
|
31 |
hl=self._target,
|
32 |
sl=self._source)
|
33 |
|
@@ -52,18 +52,18 @@ class GoogleTranslator(BaseTranslator):
|
|
52 |
raise LanguageNotSupportedException(lang)
|
53 |
return True
|
54 |
|
55 |
-
def translate(self,
|
56 |
"""
|
57 |
main function that uses google translate to translate a text
|
58 |
-
@param
|
59 |
@return: str: translated text
|
60 |
"""
|
61 |
|
62 |
-
if self._validate_payload(
|
63 |
-
|
64 |
|
65 |
if self.payload_key:
|
66 |
-
self._url_params[self.payload_key] =
|
67 |
|
68 |
response = requests.get(self.__base_url, params=self._url_params)
|
69 |
soup = BeautifulSoup(response.text, 'html.parser')
|
@@ -78,7 +78,7 @@ class GoogleTranslator(BaseTranslator):
|
|
78 |
with open(path) as f:
|
79 |
text = f.read()
|
80 |
|
81 |
-
return self.translate(
|
82 |
except Exception as e:
|
83 |
raise e
|
84 |
|
@@ -97,7 +97,7 @@ class GoogleTranslator(BaseTranslator):
|
|
97 |
translated_sentences = []
|
98 |
try:
|
99 |
for sentence in sentences:
|
100 |
-
translated = self.translate(
|
101 |
translated_sentences.append(translated)
|
102 |
|
103 |
return translated_sentences
|
@@ -107,5 +107,5 @@ class GoogleTranslator(BaseTranslator):
|
|
107 |
|
108 |
|
109 |
if __name__ == '__main__':
|
110 |
-
res = GoogleTranslator(source="auto", target="de").translate(
|
111 |
print(res)
|
|
|
27 |
target=self._target,
|
28 |
element_tag='div',
|
29 |
element_query={"class": "t0"},
|
30 |
+
payload_key='q', # key of text in the url
|
31 |
hl=self._target,
|
32 |
sl=self._source)
|
33 |
|
|
|
52 |
raise LanguageNotSupportedException(lang)
|
53 |
return True
|
54 |
|
55 |
+
def translate(self, text, **kwargs):
|
56 |
"""
|
57 |
main function that uses google translate to translate a text
|
58 |
+
@param text: desired text to translate
|
59 |
@return: str: translated text
|
60 |
"""
|
61 |
|
62 |
+
if self._validate_payload(text):
|
63 |
+
text = text.strip()
|
64 |
|
65 |
if self.payload_key:
|
66 |
+
self._url_params[self.payload_key] = text
|
67 |
|
68 |
response = requests.get(self.__base_url, params=self._url_params)
|
69 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
78 |
with open(path) as f:
|
79 |
text = f.read()
|
80 |
|
81 |
+
return self.translate(text=text)
|
82 |
except Exception as e:
|
83 |
raise e
|
84 |
|
|
|
97 |
translated_sentences = []
|
98 |
try:
|
99 |
for sentence in sentences:
|
100 |
+
translated = self.translate(text=sentence)
|
101 |
translated_sentences.append(translated)
|
102 |
|
103 |
return translated_sentences
|
|
|
107 |
|
108 |
|
109 |
if __name__ == '__main__':
|
110 |
+
res = GoogleTranslator(source="auto", target="de").translate(text='this is a good day')
|
111 |
print(res)
|
deep_translator/linguee.py
CHANGED
@@ -24,7 +24,7 @@ class LingueeTranslator(BaseTranslator):
|
|
24 |
target=self._target,
|
25 |
element_tag='a',
|
26 |
element_query={'class': 'dictLink featured'},
|
27 |
-
payload_key=None, # key of
|
28 |
)
|
29 |
|
30 |
def _map_language_to_code(self, *languages, **kwargs):
|
|
|
24 |
target=self._target,
|
25 |
element_tag='a',
|
26 |
element_query={'class': 'dictLink featured'},
|
27 |
+
payload_key=None, # key of text in the url
|
28 |
)
|
29 |
|
30 |
def _map_language_to_code(self, *languages, **kwargs):
|
deep_translator/mymemory.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
3 |
+
from deep_translator.exceptions import NotValidPayload
|
4 |
+
from deep_translator.parent import BaseTranslator
|
5 |
+
import requests
|
6 |
+
|
7 |
+
|
8 |
+
class MyMemoryTranslator(BaseTranslator):
|
9 |
+
"""
|
10 |
+
class that uses google translate to translate texts
|
11 |
+
"""
|
12 |
+
supported_languages = list(GOOGLE_LANGUAGES_TO_CODES.keys())
|
13 |
+
|
14 |
+
def __init__(self, source, target, **kwargs):
|
15 |
+
"""
|
16 |
+
@param source: source language to translate from
|
17 |
+
@param target: target language to translate to
|
18 |
+
"""
|
19 |
+
self.__base_url = BASE_URLS.get("MYMEMORY")
|
20 |
+
self._source = source
|
21 |
+
self._target = target
|
22 |
+
|
23 |
+
self.email = kwargs.get('email', None)
|
24 |
+
super(MyMemoryTranslator, self).__init__(base_url=self.__base_url,
|
25 |
+
source=self._source,
|
26 |
+
target=self._target,
|
27 |
+
payload_key='q',
|
28 |
+
langpair='{}|{}'.format(self._source, self._target))
|
29 |
+
|
30 |
+
def translate(self, text, **kwargs):
|
31 |
+
"""
|
32 |
+
main function that uses google translate to translate a text
|
33 |
+
@param text: desired text to translate
|
34 |
+
@return: str: translated text
|
35 |
+
"""
|
36 |
+
|
37 |
+
if self._validate_payload(text):
|
38 |
+
text = text.strip()
|
39 |
+
|
40 |
+
if self.payload_key:
|
41 |
+
self._url_params[self.payload_key] = text
|
42 |
+
if self.email:
|
43 |
+
self._url_params['de'] = self.email
|
44 |
+
|
45 |
+
response = requests.get(self.__base_url, params=self._url_params, headers=self.headers)
|
46 |
+
data = response.json()
|
47 |
+
if not data:
|
48 |
+
raise Exception("Translation was not found in response!")
|
49 |
+
|
50 |
+
translation = data.get('responseData').get('translatedText')
|
51 |
+
if translation:
|
52 |
+
return translation
|
53 |
+
|
54 |
+
elif not translation:
|
55 |
+
all_matches = data.get('matches')
|
56 |
+
matches = (match['translation'] for match in all_matches)
|
57 |
+
next_match = next(matches)
|
58 |
+
return next_match if not kwargs.get('return_all') else list(all_matches)
|
59 |
+
|
60 |
+
def translate_sentences(self, sentences=None, **kwargs):
|
61 |
+
"""
|
62 |
+
translate many sentences together. This makes sense if you have sentences with different languages
|
63 |
+
and you want to translate all to unified language. This is handy because it detects
|
64 |
+
automatically the language of each sentence and then translate it.
|
65 |
+
|
66 |
+
@param sentences: list of sentences to translate
|
67 |
+
@return: list of all translated sentences
|
68 |
+
"""
|
69 |
+
if not sentences:
|
70 |
+
raise NotValidPayload(sentences)
|
71 |
+
|
72 |
+
translated_sentences = []
|
73 |
+
try:
|
74 |
+
for sentence in sentences:
|
75 |
+
translated = self.translate(text=sentence, **kwargs)
|
76 |
+
translated_sentences.append(translated)
|
77 |
+
|
78 |
+
return translated_sentences
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
raise e
|
82 |
+
|
83 |
+
|
84 |
+
if __name__ == '__main__':
|
85 |
+
res = MyMemoryTranslator(source="en", target="zh").translate(text='cute')
|
86 |
+
print(res)
|
deep_translator/parent.py
CHANGED
@@ -27,12 +27,14 @@ class BaseTranslator(ABC):
|
|
27 |
self._element_tag = element_tag
|
28 |
self._element_query = element_query
|
29 |
self.payload_key = payload_key
|
|
|
|
|
30 |
super(BaseTranslator, self).__init__()
|
31 |
|
32 |
@staticmethod
|
33 |
def _validate_payload(payload, min_chars=1, max_chars=5000):
|
34 |
"""
|
35 |
-
validate the
|
36 |
@param payload: text to translate
|
37 |
@return: bool
|
38 |
"""
|
@@ -48,6 +50,6 @@ class BaseTranslator(ABC):
|
|
48 |
return True if min_chars < len(payload) < max_chars else False
|
49 |
|
50 |
@abstractmethod
|
51 |
-
def translate(self,
|
52 |
return NotImplemented('You need to implement the translate method!')
|
53 |
|
|
|
27 |
self._element_tag = element_tag
|
28 |
self._element_query = element_query
|
29 |
self.payload_key = payload_key
|
30 |
+
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19'
|
31 |
+
'(KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19'}
|
32 |
super(BaseTranslator, self).__init__()
|
33 |
|
34 |
@staticmethod
|
35 |
def _validate_payload(payload, min_chars=1, max_chars=5000):
|
36 |
"""
|
37 |
+
validate the text text to translate
|
38 |
@param payload: text to translate
|
39 |
@return: bool
|
40 |
"""
|
|
|
50 |
return True if min_chars < len(payload) < max_chars else False
|
51 |
|
52 |
@abstractmethod
|
53 |
+
def translate(self, text, **kwargs):
|
54 |
return NotImplemented('You need to implement the translate method!')
|
55 |
|
deep_translator/pons.py
CHANGED
@@ -88,12 +88,12 @@ class PonsTranslator(BaseTranslator):
|
|
88 |
|
89 |
if __name__ == '__main__':
|
90 |
|
91 |
-
# res =
|
92 |
-
# res =
|
93 |
# "this is good",
|
94 |
# "das Wetter ist schön",
|
95 |
# "un verme verde in un bicchiere verde"
|
96 |
# ])
|
97 |
-
# res = PonsTranslator(source="en", target="ar").translate(
|
98 |
res = PonsTranslator(source="en", target="ar").translate_words(words=('good', 'cute', 'angry'))
|
99 |
print(res)
|
|
|
88 |
|
89 |
if __name__ == '__main__':
|
90 |
|
91 |
+
# res = MyMemoryTranslator(source='auto', target='french').translate_text(path='../examples/test.txt')
|
92 |
+
# res = MyMemoryTranslator(source='auto', target='french').translate_sentences([
|
93 |
# "this is good",
|
94 |
# "das Wetter ist schön",
|
95 |
# "un verme verde in un bicchiere verde"
|
96 |
# ])
|
97 |
+
# res = PonsTranslator(source="en", target="ar").translate(text='good')
|
98 |
res = PonsTranslator(source="en", target="ar").translate_words(words=('good', 'cute', 'angry'))
|
99 |
print(res)
|
deep_translator/tests/test_deep_translator.py
CHANGED
@@ -19,7 +19,7 @@ def test_content(google_translator):
|
|
19 |
"""Sample pytest test function with the pytest fixture as an argument."""
|
20 |
# from bs4 import BeautifulSoup
|
21 |
# assert 'GitHub' in BeautifulSoup(response.content).title.string
|
22 |
-
assert google_translator.translate(
|
23 |
|
24 |
|
25 |
def test_inputs():
|
@@ -32,8 +32,8 @@ def test_inputs():
|
|
32 |
|
33 |
def test_payload(google_translator):
|
34 |
with pytest.raises(exceptions.NotValidPayload):
|
35 |
-
google_translator.translate(
|
36 |
with pytest.raises(exceptions.NotValidPayload):
|
37 |
-
google_translator.translate(
|
38 |
|
39 |
|
|
|
19 |
"""Sample pytest test function with the pytest fixture as an argument."""
|
20 |
# from bs4 import BeautifulSoup
|
21 |
# assert 'GitHub' in BeautifulSoup(response.content).title.string
|
22 |
+
assert google_translator.translate(text='좋은') == "good"
|
23 |
|
24 |
|
25 |
def test_inputs():
|
|
|
32 |
|
33 |
def test_payload(google_translator):
|
34 |
with pytest.raises(exceptions.NotValidPayload):
|
35 |
+
google_translator.translate(text="")
|
36 |
with pytest.raises(exceptions.NotValidPayload):
|
37 |
+
google_translator.translate(text=123)
|
38 |
|
39 |
|
docs/usage.rst
CHANGED
@@ -4,20 +4,20 @@ Usage
|
|
4 |
|
5 |
.. code-block:: python
|
6 |
|
7 |
-
from deep_translator import
|
8 |
|
9 |
english_text = 'happy coding'
|
10 |
|
11 |
-
result_german =
|
12 |
|
13 |
# Alternatively, you can pass languages by their name:
|
14 |
-
translated =
|
15 |
|
16 |
# or maybe you want to translate a text file ?
|
17 |
-
translated =
|
18 |
|
19 |
# or maybe you have many sentences in different languages and want to automate the translation process
|
20 |
-
translated =
|
21 |
|
22 |
|
23 |
or maybe you would like to use the Pons translator: Pons.com
|
|
|
4 |
|
5 |
.. code-block:: python
|
6 |
|
7 |
+
from deep_translator import MyMemoryTranslator, PonsTranslator, LingueeTranslator
|
8 |
|
9 |
english_text = 'happy coding'
|
10 |
|
11 |
+
result_german = MyMemoryTranslator(source='auto', target='de').translate(payload=english_text)
|
12 |
|
13 |
# Alternatively, you can pass languages by their name:
|
14 |
+
translated = MyMemoryTranslator(source='english', target='german').translate(payload=english_text)
|
15 |
|
16 |
# or maybe you want to translate a text file ?
|
17 |
+
translated = MyMemoryTranslator(source='auto', target='german').translate_file('path/to/file')
|
18 |
|
19 |
# or maybe you have many sentences in different languages and want to automate the translation process
|
20 |
+
translated = MyMemoryTranslator(source='auto', target='de').translate_sentences(your_list_of_sentences)
|
21 |
|
22 |
|
23 |
or maybe you would like to use the Pons translator: Pons.com
|
examples/trans.py
CHANGED
@@ -6,8 +6,8 @@ from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator
|
|
6 |
english_text = 'happy coding'
|
7 |
chinese_text = '這很好'
|
8 |
|
9 |
-
result_german = GoogleTranslator(source='english', target='german').translate(
|
10 |
-
result_french = GoogleTranslator(source='auto', target='french').translate(
|
11 |
|
12 |
print(f"original english text: {english_text} | translated text: {result_german}") # result: fröhliche Codierung
|
13 |
print(f"original chinese text: {chinese_text} | translated text: {result_french}") # result: C' est bon
|
|
|
6 |
english_text = 'happy coding'
|
7 |
chinese_text = '這很好'
|
8 |
|
9 |
+
result_german = GoogleTranslator(source='english', target='german').translate(text=english_text)
|
10 |
+
result_french = GoogleTranslator(source='auto', target='french').translate(text=chinese_text)
|
11 |
|
12 |
print(f"original english text: {english_text} | translated text: {result_german}") # result: fröhliche Codierung
|
13 |
print(f"original chinese text: {chinese_text} | translated text: {result_french}") # result: C' est bon
|
setup.cfg
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
[bumpversion]
|
2 |
-
current_version = 1.0.
|
3 |
commit = True
|
4 |
tag = True
|
5 |
|
|
|
1 |
[bumpversion]
|
2 |
+
current_version = 1.0.1
|
3 |
commit = True
|
4 |
tag = True
|
5 |
|
setup.py
CHANGED
@@ -51,6 +51,6 @@ setup(
|
|
51 |
test_suite='tests',
|
52 |
tests_require=test_requirements,
|
53 |
url='https://github.com/nidhaloff/deep_translator',
|
54 |
-
version='1.0.
|
55 |
zip_safe=False,
|
56 |
)
|
|
|
51 |
test_suite='tests',
|
52 |
tests_require=test_requirements,
|
53 |
url='https://github.com/nidhaloff/deep_translator',
|
54 |
+
version='1.0.1',
|
55 |
zip_safe=False,
|
56 |
)
|