Nidhal Baccouri
commited on
Commit
·
283a7d3
1
Parent(s):
f97759b
updated readme and format
Browse files- deep_translator/base.py +8 -4
- deep_translator/google.py +1 -1
- deep_translator/libre.py +5 -3
- deep_translator/microsoft.py +8 -3
- docs/README.rst +4 -3
- docs/conf.py +30 -31
- examples/libre.py +1 -1
- examples/linguee.py +1 -2
- examples/mymemory.py +1 -2
- examples/pons.py +1 -2
- examples/trans.py +8 -9
deep_translator/base.py
CHANGED
@@ -4,7 +4,10 @@ from abc import ABC, abstractmethod
|
|
4 |
from typing import List, Optional, Union
|
5 |
|
6 |
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
|
7 |
-
from deep_translator.exceptions import
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
class BaseTranslator(ABC):
|
@@ -21,7 +24,7 @@ class BaseTranslator(ABC):
|
|
21 |
payload_key: Optional[str] = None,
|
22 |
element_tag: Optional[str] = None,
|
23 |
element_query: Optional[dict] = None,
|
24 |
-
**url_params
|
25 |
):
|
26 |
"""
|
27 |
@param source: source language to translate from
|
@@ -76,8 +79,9 @@ class BaseTranslator(ABC):
|
|
76 |
raise LanguageNotSupportedException(
|
77 |
language,
|
78 |
message=f"No support for the provided language.\n"
|
79 |
-
|
80 |
-
|
|
|
81 |
|
82 |
def _same_source_target(self) -> bool:
|
83 |
return self._source == self._target
|
|
|
4 |
from typing import List, Optional, Union
|
5 |
|
6 |
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
|
7 |
+
from deep_translator.exceptions import (
|
8 |
+
InvalidSourceOrTargetLanguage,
|
9 |
+
LanguageNotSupportedException,
|
10 |
+
)
|
11 |
|
12 |
|
13 |
class BaseTranslator(ABC):
|
|
|
24 |
payload_key: Optional[str] = None,
|
25 |
element_tag: Optional[str] = None,
|
26 |
element_query: Optional[dict] = None,
|
27 |
+
**url_params,
|
28 |
):
|
29 |
"""
|
30 |
@param source: source language to translate from
|
|
|
79 |
raise LanguageNotSupportedException(
|
80 |
language,
|
81 |
message=f"No support for the provided language.\n"
|
82 |
+
f"Please select on of the supported languages:\n"
|
83 |
+
f"{self._languages}",
|
84 |
+
)
|
85 |
|
86 |
def _same_source_target(self) -> bool:
|
87 |
return self._source == self._target
|
deep_translator/google.py
CHANGED
@@ -118,6 +118,6 @@ class GoogleTranslator(BaseTranslator):
|
|
118 |
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
-
trans = GoogleTranslator(source=
|
122 |
res = trans.translate("good")
|
123 |
print("translation: ", res)
|
|
|
118 |
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
+
trans = GoogleTranslator(source="auto", target="zh-CN")
|
122 |
res = trans.translate("good")
|
123 |
print("translation: ", res)
|
deep_translator/libre.py
CHANGED
@@ -27,7 +27,7 @@ class LibreTranslator(BaseTranslator):
|
|
27 |
source: str = "en",
|
28 |
target: str = "es",
|
29 |
use_free_api: bool = True,
|
30 |
-
custom_url:
|
31 |
**kwargs
|
32 |
):
|
33 |
"""
|
@@ -40,7 +40,9 @@ class LibreTranslator(BaseTranslator):
|
|
40 |
@param custom_url: you can use a custom endpoint
|
41 |
"""
|
42 |
self.api_key = api_key
|
43 |
-
url =
|
|
|
|
|
44 |
super().__init__(
|
45 |
base_url=url if not custom_url else custom_url,
|
46 |
source=source,
|
@@ -107,7 +109,7 @@ class LibreTranslator(BaseTranslator):
|
|
107 |
return self._translate_batch(batch, **kwargs)
|
108 |
|
109 |
|
110 |
-
if __name__ ==
|
111 |
l = LibreTranslator(source="en", target="de")
|
112 |
res = l.translate("good")
|
113 |
print("res: ", res)
|
|
|
27 |
source: str = "en",
|
28 |
target: str = "es",
|
29 |
use_free_api: bool = True,
|
30 |
+
custom_url: Optional[str] = None,
|
31 |
**kwargs
|
32 |
):
|
33 |
"""
|
|
|
40 |
@param custom_url: you can use a custom endpoint
|
41 |
"""
|
42 |
self.api_key = api_key
|
43 |
+
url = (
|
44 |
+
BASE_URLS.get("LIBRE") if not use_free_api else BASE_URLS.get("LIBRE_FREE")
|
45 |
+
)
|
46 |
super().__init__(
|
47 |
base_url=url if not custom_url else custom_url,
|
48 |
source=source,
|
|
|
109 |
return self._translate_batch(batch, **kwargs)
|
110 |
|
111 |
|
112 |
+
if __name__ == "__main__":
|
113 |
l = LibreTranslator(source="en", target="de")
|
114 |
res = l.translate("good")
|
115 |
print("res: ", res)
|
deep_translator/microsoft.py
CHANGED
@@ -58,12 +58,17 @@ class MicrosoftTranslator(BaseTranslator):
|
|
58 |
# a common variable used in the other translators would be: MICROSOFT_CODES_TO_LANGUAGES
|
59 |
def _get_supported_languages(self):
|
60 |
|
61 |
-
microsoft_languages_api_url =
|
62 |
-
|
|
|
|
|
63 |
microsoft_languages_response = requests.get(microsoft_languages_api_url)
|
64 |
translation_dict = microsoft_languages_response.json()["translation"]
|
65 |
|
66 |
-
return {
|
|
|
|
|
|
|
67 |
|
68 |
def translate(self, text: str, **kwargs) -> str:
|
69 |
"""
|
|
|
58 |
# a common variable used in the other translators would be: MICROSOFT_CODES_TO_LANGUAGES
|
59 |
def _get_supported_languages(self):
|
60 |
|
61 |
+
microsoft_languages_api_url = (
|
62 |
+
"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope"
|
63 |
+
"=translation "
|
64 |
+
)
|
65 |
microsoft_languages_response = requests.get(microsoft_languages_api_url)
|
66 |
translation_dict = microsoft_languages_response.json()["translation"]
|
67 |
|
68 |
+
return {
|
69 |
+
translation_dict[k]["name"].lower(): k.lower()
|
70 |
+
for k in translation_dict.keys()
|
71 |
+
}
|
72 |
|
73 |
def translate(self, text: str, **kwargs) -> str:
|
74 |
"""
|
docs/README.rst
CHANGED
@@ -199,15 +199,16 @@ Check Supported Languages
|
|
199 |
.. note::
|
200 |
|
201 |
You can check the supported languages of each translator by calling the
|
202 |
-
get_supported_languages function
|
203 |
|
204 |
.. code-block:: python
|
205 |
|
206 |
# default return type is a list
|
207 |
-
langs_list = GoogleTranslator.get_supported_languages() # output: [arabic, french, english etc...]
|
208 |
|
209 |
# alternatively, you can the dictionary containing languages mapped to their abbreviation
|
210 |
-
langs_dict = GoogleTranslator.get_supported_languages(as_dict=True) # output: {arabic: ar, french: fr, english:en etc...}
|
|
|
211 |
|
212 |
Language Detection
|
213 |
------------------
|
|
|
199 |
.. note::
|
200 |
|
201 |
You can check the supported languages of each translator by calling the
|
202 |
+
get_supported_languages function.
|
203 |
|
204 |
.. code-block:: python
|
205 |
|
206 |
# default return type is a list
|
207 |
+
langs_list = GoogleTranslator().get_supported_languages() # output: [arabic, french, english etc...]
|
208 |
|
209 |
# alternatively, you can the dictionary containing languages mapped to their abbreviation
|
210 |
+
langs_dict = GoogleTranslator().get_supported_languages(as_dict=True) # output: {arabic: ar, french: fr, english:en etc...}
|
211 |
+
|
212 |
|
213 |
Language Detection
|
214 |
------------------
|
docs/conf.py
CHANGED
@@ -20,7 +20,7 @@
|
|
20 |
import os
|
21 |
import sys
|
22 |
|
23 |
-
sys.path.insert(0, os.path.abspath(
|
24 |
|
25 |
# import deep_translator
|
26 |
|
@@ -30,26 +30,26 @@ sys.path.insert(0, os.path.abspath('..'))
|
|
30 |
#
|
31 |
# needs_sphinx = '1.0'
|
32 |
|
33 |
-
html_logo =
|
34 |
|
35 |
# Add any Sphinx extension module names here, as strings. They can be
|
36 |
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
|
37 |
-
extensions = [
|
38 |
|
39 |
# Add any paths that contain templates here, relative to this directory.
|
40 |
-
templates_path = [
|
41 |
|
42 |
# The suffix(es) of source filenames.
|
43 |
# You can specify multiple suffix as a list of string:
|
44 |
#
|
45 |
# source_suffix = ['.rst', '.md']
|
46 |
-
source_suffix =
|
47 |
|
48 |
# The master toctree document.
|
49 |
-
master_doc =
|
50 |
|
51 |
# General information about the project.
|
52 |
-
project =
|
53 |
copyright = "2020, Nidhal Baccouri"
|
54 |
author = "Nidhal Baccouri"
|
55 |
|
@@ -62,7 +62,7 @@ import toml
|
|
62 |
|
63 |
with open("../pyproject.toml", "r") as f:
|
64 |
tom = toml.load(f)
|
65 |
-
version = tom[
|
66 |
# The full version, including alpha/beta/rc tags.
|
67 |
# release = deep_translator.__version__
|
68 |
|
@@ -76,10 +76,10 @@ language = None
|
|
76 |
# List of patterns, relative to source directory, that match files and
|
77 |
# directories to ignore when looking for source files.
|
78 |
# This patterns also effect to html_static_path and html_extra_path
|
79 |
-
exclude_patterns = [
|
80 |
|
81 |
# The name of the Pygments (syntax highlighting) style to use.
|
82 |
-
pygments_style =
|
83 |
|
84 |
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
85 |
todo_include_todos = False
|
@@ -90,7 +90,7 @@ todo_include_todos = False
|
|
90 |
# The theme to use for HTML and HTML Help pages. See the documentation for
|
91 |
# a list of builtin themes.
|
92 |
#
|
93 |
-
html_theme =
|
94 |
|
95 |
# Theme options are theme-specific and customize the look and feel of a
|
96 |
# theme further. For a list of options available for each theme, see the
|
@@ -101,13 +101,13 @@ html_theme = 'default'
|
|
101 |
# Add any paths that contain custom static files (such as style sheets) here,
|
102 |
# relative to this directory. They are copied after the builtin static files,
|
103 |
# so a file named "default.css" will overwrite the builtin "default.css".
|
104 |
-
html_static_path = [
|
105 |
|
106 |
|
107 |
# -- Options for HTMLHelp output ---------------------------------------
|
108 |
|
109 |
# Output file base name for HTML help builder.
|
110 |
-
htmlhelp_basename =
|
111 |
|
112 |
|
113 |
# -- Options for LaTeX output ------------------------------------------
|
@@ -116,15 +116,12 @@ latex_elements = {
|
|
116 |
# The paper size ('letterpaper' or 'a4paper').
|
117 |
#
|
118 |
# 'papersize': 'letterpaper',
|
119 |
-
|
120 |
# The font size ('10pt', '11pt' or '12pt').
|
121 |
#
|
122 |
# 'pointsize': '10pt',
|
123 |
-
|
124 |
# Additional stuff for the LaTeX preamble.
|
125 |
#
|
126 |
# 'preamble': '',
|
127 |
-
|
128 |
# Latex figure (float) alignment
|
129 |
#
|
130 |
# 'figure_align': 'htbp',
|
@@ -134,9 +131,13 @@ latex_elements = {
|
|
134 |
# (source start file, target name, title, author, documentclass
|
135 |
# [howto, manual, or own class]).
|
136 |
latex_documents = [
|
137 |
-
(
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
140 |
]
|
141 |
|
142 |
|
@@ -145,9 +146,7 @@ latex_documents = [
|
|
145 |
# One entry per manual page. List of tuples
|
146 |
# (source start file, name, description, authors, manual section).
|
147 |
man_pages = [
|
148 |
-
(master_doc,
|
149 |
-
'deep_translator Documentation',
|
150 |
-
[author], 1)
|
151 |
]
|
152 |
|
153 |
|
@@ -157,13 +156,13 @@ man_pages = [
|
|
157 |
# (source start file, target name, title, author,
|
158 |
# dir menu entry, description, category)
|
159 |
texinfo_documents = [
|
160 |
-
(
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
166 |
]
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
20 |
import os
|
21 |
import sys
|
22 |
|
23 |
+
sys.path.insert(0, os.path.abspath(".."))
|
24 |
|
25 |
# import deep_translator
|
26 |
|
|
|
30 |
#
|
31 |
# needs_sphinx = '1.0'
|
32 |
|
33 |
+
html_logo = "../assets/icon.jpg"
|
34 |
|
35 |
# Add any Sphinx extension module names here, as strings. They can be
|
36 |
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
|
37 |
+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
|
38 |
|
39 |
# Add any paths that contain templates here, relative to this directory.
|
40 |
+
templates_path = ["_templates"]
|
41 |
|
42 |
# The suffix(es) of source filenames.
|
43 |
# You can specify multiple suffix as a list of string:
|
44 |
#
|
45 |
# source_suffix = ['.rst', '.md']
|
46 |
+
source_suffix = ".rst"
|
47 |
|
48 |
# The master toctree document.
|
49 |
+
master_doc = "index"
|
50 |
|
51 |
# General information about the project.
|
52 |
+
project = "deep_translator"
|
53 |
copyright = "2020, Nidhal Baccouri"
|
54 |
author = "Nidhal Baccouri"
|
55 |
|
|
|
62 |
|
63 |
with open("../pyproject.toml", "r") as f:
|
64 |
tom = toml.load(f)
|
65 |
+
version = tom["tool"]["poetry"]["version"]
|
66 |
# The full version, including alpha/beta/rc tags.
|
67 |
# release = deep_translator.__version__
|
68 |
|
|
|
76 |
# List of patterns, relative to source directory, that match files and
|
77 |
# directories to ignore when looking for source files.
|
78 |
# This patterns also effect to html_static_path and html_extra_path
|
79 |
+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
|
80 |
|
81 |
# The name of the Pygments (syntax highlighting) style to use.
|
82 |
+
pygments_style = "sphinx"
|
83 |
|
84 |
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
85 |
todo_include_todos = False
|
|
|
90 |
# The theme to use for HTML and HTML Help pages. See the documentation for
|
91 |
# a list of builtin themes.
|
92 |
#
|
93 |
+
html_theme = "default"
|
94 |
|
95 |
# Theme options are theme-specific and customize the look and feel of a
|
96 |
# theme further. For a list of options available for each theme, see the
|
|
|
101 |
# Add any paths that contain custom static files (such as style sheets) here,
|
102 |
# relative to this directory. They are copied after the builtin static files,
|
103 |
# so a file named "default.css" will overwrite the builtin "default.css".
|
104 |
+
html_static_path = ["_static"]
|
105 |
|
106 |
|
107 |
# -- Options for HTMLHelp output ---------------------------------------
|
108 |
|
109 |
# Output file base name for HTML help builder.
|
110 |
+
htmlhelp_basename = "deep_translatordoc"
|
111 |
|
112 |
|
113 |
# -- Options for LaTeX output ------------------------------------------
|
|
|
116 |
# The paper size ('letterpaper' or 'a4paper').
|
117 |
#
|
118 |
# 'papersize': 'letterpaper',
|
|
|
119 |
# The font size ('10pt', '11pt' or '12pt').
|
120 |
#
|
121 |
# 'pointsize': '10pt',
|
|
|
122 |
# Additional stuff for the LaTeX preamble.
|
123 |
#
|
124 |
# 'preamble': '',
|
|
|
125 |
# Latex figure (float) alignment
|
126 |
#
|
127 |
# 'figure_align': 'htbp',
|
|
|
131 |
# (source start file, target name, title, author, documentclass
|
132 |
# [howto, manual, or own class]).
|
133 |
latex_documents = [
|
134 |
+
(
|
135 |
+
master_doc,
|
136 |
+
"deep_translator.tex",
|
137 |
+
"deep_translator Documentation",
|
138 |
+
"Nidhal Baccouri",
|
139 |
+
"manual",
|
140 |
+
),
|
141 |
]
|
142 |
|
143 |
|
|
|
146 |
# One entry per manual page. List of tuples
|
147 |
# (source start file, name, description, authors, manual section).
|
148 |
man_pages = [
|
149 |
+
(master_doc, "deep_translator", "deep_translator Documentation", [author], 1)
|
|
|
|
|
150 |
]
|
151 |
|
152 |
|
|
|
156 |
# (source start file, target name, title, author,
|
157 |
# dir menu entry, description, category)
|
158 |
texinfo_documents = [
|
159 |
+
(
|
160 |
+
master_doc,
|
161 |
+
"deep_translator",
|
162 |
+
"deep_translator Documentation",
|
163 |
+
author,
|
164 |
+
"deep_translator",
|
165 |
+
"A flexible free and unlimited python tool to translate between different languages in a simple way using multiple translators.",
|
166 |
+
"Miscellaneous",
|
167 |
+
),
|
168 |
]
|
|
|
|
|
|
examples/libre.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from deep_translator import LibreTranslator
|
2 |
|
3 |
-
res = LibreTranslator(source=
|
4 |
|
5 |
print(res)
|
|
|
1 |
from deep_translator import LibreTranslator
|
2 |
|
3 |
+
res = LibreTranslator(source="de", target="en").translate("laufen")
|
4 |
|
5 |
print(res)
|
examples/linguee.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
|
2 |
from deep_translator import LingueeTranslator
|
3 |
|
4 |
-
res = LingueeTranslator(source=
|
5 |
|
6 |
print(res)
|
|
|
|
|
1 |
from deep_translator import LingueeTranslator
|
2 |
|
3 |
+
res = LingueeTranslator(source="de", target="en").translate("laufen", return_all=False)
|
4 |
|
5 |
print(res)
|
examples/mymemory.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
|
2 |
from deep_translator import MyMemoryTranslator
|
3 |
|
4 |
-
res = MyMemoryTranslator(source=
|
5 |
|
6 |
print(res)
|
|
|
|
|
1 |
from deep_translator import MyMemoryTranslator
|
2 |
|
3 |
+
res = MyMemoryTranslator(source="ar", target="en").translate("آخُذ اَلْباص.")
|
4 |
|
5 |
print(res)
|
examples/pons.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
|
2 |
from deep_translator import PonsTranslator
|
3 |
|
4 |
-
res = PonsTranslator(source=
|
5 |
|
6 |
print(res)
|
|
|
|
|
1 |
from deep_translator import PonsTranslator
|
2 |
|
3 |
+
res = PonsTranslator(source="en", target="de").translate("good", return_all=False)
|
4 |
|
5 |
print(res)
|
examples/trans.py
CHANGED
@@ -2,9 +2,9 @@ from deep_translator import GoogleTranslator, LingueeTranslator, PonsTranslator
|
|
2 |
|
3 |
# examples using google translate
|
4 |
|
5 |
-
english_text =
|
6 |
-
chinese_text =
|
7 |
-
translator = GoogleTranslator(source=
|
8 |
result1 = translator.translate(text=english_text)
|
9 |
result2 = translator.translate(text=chinese_text)
|
10 |
|
@@ -12,16 +12,15 @@ print(f"original english text: {english_text} | translated text: {result1}")
|
|
12 |
print(f"original chinese text: {chinese_text} | translated text: {result2}")
|
13 |
|
14 |
# file translation
|
15 |
-
result_file = translator.translate_file(
|
16 |
print("file translation: ", result_file)
|
17 |
|
18 |
# examples using linguee:
|
19 |
-
text =
|
20 |
-
translated = LingueeTranslator(source=
|
21 |
print("Using Linguee ==> the translated text: ", translated)
|
22 |
|
23 |
# examples using pons:
|
24 |
-
text =
|
25 |
-
translated = PonsTranslator(source=
|
26 |
print("using Pons ==> the translated text: ", translated)
|
27 |
-
|
|
|
2 |
|
3 |
# examples using google translate
|
4 |
|
5 |
+
english_text = "happy coding"
|
6 |
+
chinese_text = "這很好"
|
7 |
+
translator = GoogleTranslator(source="auto", target="german")
|
8 |
result1 = translator.translate(text=english_text)
|
9 |
result2 = translator.translate(text=chinese_text)
|
10 |
|
|
|
12 |
print(f"original chinese text: {chinese_text} | translated text: {result2}")
|
13 |
|
14 |
# file translation
|
15 |
+
result_file = translator.translate_file("./test.txt")
|
16 |
print("file translation: ", result_file)
|
17 |
|
18 |
# examples using linguee:
|
19 |
+
text = "cute"
|
20 |
+
translated = LingueeTranslator(source="english", target="german").translate(word=text)
|
21 |
print("Using Linguee ==> the translated text: ", translated)
|
22 |
|
23 |
# examples using pons:
|
24 |
+
text = "good"
|
25 |
+
translated = PonsTranslator(source="en", target="ar").translate(word=text)
|
26 |
print("using Pons ==> the translated text: ", translated)
|
|