=
commited on
Commit
·
c754d32
1
Parent(s):
418aed1
added support for linguee.
Browse files- HISTORY.rst +1 -1
- README.rst +28 -9
- deep_translator/__init__.py +3 -3
- deep_translator/tests/test_deep_translator.py +19 -7
- docs/usage.rst +28 -9
- examples/trans.py +14 -1
- setup.cfg +1 -1
- setup.py +1 -1
HISTORY.rst
CHANGED
@@ -2,6 +2,6 @@
|
|
2 |
History
|
3 |
=======
|
4 |
|
5 |
-
0.1
|
6 |
---------------------
|
7 |
|
|
|
2 |
History
|
3 |
=======
|
4 |
|
5 |
+
0.3.1 stable release
|
6 |
---------------------
|
7 |
|
README.rst
CHANGED
@@ -49,26 +49,45 @@ Usage
|
|
49 |
|
50 |
.. code-block:: python
|
51 |
|
52 |
-
from deep_translator import GoogleTranslator
|
53 |
|
54 |
english_text = 'happy coding'
|
55 |
|
56 |
-
# first create a GoogleTranslator object with source and target language
|
57 |
-
# then use the translate function to translate a text.
|
58 |
-
# All language are supported. Basic example:
|
59 |
-
|
60 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
61 |
|
62 |
# Alternatively, you can pass languages by their name:
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
72 |
word = 'good'
|
73 |
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
.. code-block:: python
|
51 |
|
52 |
+
from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator
|
53 |
|
54 |
english_text = 'happy coding'
|
55 |
|
|
|
|
|
|
|
|
|
56 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
57 |
|
58 |
# Alternatively, you can pass languages by their name:
|
59 |
+
translated = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
60 |
|
61 |
+
# or maybe you want to translate a text file ?
|
62 |
+
translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
|
63 |
|
64 |
# or maybe you have many sentences in different languages and want to automate the translation process
|
65 |
translated = GoogleTranslator(source='auto', target='de').translate_sentences(your_list_of_sentences)
|
66 |
|
67 |
+
|
68 |
+
or maybe you would like to use the Pons translator: Pons.com
|
69 |
+
|
70 |
+
|
71 |
+
.. code-block:: python
|
72 |
+
|
73 |
word = 'good'
|
74 |
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
75 |
|
76 |
+
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
77 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
78 |
+
|
79 |
+
|
80 |
+
Alternatively deep_translator now supports the Linguee translator:
|
81 |
+
|
82 |
+
|
83 |
+
.. code-block:: python
|
84 |
+
|
85 |
+
word = 'good'
|
86 |
+
translated_word = LingueeTranslator(source='english', target='french').translate(word)
|
87 |
+
|
88 |
+
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
89 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
90 |
+
|
91 |
+
Take a look in the examples folder for more :)
|
92 |
+
|
93 |
+
Please contribute and give me a feedback if you found the package useful/helpful or you are using it :)
|
deep_translator/__init__.py
CHANGED
@@ -2,10 +2,10 @@
|
|
2 |
|
3 |
from .google_trans import GoogleTranslator
|
4 |
from .pons import PonsTranslator
|
|
|
5 |
|
6 |
__author__ = """Nidhal Baccouri"""
|
7 |
__email__ = '[email protected]'
|
8 |
-
__version__ = '0.1
|
9 |
|
10 |
-
__all__ = [GoogleTranslator,
|
11 |
-
PonsTranslator]
|
|
|
2 |
|
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__ = '0.3.1'
|
10 |
|
11 |
+
__all__ = [GoogleTranslator, PonsTranslator, LingueeTranslator]
|
|
deep_translator/tests/test_deep_translator.py
CHANGED
@@ -7,13 +7,22 @@ from deep_translator import exceptions, GoogleTranslator
|
|
7 |
|
8 |
|
9 |
@pytest.fixture
|
10 |
-
def
|
11 |
"""Sample pytest fixture.
|
12 |
|
13 |
See more at: http://doc.pytest.org/en/latest/fixture.html
|
14 |
"""
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
with pytest.raises(exceptions.LanguageNotSupportedException):
|
18 |
GoogleTranslator(source="", target="")
|
19 |
|
@@ -21,7 +30,10 @@ def response():
|
|
21 |
GoogleTranslator(source="auto", target="nothing")
|
22 |
|
23 |
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
@pytest.fixture
|
10 |
+
def google_translator():
|
11 |
"""Sample pytest fixture.
|
12 |
|
13 |
See more at: http://doc.pytest.org/en/latest/fixture.html
|
14 |
"""
|
15 |
+
return GoogleTranslator(target='en')
|
16 |
+
|
17 |
+
|
18 |
+
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(payload='좋은') == "good"
|
23 |
+
|
24 |
+
|
25 |
+
def test_inputs():
|
26 |
with pytest.raises(exceptions.LanguageNotSupportedException):
|
27 |
GoogleTranslator(source="", target="")
|
28 |
|
|
|
30 |
GoogleTranslator(source="auto", target="nothing")
|
31 |
|
32 |
|
33 |
+
def test_payload(google_translator):
|
34 |
+
with pytest.raises(exceptions.NotValidPayload):
|
35 |
+
google_translator.translate(payload="")
|
36 |
+
with pytest.raises(exceptions.NotValidPayload):
|
37 |
+
google_translator.translate(payload=123)
|
38 |
+
|
39 |
+
|
docs/usage.rst
CHANGED
@@ -2,24 +2,43 @@
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
5 |
-
|
6 |
.. code-block:: python
|
7 |
|
8 |
-
from deep_translator import GoogleTranslator, PonsTranslator
|
9 |
|
10 |
english_text = 'happy coding'
|
11 |
|
12 |
-
# first create a GoogleTranslator object with source and target language
|
13 |
-
# then use the translate function to translate a text.
|
14 |
-
# All language are supported. Basic example:
|
15 |
-
|
16 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
17 |
|
18 |
-
|
19 |
# Alternatively, you can pass languages by their name:
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
###################### Use Pons as a translator ###########################
|
23 |
word = 'good'
|
24 |
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
|
|
5 |
.. code-block:: python
|
6 |
|
7 |
+
from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator
|
8 |
|
9 |
english_text = 'happy coding'
|
10 |
|
|
|
|
|
|
|
|
|
11 |
result_german = GoogleTranslator(source='auto', target='de').translate(payload=english_text)
|
12 |
|
|
|
13 |
# Alternatively, you can pass languages by their name:
|
14 |
+
translated = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
15 |
+
|
16 |
+
# or maybe you want to translate a text file ?
|
17 |
+
translated = GoogleTranslator(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 = GoogleTranslator(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
|
24 |
+
|
25 |
+
|
26 |
+
.. code-block:: python
|
27 |
|
|
|
28 |
word = 'good'
|
29 |
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
30 |
|
31 |
+
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
32 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
33 |
+
|
34 |
+
|
35 |
+
Alternatively deep_translator now supports the Linguee translator:
|
36 |
+
|
37 |
+
|
38 |
+
.. code-block:: python
|
39 |
+
|
40 |
+
word = 'good'
|
41 |
+
translated_word = LingueeTranslator(source='english', target='french').translate(word)
|
42 |
+
|
43 |
+
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
44 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
examples/trans.py
CHANGED
@@ -1,10 +1,23 @@
|
|
1 |
-
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
2 |
|
3 |
english_text = 'happy coding'
|
4 |
chinese_text = '這很好'
|
|
|
5 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
6 |
result_french = GoogleTranslator(source='auto', target='french').translate(payload=chinese_text)
|
7 |
|
8 |
print(f"original english text: {english_text} | translated text: {result_german}") # result: fröhliche Codierung
|
9 |
print(f"original chinese text: {chinese_text} | translated text: {result_french}") # result: C' est bon
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator
|
2 |
+
|
3 |
+
|
4 |
+
# examples using google translate
|
5 |
|
6 |
english_text = 'happy coding'
|
7 |
chinese_text = '這很好'
|
8 |
+
|
9 |
result_german = GoogleTranslator(source='english', target='german').translate(payload=english_text)
|
10 |
result_french = GoogleTranslator(source='auto', target='french').translate(payload=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
|
14 |
|
15 |
+
# examples using linguee:
|
16 |
+
text = 'cute'
|
17 |
+
translated = LingueeTranslator(source='english', target='german').translate(word=text)
|
18 |
+
print("Using Linguee ==> the translated text: ", translated)
|
19 |
+
|
20 |
+
# examples using pons:
|
21 |
+
text = 'good'
|
22 |
+
translated = PonsTranslator(source='english', target='arabic').translate(word=text)
|
23 |
+
print("using Pons ==> the translated text: ", translated)
|
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.3.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='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.3.1',
|
55 |
zip_safe=False,
|
56 |
)
|