=
commited on
Commit
·
7167a1f
1
Parent(s):
8ab0bea
update readme
Browse files- README.rst +25 -0
- deep_translator/google_trans.py +5 -0
- deep_translator/mymemory.py +0 -5
- docs/usage.rst +170 -18
README.rst
CHANGED
@@ -105,6 +105,22 @@ Install the stable release:
|
|
105 |
|
106 |
take a look at the docs if you want to install from source.
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
=====
|
109 |
Usage
|
110 |
=====
|
@@ -270,6 +286,11 @@ Linguee Translator
|
|
270 |
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
271 |
translated_word = LingueeTranslator(source='english', target='french').translate(word, return_all=True)
|
272 |
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
PONS Translator
|
275 |
----------------
|
@@ -299,7 +320,11 @@ PONS Translator
|
|
299 |
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
300 |
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
301 |
|
|
|
|
|
|
|
302 |
|
|
|
303 |
|
304 |
Usage from Terminal
|
305 |
--------------------
|
|
|
105 |
|
106 |
take a look at the docs if you want to install from source.
|
107 |
|
108 |
+
============
|
109 |
+
Quick Start
|
110 |
+
============
|
111 |
+
|
112 |
+
.. code-block:: python
|
113 |
+
|
114 |
+
from deep_translator import GoogleTranslator
|
115 |
+
translated = GoogleTranslator(source='auto', target='de').translate("keep it up, you are awesome") # output -> Weiter so, du bist großartig
|
116 |
+
|
117 |
+
or from terminal
|
118 |
+
|
119 |
+
.. code-block:: console
|
120 |
+
|
121 |
+
$ deep_translator -trans "google" -src "en" -tg "de" -txt "keep it up, you are awesome"
|
122 |
+
|
123 |
+
|
124 |
=====
|
125 |
Usage
|
126 |
=====
|
|
|
286 |
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
287 |
translated_word = LingueeTranslator(source='english', target='french').translate(word, return_all=True)
|
288 |
|
289 |
+
- Translate a batch of words
|
290 |
+
|
291 |
+
.. code-block:: python
|
292 |
+
|
293 |
+
translated_words = LingueeTranslator(source='english', target='french').translate_words(["good", "awesome"])
|
294 |
|
295 |
PONS Translator
|
296 |
----------------
|
|
|
320 |
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
321 |
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
322 |
|
323 |
+
- Translate a batch of words
|
324 |
+
|
325 |
+
.. code-block:: python
|
326 |
|
327 |
+
translated_words = LingueeTranslator(source='english', target='french').translate_words(["good", "awesome"])
|
328 |
|
329 |
Usage from Terminal
|
330 |
--------------------
|
deep_translator/google_trans.py
CHANGED
@@ -135,3 +135,8 @@ class GoogleTranslator(BaseTranslator):
|
|
135 |
|
136 |
except Exception as e:
|
137 |
raise e
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
except Exception as e:
|
137 |
raise e
|
138 |
+
|
139 |
+
|
140 |
+
if __name__ == '__main__':
|
141 |
+
res = GoogleTranslator("auto", "de").translate("keep it up, you are awesome")
|
142 |
+
print(res)
|
deep_translator/mymemory.py
CHANGED
@@ -143,8 +143,3 @@ class MyMemoryTranslator(BaseTranslator):
|
|
143 |
except Exception as e:
|
144 |
raise e
|
145 |
|
146 |
-
|
147 |
-
if __name__ == '__main__':
|
148 |
-
|
149 |
-
res = MyMemoryTranslator('de', 'english').translate_sentences(["hallo welt", "guten morgen"])
|
150 |
-
print(res)
|
|
|
143 |
except Exception as e:
|
144 |
raise e
|
145 |
|
|
|
|
|
|
|
|
|
|
docs/usage.rst
CHANGED
@@ -2,58 +2,210 @@
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
.. code-block:: python
|
6 |
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
-
|
|
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
.. code-block:: python
|
27 |
|
28 |
-
|
29 |
-
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
30 |
|
31 |
-
|
32 |
-
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
33 |
|
|
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = LingueeTranslator(source='english', target='french').translate(word, return_all=True)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
48 |
|
49 |
.. code-block:: python
|
50 |
|
51 |
-
word = '
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
54 |
|
55 |
Usage from Terminal
|
56 |
-
|
57 |
|
58 |
For a quick access, you can use the deep_translator from terminal. For this to work, you need to provide
|
59 |
the right arguments, which are the translator you want to use, source language, target language and the text
|
|
|
2 |
Usage
|
3 |
=====
|
4 |
|
5 |
+
|
6 |
+
In this section, demos on how to use all different integrated translators in this tool are provided.
|
7 |
+
This includes the google, pons, linguee and mymemory translator (at least for now). Perhaps more
|
8 |
+
translators will be integrated in the future.
|
9 |
+
|
10 |
+
.. note::
|
11 |
+
|
12 |
+
You can always pass the languages by the name or by abbreviation.
|
13 |
+
|
14 |
+
*Example*: If you want to use english as a source or target language, you can pass **english** or **en** as an argument
|
15 |
+
|
16 |
+
Imports
|
17 |
+
--------
|
18 |
+
|
19 |
+
.. code-block:: python
|
20 |
+
|
21 |
+
from deep_translator import (GoogleTranslator,
|
22 |
+
PonsTranslator,
|
23 |
+
LingueeTranslator,
|
24 |
+
MyMemoryTranslator,
|
25 |
+
single_detection,
|
26 |
+
batch_detection)
|
27 |
+
|
28 |
+
|
29 |
+
Check Supported Languages
|
30 |
+
---------------------------
|
31 |
+
|
32 |
+
.. note::
|
33 |
+
|
34 |
+
You can check the supported languages of each translator by calling the
|
35 |
+
get_supported_languages function as a static method.
|
36 |
+
|
37 |
+
.. code-block:: python
|
38 |
+
|
39 |
+
# default return type is a list
|
40 |
+
langs_list = GoogleTranslator.get_supported_languages() # output: [arabic, french, english etc...]
|
41 |
+
|
42 |
+
# alternatively, you can the dictionary containing languages mapped to their abbreviation
|
43 |
+
langs_dict = GoogleTranslator.get_supported_languages(as_dict=True) # output: {arabic: ar, french: fr, english:en etc...}
|
44 |
+
|
45 |
+
Language Detection
|
46 |
+
------------------
|
47 |
+
|
48 |
+
.. note::
|
49 |
+
|
50 |
+
You can also detect language automatically. Notice that this package is free and my goal is to keep it free.
|
51 |
+
Therefore, you will need to get your own api_key if you want to use the language detection function.
|
52 |
+
I figured out you can get one for free here: https://detectlanguage.com/documentation
|
53 |
+
|
54 |
+
- Single Text Detection
|
55 |
+
|
56 |
+
.. code-block:: python
|
57 |
+
|
58 |
+
lang = single_detection('bonjour la vie', api_key='your_api_key')
|
59 |
+
print(lang) # output: fr
|
60 |
+
|
61 |
+
- Batch Detection
|
62 |
+
|
63 |
+
.. code-block:: python
|
64 |
+
|
65 |
+
lang = batch_detection(['bonjour la vie', 'hello world'], api_key='your_api_key')
|
66 |
+
print(lang) # output: [fr, en]
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
Google Translate
|
71 |
+
-----------------
|
72 |
+
|
73 |
.. code-block:: python
|
74 |
|
75 |
+
text = 'happy coding'
|
76 |
+
|
77 |
+
- You can use automatic language detection to detect the source language:
|
78 |
|
79 |
+
.. code-block:: python
|
80 |
+
|
81 |
+
translated = GoogleTranslator(source='auto', target='de').translate(text=text)
|
82 |
|
83 |
+
- You can pass languages by name:
|
84 |
|
85 |
+
.. code-block:: python
|
86 |
+
|
87 |
+
translated = GoogleTranslator(source='auto', target='german').translate(text=text)
|
88 |
+
|
89 |
+
- Alternatively, you can pass languages by their abbreviation:
|
90 |
+
|
91 |
+
.. code-block:: python
|
92 |
+
|
93 |
+
translated = GoogleTranslator(source='en', target='de').translate(text=text)
|
94 |
+
|
95 |
+
- Translate from a file:
|
96 |
+
|
97 |
+
.. code-block:: python
|
98 |
|
|
|
99 |
translated = GoogleTranslator(source='auto', target='german').translate_file('path/to/file')
|
100 |
|
101 |
+
- Automate translation by detecting the source language and translate it automatically to the desired language
|
102 |
+
|
103 |
+
.. code-block:: python
|
104 |
+
|
105 |
+
# this is useful if you have many sentences in different languages and want to automate the translation process
|
106 |
+
translated = GoogleTranslator(source='auto', target='de').translate_sentences([your_list_of_sentences])
|
107 |
|
108 |
|
109 |
+
Mymemory Translator
|
110 |
+
--------------------
|
111 |
|
112 |
+
.. note::
|
113 |
+
|
114 |
+
As in google translate, you can use the automatic language detection with mymemory by using "auto" as an
|
115 |
+
argument for the source language. However, this feature in the mymemory translator is not so powerful as
|
116 |
+
in google translate.
|
117 |
+
|
118 |
+
- Simple translation
|
119 |
|
120 |
.. code-block:: python
|
121 |
|
122 |
+
text = 'Keep it up. You are awesome'
|
|
|
123 |
|
124 |
+
translated = MyMemoryTranslator(source='auto', target='french').translate(text)
|
|
|
125 |
|
126 |
+
- Translate a list of sentences or paragraphs
|
127 |
|
128 |
+
.. code-block:: python
|
129 |
+
|
130 |
+
texts = ["hallo welt", "guten morgen"]
|
131 |
+
translated = MyMemoryTranslator('de', 'english').translate_sentences(texts)
|
132 |
+
|
133 |
+
- Translate from file
|
134 |
+
|
135 |
+
.. code-block:: python
|
136 |
+
|
137 |
+
path = "your_file.txt"
|
138 |
+
|
139 |
+
translated = MyMemoryTranslator(source='en', target='fr').translate_file(path)
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
Linguee Translator
|
144 |
+
-------------------
|
145 |
|
146 |
|
147 |
.. code-block:: python
|
148 |
|
149 |
word = 'good'
|
150 |
+
|
151 |
+
- Simple Translation
|
152 |
+
|
153 |
+
.. code-block:: python
|
154 |
+
|
155 |
translated_word = LingueeTranslator(source='english', target='french').translate(word)
|
156 |
|
157 |
+
# pass language by their abbreviation
|
158 |
+
translated_word = LingueeTranslator(source='en', target='fr').translate(word)
|
159 |
+
|
160 |
+
- Return all synonyms or words that matches
|
161 |
+
|
162 |
+
.. code-block:: python
|
163 |
+
|
164 |
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
165 |
translated_word = LingueeTranslator(source='english', target='french').translate(word, return_all=True)
|
166 |
|
167 |
+
- Translate a batch of words
|
168 |
+
|
169 |
+
.. code-block:: python
|
170 |
+
|
171 |
+
translated_words = LingueeTranslator(source='english', target='french').translate_words(["good", "awesome"])
|
172 |
+
|
173 |
+
PONS Translator
|
174 |
+
----------------
|
175 |
+
|
176 |
+
.. note::
|
177 |
|
178 |
+
You can pass the languages by the name or by abbreviation just like
|
179 |
+
previous examples using GoogleTranslate
|
180 |
|
181 |
.. code-block:: python
|
182 |
|
183 |
+
word = 'awesome'
|
184 |
+
|
185 |
+
- Simple Translation
|
186 |
+
|
187 |
+
.. code-block:: python
|
188 |
+
|
189 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word)
|
190 |
+
|
191 |
+
# pass language by their abbreviation
|
192 |
+
translated_word = PonsTranslator(source='en', target='fr').translate(word)
|
193 |
+
|
194 |
+
- Return all synonyms or words that matches
|
195 |
+
|
196 |
+
.. code-block:: python
|
197 |
+
|
198 |
+
# set the argument return_all to True if you want to get all synonyms of the word to translate
|
199 |
+
translated_word = PonsTranslator(source='english', target='french').translate(word, return_all=True)
|
200 |
+
|
201 |
+
- Translate a batch of words
|
202 |
+
|
203 |
+
.. code-block:: python
|
204 |
|
205 |
+
translated_words = LingueeTranslator(source='english', target='french').translate_words(["good", "awesome"])
|
206 |
|
207 |
Usage from Terminal
|
208 |
+
--------------------
|
209 |
|
210 |
For a quick access, you can use the deep_translator from terminal. For this to work, you need to provide
|
211 |
the right arguments, which are the translator you want to use, source language, target language and the text
|