=
commited on
Commit
·
9a69a65
1
Parent(s):
a69a321
other release with pons
Browse files- README.rst +14 -6
- deep_translator/__init__.py +1 -1
- deep_translator/google_trans.py +31 -8
- deep_translator/parent.py +9 -36
- deep_translator/pons.py +35 -20
- docs/usage.rst +6 -4
- setup.cfg +1 -1
- setup.py +1 -1
README.rst
CHANGED
@@ -38,14 +38,16 @@ in this tool starting with google translate
|
|
38 |
Features
|
39 |
--------
|
40 |
|
41 |
-
*
|
42 |
-
*
|
|
|
|
|
43 |
|
44 |
|
45 |
Usage
|
46 |
=====
|
47 |
|
48 |
-
|
49 |
|
50 |
from deep_translator import GoogleTranslator
|
51 |
|
@@ -57,10 +59,16 @@ To use deep_translator in a project::
|
|
57 |
|
58 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
59 |
|
60 |
-
|
61 |
# Alternatively, you can pass languages by their name:
|
62 |
-
|
63 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
|
|
38 |
Features
|
39 |
--------
|
40 |
|
41 |
+
* Support for google translate
|
42 |
+
* Support for Pons translator (pons.com)
|
43 |
+
* Translate directly from a text file
|
44 |
+
* Translate different paragraphs in different languages
|
45 |
|
46 |
|
47 |
Usage
|
48 |
=====
|
49 |
|
50 |
+
.. code-block:: python
|
51 |
|
52 |
from deep_translator import GoogleTranslator
|
53 |
|
|
|
59 |
|
60 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
61 |
|
|
|
62 |
# Alternatively, you can pass languages by their name:
|
|
|
63 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
64 |
|
65 |
+
##################### or maybe you want to translate a text file ? #############################
|
66 |
+
translated_text = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
|
67 |
+
|
68 |
+
# or maybe you have many sentences in different languages and want to automate the translation process
|
69 |
+
translated = GoogleTranslator(source='auto', target='de').translate_sentences(your_list_of_sentences)
|
70 |
+
|
71 |
+
###################### or use Pons as a translator ###########################
|
72 |
+
word = 'good'
|
73 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
74 |
|
deep_translator/__init__.py
CHANGED
@@ -5,7 +5,7 @@ from .pons import PonsTranslator
|
|
5 |
|
6 |
__author__ = """Nidhal Baccouri"""
|
7 |
__email__ = '[email protected]'
|
8 |
-
__version__ = '0.1.
|
9 |
|
10 |
__all__ = [GoogleTranslator,
|
11 |
PonsTranslator]
|
|
|
5 |
|
6 |
__author__ = """Nidhal Baccouri"""
|
7 |
__email__ = '[email protected]'
|
8 |
+
__version__ = '0.1.3'
|
9 |
|
10 |
__all__ = [GoogleTranslator,
|
11 |
PonsTranslator]
|
deep_translator/google_trans.py
CHANGED
@@ -59,13 +59,7 @@ class GoogleTranslator(BaseTranslator, ABC):
|
|
59 |
@return: str: translated text
|
60 |
"""
|
61 |
|
62 |
-
if
|
63 |
-
raise NotValidPayload(payload)
|
64 |
-
|
65 |
-
if not self._check_length(payload):
|
66 |
-
raise NotValidLength(payload)
|
67 |
-
|
68 |
-
try:
|
69 |
payload = payload.strip()
|
70 |
|
71 |
if self.payload_key:
|
@@ -79,8 +73,37 @@ class GoogleTranslator(BaseTranslator, ABC):
|
|
79 |
|
80 |
return element.get_text(strip=True)
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
except Exception as e:
|
83 |
-
raise
|
84 |
|
85 |
|
86 |
if __name__ == '__main__':
|
|
|
59 |
@return: str: translated text
|
60 |
"""
|
61 |
|
62 |
+
if self._validate_payload(payload):
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
payload = payload.strip()
|
64 |
|
65 |
if self.payload_key:
|
|
|
73 |
|
74 |
return element.get_text(strip=True)
|
75 |
|
76 |
+
def translate_file(self, path, **kwargs):
|
77 |
+
try:
|
78 |
+
with open(path) as f:
|
79 |
+
text = f.read()
|
80 |
+
|
81 |
+
return self.translate(payload=text)
|
82 |
+
except Exception as e:
|
83 |
+
raise e
|
84 |
+
|
85 |
+
def translate_sentences(self, sentences=None, **kwargs):
|
86 |
+
"""
|
87 |
+
translate many sentences together. This makes sense if you have sentences with different languages
|
88 |
+
and you want to translate all to unified language. This is handy because it detects
|
89 |
+
automatically the language of each sentence and then translate it.
|
90 |
+
|
91 |
+
@param sentences: list of sentences to translate
|
92 |
+
@return: list of all translated sentences
|
93 |
+
"""
|
94 |
+
if not sentences:
|
95 |
+
raise NotValidPayload(sentences)
|
96 |
+
|
97 |
+
translated_sentences = []
|
98 |
+
try:
|
99 |
+
for sentence in sentences:
|
100 |
+
translated = self.translate(payload=sentence)
|
101 |
+
translated_sentences.append(translated)
|
102 |
+
|
103 |
+
return translated_sentences
|
104 |
+
|
105 |
except Exception as e:
|
106 |
+
raise e
|
107 |
|
108 |
|
109 |
if __name__ == '__main__':
|
deep_translator/parent.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
"""Main module."""
|
2 |
|
3 |
-
from deep_translator.exceptions import NotValidPayload
|
4 |
from abc import ABC, abstractmethod
|
5 |
|
6 |
|
@@ -29,52 +29,25 @@ class BaseTranslator(ABC):
|
|
29 |
self.payload_key = payload_key
|
30 |
super(BaseTranslator, self).__init__()
|
31 |
|
32 |
-
|
|
|
33 |
"""
|
34 |
validate the payload text to translate
|
35 |
@param payload: text to translate
|
36 |
@return: bool
|
37 |
"""
|
38 |
-
if not payload or not isinstance(payload, str):
|
39 |
-
return False
|
40 |
|
|
|
|
|
|
|
|
|
41 |
return True
|
42 |
|
43 |
-
|
|
|
44 |
return True if min_chars < len(payload) < max_chars else False
|
45 |
|
46 |
@abstractmethod
|
47 |
def translate(self, payload, **kwargs):
|
48 |
pass
|
49 |
|
50 |
-
def translate_file(self, path, **kwargs):
|
51 |
-
try:
|
52 |
-
with open(path) as f:
|
53 |
-
text = f.read()
|
54 |
-
|
55 |
-
return self.translate(payload=text)
|
56 |
-
except Exception as e:
|
57 |
-
raise e
|
58 |
-
|
59 |
-
def translate_sentences(self, sentences=None, **kwargs):
|
60 |
-
"""
|
61 |
-
translate many sentences together. This makes sense if you have sentences with different languages
|
62 |
-
and you want to translate all to unified language. This is handy because it detects
|
63 |
-
automatically the language of each sentence and then translate it.
|
64 |
-
|
65 |
-
@param sentences: list of sentences to translate
|
66 |
-
@return: list of all translated sentences
|
67 |
-
"""
|
68 |
-
if not sentences:
|
69 |
-
raise NotValidPayload
|
70 |
-
|
71 |
-
translated_sentences = []
|
72 |
-
try:
|
73 |
-
for sentence in sentences:
|
74 |
-
translated = self.translate(payload=sentence)
|
75 |
-
translated_sentences.append(translated)
|
76 |
-
|
77 |
-
return translated_sentences
|
78 |
-
|
79 |
-
except Exception as e:
|
80 |
-
raise e
|
|
|
1 |
"""Main module."""
|
2 |
|
3 |
+
from deep_translator.exceptions import NotValidPayload, NotValidLength
|
4 |
from abc import ABC, abstractmethod
|
5 |
|
6 |
|
|
|
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 payload text to translate
|
36 |
@param payload: text to translate
|
37 |
@return: bool
|
38 |
"""
|
|
|
|
|
39 |
|
40 |
+
if not payload or not isinstance(payload, str):
|
41 |
+
raise NotValidPayload(payload)
|
42 |
+
if not BaseTranslator.__check_length(payload, min_chars, max_chars):
|
43 |
+
raise NotValidLength
|
44 |
return True
|
45 |
|
46 |
+
@staticmethod
|
47 |
+
def __check_length(payload, min_chars, max_chars):
|
48 |
return True if min_chars < len(payload) < max_chars else False
|
49 |
|
50 |
@abstractmethod
|
51 |
def translate(self, payload, **kwargs):
|
52 |
pass
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deep_translator/pons.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
from deep_translator.constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
5 |
-
from deep_translator.exceptions import LanguageNotSupportedException, ElementNotFoundInGetRequest
|
6 |
from deep_translator.parent import BaseTranslator
|
7 |
from requests.utils import quote
|
8 |
|
@@ -18,7 +18,7 @@ class PonsTranslator(BaseTranslator):
|
|
18 |
"""
|
19 |
self.__base_url = BASE_URLS.get("PONS")
|
20 |
|
21 |
-
if self.is_language_supported(source, target
|
22 |
self._source, self._target = self._map_language_to_code(source, target)
|
23 |
|
24 |
super().__init__(base_url=self.__base_url,
|
@@ -50,34 +50,49 @@ class PonsTranslator(BaseTranslator):
|
|
50 |
raise LanguageNotSupportedException(lang)
|
51 |
return True
|
52 |
|
53 |
-
def translate(self,
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
|
74 |
if __name__ == '__main__':
|
75 |
-
|
76 |
# res = GoogleTranslator(source='auto', target='french').translate_text(path='../examples/test.txt')
|
77 |
# res = GoogleTranslator(source='auto', target='french').translate_sentences([
|
78 |
# "this is good",
|
79 |
# "das Wetter ist schön",
|
80 |
# "un verme verde in un bicchiere verde"
|
81 |
# ])
|
82 |
-
res = PonsTranslator(source="
|
|
|
|
|
83 |
print(res)
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
from deep_translator.constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
5 |
+
from deep_translator.exceptions import LanguageNotSupportedException, ElementNotFoundInGetRequest, NotValidPayload
|
6 |
from deep_translator.parent import BaseTranslator
|
7 |
from requests.utils import quote
|
8 |
|
|
|
18 |
"""
|
19 |
self.__base_url = BASE_URLS.get("PONS")
|
20 |
|
21 |
+
if self.is_language_supported(source, target):
|
22 |
self._source, self._target = self._map_language_to_code(source, target)
|
23 |
|
24 |
super().__init__(base_url=self.__base_url,
|
|
|
50 |
raise LanguageNotSupportedException(lang)
|
51 |
return True
|
52 |
|
53 |
+
def translate(self, word, **kwargs):
|
54 |
|
55 |
+
if self._validate_payload(word):
|
56 |
+
url = "{}{}-{}/{}".format(self.__base_url, self._source, self._target, quote(word))
|
57 |
+
response = requests.get(url)
|
58 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
59 |
+
elements = soup.findAll(self._element_tag, self._element_query)
|
60 |
+
if not elements:
|
61 |
+
raise ElementNotFoundInGetRequest(elements)
|
62 |
|
63 |
+
eof = []
|
64 |
+
for el in elements:
|
65 |
+
temp = ''
|
66 |
+
for e in el.findAll('a'):
|
67 |
+
if e.parent.name == 'div':
|
68 |
+
if e and "/translate/{}-{}/".format(self._target, self._source) in e.get('href'):
|
69 |
+
temp += e.get_text() + ' '
|
70 |
+
if not kwargs.get('return_all'):
|
71 |
+
return temp
|
72 |
+
eof.append(temp)
|
73 |
|
74 |
+
if 'return_all' in kwargs and kwargs.get('return_all'):
|
75 |
+
return [word for word in eof if word and len(word) > 1]
|
76 |
+
|
77 |
+
def translate_words(self, words, **kwargs):
|
78 |
+
if not words:
|
79 |
+
raise NotValidPayload(words)
|
80 |
+
|
81 |
+
translated_words = []
|
82 |
+
for word in words:
|
83 |
+
translated_words.append(self.translate(payload=word))
|
84 |
+
return translated_words
|
85 |
|
86 |
|
87 |
if __name__ == '__main__':
|
88 |
+
|
89 |
# res = GoogleTranslator(source='auto', target='french').translate_text(path='../examples/test.txt')
|
90 |
# res = GoogleTranslator(source='auto', target='french').translate_sentences([
|
91 |
# "this is good",
|
92 |
# "das Wetter ist schön",
|
93 |
# "un verme verde in un bicchiere verde"
|
94 |
# ])
|
95 |
+
# res = PonsTranslator(source="en", target="ar").translate(payload='good')
|
96 |
+
res = PonsTranslator(source="en", target="ar").translate_words(words=('good', 'cute', 'angry'))
|
97 |
+
|
98 |
print(res)
|
docs/usage.rst
CHANGED
@@ -2,9 +2,10 @@
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
5 |
-
To use deep_translator in a project::
|
6 |
|
7 |
-
|
|
|
|
|
8 |
|
9 |
english_text = 'happy coding'
|
10 |
|
@@ -16,8 +17,9 @@ To use deep_translator in a project::
|
|
16 |
|
17 |
|
18 |
# Alternatively, you can pass languages by their name:
|
19 |
-
|
20 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
21 |
|
22 |
-
|
|
|
|
|
23 |
|
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
|
|
5 |
|
6 |
+
.. code-block:: python
|
7 |
+
|
8 |
+
from deep_translator import GoogleTranslator, PonsTranslator
|
9 |
|
10 |
english_text = 'happy coding'
|
11 |
|
|
|
17 |
|
18 |
|
19 |
# Alternatively, you can pass languages by their name:
|
|
|
20 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
21 |
|
22 |
+
###################### Use Pons as a translator ###########################
|
23 |
+
word = 'good'
|
24 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
25 |
|
setup.cfg
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
[bumpversion]
|
2 |
-
current_version = 0.1.
|
3 |
commit = True
|
4 |
tag = True
|
5 |
|
|
|
1 |
[bumpversion]
|
2 |
+
current_version = 0.1.3
|
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='0.1.
|
55 |
zip_safe=False,
|
56 |
)
|
|
|
51 |
test_suite='tests',
|
52 |
tests_require=test_requirements,
|
53 |
url='https://github.com/nidhaloff/deep_translator',
|
54 |
+
version='0.1.3',
|
55 |
zip_safe=False,
|
56 |
)
|