Merge pull request #60 from tuyenhn/cli-update
Browse files- deep_translator/cli.py +132 -45
- deep_translator/constants.py +10 -0
- deep_translator/qcri.py +6 -4
deep_translator/cli.py
CHANGED
@@ -1,75 +1,162 @@
|
|
1 |
"""Console script for deep_translator."""
|
2 |
|
3 |
-
import
|
4 |
-
import sys
|
5 |
from .google_trans import GoogleTranslator
|
6 |
from .mymemory import MyMemoryTranslator
|
7 |
-
from .
|
|
|
8 |
from .linguee import LingueeTranslator
|
|
|
|
|
|
|
|
|
9 |
|
|
|
10 |
|
11 |
-
|
|
|
12 |
"""
|
13 |
function used to provide translations from the parsed terminal arguments
|
14 |
-
@param
|
|
|
|
|
|
|
|
|
15 |
@return: None
|
16 |
"""
|
17 |
-
translator
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
else:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
res = translator.translate(args.text)
|
30 |
-
print(" | Translation from {} to {} |".format(args.source, args.target))
|
31 |
-
print("Translated text: \n {}".format(res))
|
32 |
|
33 |
-
def
|
34 |
"""
|
35 |
-
function used to
|
|
|
36 |
@param args: parsed terminal arguments
|
37 |
@return: None
|
38 |
"""
|
39 |
-
|
|
|
40 |
translator = GoogleTranslator
|
41 |
-
elif
|
42 |
-
translator = PonsTranslator
|
43 |
-
elif args.translator == 'linguee':
|
44 |
-
translator = LingueeTranslator
|
45 |
-
elif args.translator == 'mymemory':
|
46 |
translator = MyMemoryTranslator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
"""
|
57 |
-
|
58 |
-
|
|
|
59 |
"""
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
args = parser.parse_args()
|
69 |
-
if args.languages:
|
70 |
-
return_supported_languages(args)
|
71 |
else:
|
72 |
-
translate(
|
73 |
# sys.exit()
|
74 |
|
75 |
|
|
|
1 |
"""Console script for deep_translator."""
|
2 |
|
3 |
+
import click
|
|
|
4 |
from .google_trans import GoogleTranslator
|
5 |
from .mymemory import MyMemoryTranslator
|
6 |
+
from .deepl import DeepL
|
7 |
+
from .qcri import QCRI
|
8 |
from .linguee import LingueeTranslator
|
9 |
+
from .pons import PonsTranslator
|
10 |
+
from .yandex import YandexTranslator
|
11 |
+
from .microsoft import MicrosoftTranslator
|
12 |
+
from .papago import PapagoTranslator
|
13 |
|
14 |
+
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
15 |
|
16 |
+
|
17 |
+
def translate(translator, source, target, text, api_key):
|
18 |
"""
|
19 |
function used to provide translations from the parsed terminal arguments
|
20 |
+
@param translator: translator name parsed from terminal arguments
|
21 |
+
@param source: source language parsed from terminal arguments
|
22 |
+
@param target: target language parsed from terminal arguments
|
23 |
+
@param text: text that will be translated parsed from terminal arguments
|
24 |
+
@param api_key: api key for translators that requires them
|
25 |
@return: None
|
26 |
"""
|
27 |
+
if translator == "google":
|
28 |
+
translator = GoogleTranslator(source=source, target=target)
|
29 |
+
elif translator == "mymemory":
|
30 |
+
translator = MyMemoryTranslator(source=source, target=target)
|
31 |
+
elif translator == "deepl":
|
32 |
+
translator = DeepL(source=source, target=target, api_key=api_key)
|
33 |
+
elif translator == "qcri":
|
34 |
+
translator = QCRI(source=source, target=target, api_key=api_key)
|
35 |
+
elif translator == "linguee":
|
36 |
+
translator = LingueeTranslator(source=source, target=target)
|
37 |
+
elif translator == "pons":
|
38 |
+
translator = PonsTranslator(source=source, target=target)
|
39 |
+
elif translator == "yandex":
|
40 |
+
translator = YandexTranslator(
|
41 |
+
source=source,
|
42 |
+
target=target,
|
43 |
+
api_key=api_key
|
44 |
+
)
|
45 |
+
elif translator == "microsoft":
|
46 |
+
translator = MicrosoftTranslator(
|
47 |
+
source=source,
|
48 |
+
target=target,
|
49 |
+
api_key=api_key
|
50 |
+
)
|
51 |
+
elif translator == "papago":
|
52 |
+
translator = PapagoTranslator(
|
53 |
+
source=source,
|
54 |
+
target=target,
|
55 |
+
api_key=api_key
|
56 |
+
)
|
57 |
else:
|
58 |
+
click.echo(
|
59 |
+
"The given translator is not supported."
|
60 |
+
" Please use a translator supported by the deep_translator tool"
|
61 |
+
)
|
62 |
+
return
|
63 |
+
|
64 |
+
res = translator.translate(text)
|
65 |
+
click.echo(f" | Translation from {source} to {target} |")
|
66 |
+
click.echo(f"Translated text: \n {res}")
|
67 |
|
|
|
|
|
|
|
68 |
|
69 |
+
def print_supported_languages(requested_translator, api_key):
|
70 |
"""
|
71 |
+
function used to print the languages supported by the translator service
|
72 |
+
from the parsed terminal arguments
|
73 |
@param args: parsed terminal arguments
|
74 |
@return: None
|
75 |
"""
|
76 |
+
translator = None
|
77 |
+
if requested_translator == "google":
|
78 |
translator = GoogleTranslator
|
79 |
+
elif requested_translator == "mymemory":
|
|
|
|
|
|
|
|
|
80 |
translator = MyMemoryTranslator
|
81 |
+
elif requested_translator == "qcri":
|
82 |
+
translator = QCRI(api_key=api_key)
|
83 |
+
elif requested_translator == "linguee":
|
84 |
+
translator = LingueeTranslator
|
85 |
+
elif requested_translator == "pons":
|
86 |
+
translator = PonsTranslator
|
87 |
+
elif requested_translator == "yandex":
|
88 |
+
translator = YandexTranslator(api_key=api_key)
|
89 |
+
elif requested_translator == "microsoft":
|
90 |
+
translator = MicrosoftTranslator(api_key=api_key)
|
91 |
+
elif requested_translator == "papago":
|
92 |
+
translator = PapagoTranslator(api_key=api_key)
|
93 |
else:
|
94 |
+
click.echo(
|
95 |
+
"The given translator is not supported."
|
96 |
+
" Please use a translator supported by the deep_translator tool"
|
97 |
+
)
|
98 |
+
return
|
99 |
|
100 |
+
supported_languages = translator.get_supported_languages(as_dict=True)
|
101 |
+
click.echo(f"Languages supported by '{requested_translator}' are :")
|
102 |
+
for k, v in supported_languages.items():
|
103 |
+
click.echo(f"|- {k}: {v}")
|
104 |
|
105 |
|
106 |
+
@click.command(context_settings=CONTEXT_SETTINGS)
|
107 |
+
@click.option(
|
108 |
+
"--translator",
|
109 |
+
"-trans",
|
110 |
+
default="google",
|
111 |
+
type=str,
|
112 |
+
help="name of the translator you want to use",
|
113 |
+
show_default=True,
|
114 |
+
)
|
115 |
+
@click.option(
|
116 |
+
"--source",
|
117 |
+
"-src",
|
118 |
+
type=str,
|
119 |
+
help="source language to translate from"
|
120 |
+
)
|
121 |
+
@click.option(
|
122 |
+
"--target",
|
123 |
+
"-tg",
|
124 |
+
type=str,
|
125 |
+
help="target language to translate to"
|
126 |
+
)
|
127 |
+
@click.option(
|
128 |
+
"--text",
|
129 |
+
"-txt",
|
130 |
+
type=str,
|
131 |
+
help="text you want to translate"
|
132 |
+
)
|
133 |
+
@click.option(
|
134 |
+
"--api-key",
|
135 |
+
type=str,
|
136 |
+
help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators"
|
137 |
+
)
|
138 |
+
@click.option(
|
139 |
+
"--languages",
|
140 |
+
"-lang",
|
141 |
+
is_flag=True,
|
142 |
+
help="list all the languages available with the translator."
|
143 |
+
" Run with deep_translator -trans <translator service> -lang",
|
144 |
+
)
|
145 |
+
def main(translator, source, target, text, api_key, languages):
|
146 |
"""
|
147 |
+
\f
|
148 |
+
function responsible for parsing terminal arguments and provide them for
|
149 |
+
further use in the translation process
|
150 |
"""
|
151 |
+
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
152 |
+
if translator in api_key_required and not api_key:
|
153 |
+
click.echo(
|
154 |
+
"This translator requires an api key provided through --api-key"
|
155 |
+
)
|
156 |
+
elif languages:
|
157 |
+
print_supported_languages(translator, api_key)
|
|
|
|
|
|
|
|
|
158 |
else:
|
159 |
+
translate(translator, source, target, text, api_key)
|
160 |
# sys.exit()
|
161 |
|
162 |
|
deep_translator/constants.py
CHANGED
@@ -238,3 +238,13 @@ PAPAGO_CODE_TO_LANGUAGE = {
|
|
238 |
}
|
239 |
|
240 |
PAPAGO_LANGUAGE_TO_CODE = {v: k for v, k in PAPAGO_CODE_TO_LANGUAGE.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
}
|
239 |
|
240 |
PAPAGO_LANGUAGE_TO_CODE = {v: k for v, k in PAPAGO_CODE_TO_LANGUAGE.items()}
|
241 |
+
|
242 |
+
QCRI_CODE_TO_LANGUAGE = {
|
243 |
+
'ar': 'Arabic',
|
244 |
+
'en': 'English',
|
245 |
+
'es': 'Spanish'
|
246 |
+
}
|
247 |
+
|
248 |
+
QCRI_LANGUAGE_TO_CODE = {
|
249 |
+
v: k for k, v in QCRI_CODE_TO_LANGUAGE.items()
|
250 |
+
}
|
deep_translator/qcri.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
|
2 |
import requests
|
3 |
-
from deep_translator.constants import BASE_URLS
|
4 |
from deep_translator.exceptions import (
|
5 |
ServerException, TranslationNotFound)
|
6 |
|
@@ -40,10 +40,12 @@ class QCRI(object):
|
|
40 |
except Exception as e:
|
41 |
raise e
|
42 |
|
43 |
-
def get_supported_languages(self):
|
44 |
-
|
|
|
45 |
pairs = self._get("get_languages")
|
46 |
-
|
|
|
47 |
|
48 |
@property
|
49 |
def languages(self):
|
|
|
1 |
|
2 |
import requests
|
3 |
+
from deep_translator.constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
|
4 |
from deep_translator.exceptions import (
|
5 |
ServerException, TranslationNotFound)
|
6 |
|
|
|
40 |
except Exception as e:
|
41 |
raise e
|
42 |
|
43 |
+
def get_supported_languages(self, **kwargs):
|
44 |
+
# Have no use for this as the format is not what we need
|
45 |
+
# Save this for whenever
|
46 |
pairs = self._get("get_languages")
|
47 |
+
# Using a this one instead
|
48 |
+
return QCRI_LANGUAGE_TO_CODE
|
49 |
|
50 |
@property
|
51 |
def languages(self):
|