nidhal baccouri
commited on
Commit
·
7ade3be
1
Parent(s):
76688c0
added typing
Browse files- deep_translator/base.py +19 -16
- deep_translator/cli.py +6 -5
- deep_translator/deepl.py +10 -5
- deep_translator/detection.py +9 -3
- deep_translator/google.py +11 -4
- deep_translator/libre.py +11 -5
- deep_translator/linguee.py +8 -3
- deep_translator/microsoft.py +9 -8
- deep_translator/mymemory.py +13 -4
- deep_translator/papago.py +10 -5
- deep_translator/pons.py +8 -3
- deep_translator/qcri.py +14 -5
- deep_translator/validate.py +2 -2
- deep_translator/yandex.py +13 -6
deep_translator/base.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
|
4 |
from deep_translator.exceptions import InvalidSourceOrTargetLanguage
|
5 |
from abc import ABC, abstractmethod
|
|
|
6 |
|
7 |
|
8 |
class BaseTranslator(ABC):
|
@@ -12,13 +13,13 @@ class BaseTranslator(ABC):
|
|
12 |
|
13 |
def __init__(
|
14 |
self,
|
15 |
-
base_url
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
**url_params
|
23 |
):
|
24 |
"""
|
@@ -26,8 +27,8 @@ class BaseTranslator(ABC):
|
|
26 |
@param target: target language to translate to
|
27 |
"""
|
28 |
self._base_url = base_url
|
29 |
-
self.languages
|
30 |
-
self.supported_languages
|
31 |
if not source:
|
32 |
raise InvalidSourceOrTargetLanguage(source)
|
33 |
if not target:
|
@@ -55,18 +56,20 @@ class BaseTranslator(ABC):
|
|
55 |
elif language in self.languages.keys():
|
56 |
yield self.languages[language]
|
57 |
|
58 |
-
def _same_source_target(self):
|
59 |
return self._source == self._target
|
60 |
|
61 |
-
def get_supported_languages(
|
|
|
|
|
62 |
"""
|
63 |
-
return the supported languages by the
|
64 |
@param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
|
65 |
@return: list or dict
|
66 |
"""
|
67 |
return self.supported_languages if not as_dict else self.languages
|
68 |
|
69 |
-
def is_language_supported(self, language, **kwargs):
|
70 |
"""
|
71 |
check if the language is supported by the translator
|
72 |
@param language: a string for 1 language
|
@@ -82,7 +85,7 @@ class BaseTranslator(ABC):
|
|
82 |
return False
|
83 |
|
84 |
@abstractmethod
|
85 |
-
def translate(self, text, **kwargs):
|
86 |
"""
|
87 |
translate a text using a translator under the hood and return the translated text
|
88 |
@param text: text to translate
|
@@ -91,7 +94,7 @@ class BaseTranslator(ABC):
|
|
91 |
"""
|
92 |
return NotImplemented("You need to implement the translate method!")
|
93 |
|
94 |
-
def _translate_file(self, path, **kwargs):
|
95 |
"""
|
96 |
translate directly from file
|
97 |
@param path: path to the target file
|
@@ -106,7 +109,7 @@ class BaseTranslator(ABC):
|
|
106 |
except Exception as e:
|
107 |
raise e
|
108 |
|
109 |
-
def _translate_batch(self, batch
|
110 |
"""
|
111 |
translate a list of texts
|
112 |
@param batch: list of texts you want to translate
|
|
|
3 |
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
|
4 |
from deep_translator.exceptions import InvalidSourceOrTargetLanguage
|
5 |
from abc import ABC, abstractmethod
|
6 |
+
from typing import Optional, List, Union
|
7 |
|
8 |
|
9 |
class BaseTranslator(ABC):
|
|
|
13 |
|
14 |
def __init__(
|
15 |
self,
|
16 |
+
base_url: str,
|
17 |
+
languages: dict = GOOGLE_LANGUAGES_TO_CODES,
|
18 |
+
source: str = "auto",
|
19 |
+
target: str = "en",
|
20 |
+
payload_key: Optional[str] = None,
|
21 |
+
element_tag: Optional[str] = None,
|
22 |
+
element_query: Optional[dict] = None,
|
23 |
**url_params
|
24 |
):
|
25 |
"""
|
|
|
27 |
@param target: target language to translate to
|
28 |
"""
|
29 |
self._base_url = base_url
|
30 |
+
self.languages = languages
|
31 |
+
self.supported_languages = list(self.languages.keys())
|
32 |
if not source:
|
33 |
raise InvalidSourceOrTargetLanguage(source)
|
34 |
if not target:
|
|
|
56 |
elif language in self.languages.keys():
|
57 |
yield self.languages[language]
|
58 |
|
59 |
+
def _same_source_target(self) -> bool:
|
60 |
return self._source == self._target
|
61 |
|
62 |
+
def get_supported_languages(
|
63 |
+
self, as_dict: bool = False, **kwargs
|
64 |
+
) -> Union[list, dict]:
|
65 |
"""
|
66 |
+
return the supported languages by the Google translator
|
67 |
@param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
|
68 |
@return: list or dict
|
69 |
"""
|
70 |
return self.supported_languages if not as_dict else self.languages
|
71 |
|
72 |
+
def is_language_supported(self, language: str, **kwargs) -> bool:
|
73 |
"""
|
74 |
check if the language is supported by the translator
|
75 |
@param language: a string for 1 language
|
|
|
85 |
return False
|
86 |
|
87 |
@abstractmethod
|
88 |
+
def translate(self, text: str, **kwargs) -> str:
|
89 |
"""
|
90 |
translate a text using a translator under the hood and return the translated text
|
91 |
@param text: text to translate
|
|
|
94 |
"""
|
95 |
return NotImplemented("You need to implement the translate method!")
|
96 |
|
97 |
+
def _translate_file(self, path: str, **kwargs) -> str:
|
98 |
"""
|
99 |
translate directly from file
|
100 |
@param path: path to the target file
|
|
|
109 |
except Exception as e:
|
110 |
raise e
|
111 |
|
112 |
+
def _translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
113 |
"""
|
114 |
translate a list of texts
|
115 |
@param batch: list of texts you want to translate
|
deep_translator/cli.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
"""Console script for deep_translator."""
|
2 |
import argparse
|
3 |
from deep_translator.engines import __engines__
|
|
|
4 |
|
5 |
|
6 |
class CLI(object):
|
7 |
translators_dict = __engines__
|
8 |
translator = None
|
9 |
|
10 |
-
def __init__(self, custom_args=None):
|
11 |
self.custom_args = custom_args
|
12 |
self.args = self.parse_args()
|
13 |
translator_class = self.translators_dict.get(self.args.translator, None)
|
@@ -20,7 +21,7 @@ class CLI(object):
|
|
20 |
source=self.args.source, target=self.args.target
|
21 |
)
|
22 |
|
23 |
-
def translate(self):
|
24 |
"""
|
25 |
function used to provide translations from the parsed terminal arguments
|
26 |
@return: None
|
@@ -30,7 +31,7 @@ class CLI(object):
|
|
30 |
print("-" * 50)
|
31 |
print(f"Translation result: {res}")
|
32 |
|
33 |
-
def get_supported_languages(self):
|
34 |
"""
|
35 |
function used to return the languages supported by the translator service from the parsed terminal arguments
|
36 |
@return: None
|
@@ -42,7 +43,7 @@ class CLI(object):
|
|
42 |
print(f"Languages supported by '{self.args.translator}' are :\n")
|
43 |
print(translator_supported_languages)
|
44 |
|
45 |
-
def parse_args(self):
|
46 |
"""
|
47 |
function responsible for parsing terminal arguments and provide them for further use in the translation process
|
48 |
"""
|
@@ -86,7 +87,7 @@ class CLI(object):
|
|
86 |
)
|
87 |
return parsed_args
|
88 |
|
89 |
-
def run(self):
|
90 |
if self.args.languages:
|
91 |
self.get_supported_languages()
|
92 |
else:
|
|
|
1 |
"""Console script for deep_translator."""
|
2 |
import argparse
|
3 |
from deep_translator.engines import __engines__
|
4 |
+
from typing import Optional
|
5 |
|
6 |
|
7 |
class CLI(object):
|
8 |
translators_dict = __engines__
|
9 |
translator = None
|
10 |
|
11 |
+
def __init__(self, custom_args: Optional[list] = None):
|
12 |
self.custom_args = custom_args
|
13 |
self.args = self.parse_args()
|
14 |
translator_class = self.translators_dict.get(self.args.translator, None)
|
|
|
21 |
source=self.args.source, target=self.args.target
|
22 |
)
|
23 |
|
24 |
+
def translate(self) -> None:
|
25 |
"""
|
26 |
function used to provide translations from the parsed terminal arguments
|
27 |
@return: None
|
|
|
31 |
print("-" * 50)
|
32 |
print(f"Translation result: {res}")
|
33 |
|
34 |
+
def get_supported_languages(self) -> None:
|
35 |
"""
|
36 |
function used to return the languages supported by the translator service from the parsed terminal arguments
|
37 |
@return: None
|
|
|
43 |
print(f"Languages supported by '{self.args.translator}' are :\n")
|
44 |
print(translator_supported_languages)
|
45 |
|
46 |
+
def parse_args(self) -> argparse.Namespace:
|
47 |
"""
|
48 |
function responsible for parsing terminal arguments and provide them for further use in the translation process
|
49 |
"""
|
|
|
87 |
)
|
88 |
return parsed_args
|
89 |
|
90 |
+
def run(self) -> None:
|
91 |
if self.args.languages:
|
92 |
self.get_supported_languages()
|
93 |
else:
|
deep_translator/deepl.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import requests
|
2 |
-
|
3 |
from deep_translator.validate import is_empty, validate_input
|
4 |
from deep_translator.constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
5 |
from deep_translator.exceptions import (
|
@@ -16,7 +16,12 @@ class DeeplTranslator(BaseTranslator):
|
|
16 |
"""
|
17 |
|
18 |
def __init__(
|
19 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
20 |
):
|
21 |
"""
|
22 |
@param api_key: your DeeplTranslator api key.
|
@@ -41,7 +46,7 @@ class DeeplTranslator(BaseTranslator):
|
|
41 |
**kwargs
|
42 |
)
|
43 |
|
44 |
-
def translate(self, text, **kwargs):
|
45 |
"""
|
46 |
@param text: text to translate
|
47 |
@return: translated text
|
@@ -77,10 +82,10 @@ class DeeplTranslator(BaseTranslator):
|
|
77 |
# Process and return the response.
|
78 |
return res["translations"][0]["text"]
|
79 |
|
80 |
-
def translate_file(self, path, **kwargs):
|
81 |
return self._translate_file(path, **kwargs)
|
82 |
|
83 |
-
def translate_batch(self, batch, **kwargs):
|
84 |
"""
|
85 |
@param batch: list of texts to translate
|
86 |
@return: list of translations
|
|
|
1 |
import requests
|
2 |
+
from typing import Optional, List
|
3 |
from deep_translator.validate import is_empty, validate_input
|
4 |
from deep_translator.constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
5 |
from deep_translator.exceptions import (
|
|
|
16 |
"""
|
17 |
|
18 |
def __init__(
|
19 |
+
self,
|
20 |
+
api_key: Optional[str] = None,
|
21 |
+
source: str = "de",
|
22 |
+
target: str = "en",
|
23 |
+
use_free_api: bool = True,
|
24 |
+
**kwargs
|
25 |
):
|
26 |
"""
|
27 |
@param api_key: your DeeplTranslator api key.
|
|
|
46 |
**kwargs
|
47 |
)
|
48 |
|
49 |
+
def translate(self, text: str, **kwargs) -> str:
|
50 |
"""
|
51 |
@param text: text to translate
|
52 |
@return: translated text
|
|
|
82 |
# Process and return the response.
|
83 |
return res["translations"][0]["text"]
|
84 |
|
85 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
86 |
return self._translate_file(path, **kwargs)
|
87 |
|
88 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
89 |
"""
|
90 |
@param batch: list of texts to translate
|
91 |
@return: list of translations
|
deep_translator/detection.py
CHANGED
@@ -3,6 +3,8 @@ language detection API
|
|
3 |
"""
|
4 |
import requests
|
5 |
from requests.exceptions import HTTPError
|
|
|
|
|
6 |
|
7 |
# Module global config
|
8 |
config = {
|
@@ -14,7 +16,7 @@ config = {
|
|
14 |
}
|
15 |
|
16 |
|
17 |
-
def get_request_body(text, api_key, *args, **kwargs):
|
18 |
"""
|
19 |
send a request and return the response body parsed as dictionary
|
20 |
|
@@ -46,7 +48,9 @@ def get_request_body(text, api_key, *args, **kwargs):
|
|
46 |
raise e
|
47 |
|
48 |
|
49 |
-
def single_detection(
|
|
|
|
|
50 |
"""
|
51 |
function responsible for detecting the language from a text
|
52 |
|
@@ -66,7 +70,9 @@ def single_detection(text, api_key=None, detailed=False, *args, **kwargs):
|
|
66 |
return lang
|
67 |
|
68 |
|
69 |
-
def batch_detection(
|
|
|
|
|
70 |
"""
|
71 |
function responsible for detecting the language from a text
|
72 |
|
|
|
3 |
"""
|
4 |
import requests
|
5 |
from requests.exceptions import HTTPError
|
6 |
+
from typing import Optional, List, Union
|
7 |
+
|
8 |
|
9 |
# Module global config
|
10 |
config = {
|
|
|
16 |
}
|
17 |
|
18 |
|
19 |
+
def get_request_body(text: Union[str, List[str]], api_key: str, *args, **kwargs):
|
20 |
"""
|
21 |
send a request and return the response body parsed as dictionary
|
22 |
|
|
|
48 |
raise e
|
49 |
|
50 |
|
51 |
+
def single_detection(
|
52 |
+
text: str, api_key: Optional[str] = None, detailed: bool = False, *args, **kwargs
|
53 |
+
):
|
54 |
"""
|
55 |
function responsible for detecting the language from a text
|
56 |
|
|
|
70 |
return lang
|
71 |
|
72 |
|
73 |
+
def batch_detection(
|
74 |
+
text_list: List[str], api_key: str, detailed: bool = False, *args, **kwargs
|
75 |
+
):
|
76 |
"""
|
77 |
function responsible for detecting the language from a text
|
78 |
|
deep_translator/google.py
CHANGED
@@ -12,6 +12,7 @@ from deep_translator.base import BaseTranslator
|
|
12 |
from deep_translator.validate import validate_input, is_empty
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
|
|
15 |
|
16 |
|
17 |
class GoogleTranslator(BaseTranslator):
|
@@ -19,7 +20,13 @@ class GoogleTranslator(BaseTranslator):
|
|
19 |
class that wraps functions, which use google translate under the hood to translate text(s)
|
20 |
"""
|
21 |
|
22 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""
|
24 |
@param source: source language to translate from
|
25 |
@param target: target language to translate to
|
@@ -37,7 +44,7 @@ class GoogleTranslator(BaseTranslator):
|
|
37 |
|
38 |
self._alt_element_query = {"class": "result-container"}
|
39 |
|
40 |
-
def translate(self, text, **kwargs):
|
41 |
"""
|
42 |
function that uses google translate to translate a text
|
43 |
@param text: desired text to translate
|
@@ -89,7 +96,7 @@ class GoogleTranslator(BaseTranslator):
|
|
89 |
else:
|
90 |
return element.get_text(strip=True)
|
91 |
|
92 |
-
def translate_file(self, path, **kwargs):
|
93 |
"""
|
94 |
translate directly from file
|
95 |
@param path: path to the target file
|
@@ -99,7 +106,7 @@ class GoogleTranslator(BaseTranslator):
|
|
99 |
"""
|
100 |
return self._translate_file(path, **kwargs)
|
101 |
|
102 |
-
def translate_batch(self, batch
|
103 |
"""
|
104 |
translate a list of texts
|
105 |
@param batch: list of texts you want to translate
|
|
|
12 |
from deep_translator.validate import validate_input, is_empty
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
15 |
+
from typing import Optional, List
|
16 |
|
17 |
|
18 |
class GoogleTranslator(BaseTranslator):
|
|
|
20 |
class that wraps functions, which use google translate under the hood to translate text(s)
|
21 |
"""
|
22 |
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
source: str = "auto",
|
26 |
+
target: str = "en",
|
27 |
+
proxies: Optional[dict] = None,
|
28 |
+
**kwargs
|
29 |
+
):
|
30 |
"""
|
31 |
@param source: source language to translate from
|
32 |
@param target: target language to translate to
|
|
|
44 |
|
45 |
self._alt_element_query = {"class": "result-container"}
|
46 |
|
47 |
+
def translate(self, text: str, **kwargs) -> str:
|
48 |
"""
|
49 |
function that uses google translate to translate a text
|
50 |
@param text: desired text to translate
|
|
|
96 |
else:
|
97 |
return element.get_text(strip=True)
|
98 |
|
99 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
100 |
"""
|
101 |
translate directly from file
|
102 |
@param path: path to the target file
|
|
|
106 |
"""
|
107 |
return self._translate_file(path, **kwargs)
|
108 |
|
109 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
110 |
"""
|
111 |
translate a list of texts
|
112 |
@param batch: list of texts you want to translate
|
deep_translator/libre.py
CHANGED
@@ -3,7 +3,7 @@ LibreTranslate API
|
|
3 |
"""
|
4 |
|
5 |
import requests
|
6 |
-
|
7 |
from deep_translator.validate import is_empty, validate_input
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.constants import BASE_URLS, LIBRE_LANGUAGES_TO_CODES
|
@@ -19,7 +19,13 @@ class LibreTranslator(BaseTranslator):
|
|
19 |
class that wraps functions, which use libre translator under the hood to translate text(s)
|
20 |
"""
|
21 |
|
22 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""
|
24 |
@param source: source language to translate from
|
25 |
List of LibreTranslate nedpoints can be found at : https://github.com/LibreTranslate/LibreTranslate#mirrors
|
@@ -36,7 +42,7 @@ class LibreTranslator(BaseTranslator):
|
|
36 |
languages=LIBRE_LANGUAGES_TO_CODES,
|
37 |
)
|
38 |
|
39 |
-
def translate(self, text, **kwargs):
|
40 |
"""
|
41 |
function that uses microsoft translate to translate a text
|
42 |
@param text: desired text to translate
|
@@ -76,7 +82,7 @@ class LibreTranslator(BaseTranslator):
|
|
76 |
# Process and return the response.
|
77 |
return res["translatedText"]
|
78 |
|
79 |
-
def translate_file(self, path, **kwargs):
|
80 |
"""
|
81 |
translate directly from file
|
82 |
@param path: path to the target file
|
@@ -86,7 +92,7 @@ class LibreTranslator(BaseTranslator):
|
|
86 |
"""
|
87 |
return self._translate_file(path, **kwargs)
|
88 |
|
89 |
-
def translate_batch(self, batch
|
90 |
"""
|
91 |
translate a list of texts
|
92 |
@param batch: list of texts you want to translate
|
|
|
3 |
"""
|
4 |
|
5 |
import requests
|
6 |
+
from typing import Optional, List
|
7 |
from deep_translator.validate import is_empty, validate_input
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.constants import BASE_URLS, LIBRE_LANGUAGES_TO_CODES
|
|
|
19 |
class that wraps functions, which use libre translator under the hood to translate text(s)
|
20 |
"""
|
21 |
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
api_key: Optional[str] = None,
|
25 |
+
source: str = "auto",
|
26 |
+
target: str = "en",
|
27 |
+
**kwargs
|
28 |
+
):
|
29 |
"""
|
30 |
@param source: source language to translate from
|
31 |
List of LibreTranslate nedpoints can be found at : https://github.com/LibreTranslate/LibreTranslate#mirrors
|
|
|
42 |
languages=LIBRE_LANGUAGES_TO_CODES,
|
43 |
)
|
44 |
|
45 |
+
def translate(self, text: str, **kwargs) -> str:
|
46 |
"""
|
47 |
function that uses microsoft translate to translate a text
|
48 |
@param text: desired text to translate
|
|
|
82 |
# Process and return the response.
|
83 |
return res["translatedText"]
|
84 |
|
85 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
86 |
"""
|
87 |
translate directly from file
|
88 |
@param path: path to the target file
|
|
|
92 |
"""
|
93 |
return self._translate_file(path, **kwargs)
|
94 |
|
95 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
96 |
"""
|
97 |
translate a list of texts
|
98 |
@param batch: list of texts you want to translate
|
deep_translator/linguee.py
CHANGED
@@ -14,6 +14,7 @@ from deep_translator.base import BaseTranslator
|
|
14 |
from bs4 import BeautifulSoup
|
15 |
import requests
|
16 |
from requests.utils import requote_uri
|
|
|
17 |
|
18 |
|
19 |
class LingueeTranslator(BaseTranslator):
|
@@ -21,7 +22,9 @@ class LingueeTranslator(BaseTranslator):
|
|
21 |
class that wraps functions, which use the linguee translator under the hood to translate word(s)
|
22 |
"""
|
23 |
|
24 |
-
def __init__(
|
|
|
|
|
25 |
"""
|
26 |
@param source: source language to translate from
|
27 |
@param target: target language to translate to
|
@@ -37,7 +40,9 @@ class LingueeTranslator(BaseTranslator):
|
|
37 |
payload_key=None, # key of text in the url
|
38 |
)
|
39 |
|
40 |
-
def translate(
|
|
|
|
|
41 |
"""
|
42 |
function that uses linguee to translate a word
|
43 |
@param word: word to translate
|
@@ -82,7 +87,7 @@ class LingueeTranslator(BaseTranslator):
|
|
82 |
|
83 |
return filtered_elements if return_all else filtered_elements[0]
|
84 |
|
85 |
-
def translate_words(self, words, **kwargs):
|
86 |
"""
|
87 |
translate a batch of words together by providing them in a list
|
88 |
@param words: list of words you want to translate
|
|
|
14 |
from bs4 import BeautifulSoup
|
15 |
import requests
|
16 |
from requests.utils import requote_uri
|
17 |
+
from typing import Optional, List, Union
|
18 |
|
19 |
|
20 |
class LingueeTranslator(BaseTranslator):
|
|
|
22 |
class that wraps functions, which use the linguee translator under the hood to translate word(s)
|
23 |
"""
|
24 |
|
25 |
+
def __init__(
|
26 |
+
self, source: str, target: str = "en", proxies: Optional[dict] = None, **kwargs
|
27 |
+
):
|
28 |
"""
|
29 |
@param source: source language to translate from
|
30 |
@param target: target language to translate to
|
|
|
40 |
payload_key=None, # key of text in the url
|
41 |
)
|
42 |
|
43 |
+
def translate(
|
44 |
+
self, word: str, return_all: bool = False, **kwargs
|
45 |
+
) -> Union[str, List[str]]:
|
46 |
"""
|
47 |
function that uses linguee to translate a word
|
48 |
@param word: word to translate
|
|
|
87 |
|
88 |
return filtered_elements if return_all else filtered_elements[0]
|
89 |
|
90 |
+
def translate_words(self, words: List[str], **kwargs) -> List[str]:
|
91 |
"""
|
92 |
translate a batch of words together by providing them in a list
|
93 |
@param words: list of words you want to translate
|
deep_translator/microsoft.py
CHANGED
@@ -7,6 +7,7 @@ from deep_translator.constants import BASE_URLS
|
|
7 |
from deep_translator.exceptions import ServerException, MicrosoftAPIerror
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.validate import validate_input
|
|
|
10 |
|
11 |
|
12 |
class MicrosoftTranslator(BaseTranslator):
|
@@ -16,11 +17,11 @@ class MicrosoftTranslator(BaseTranslator):
|
|
16 |
|
17 |
def __init__(
|
18 |
self,
|
19 |
-
api_key=None,
|
20 |
-
region=None,
|
21 |
-
source=
|
22 |
-
target=
|
23 |
-
proxies=None,
|
24 |
**kwargs,
|
25 |
):
|
26 |
"""
|
@@ -60,7 +61,7 @@ class MicrosoftTranslator(BaseTranslator):
|
|
60 |
|
61 |
return {translation_dict[k]["name"].lower(): k for k in translation_dict.keys()}
|
62 |
|
63 |
-
def translate(self, text, **kwargs):
|
64 |
"""
|
65 |
function that uses microsoft translate to translate a text
|
66 |
@param text: desired text to translate
|
@@ -97,7 +98,7 @@ class MicrosoftTranslator(BaseTranslator):
|
|
97 |
]
|
98 |
return "\n".join(all_translations)
|
99 |
|
100 |
-
def translate_file(self, path, **kwargs):
|
101 |
"""
|
102 |
translate from a file
|
103 |
@param path: path to file
|
@@ -105,7 +106,7 @@ class MicrosoftTranslator(BaseTranslator):
|
|
105 |
"""
|
106 |
return self._translate_file(path, **kwargs)
|
107 |
|
108 |
-
def translate_batch(self, batch, **kwargs):
|
109 |
"""
|
110 |
translate a batch of texts
|
111 |
@param batch: list of texts to translate
|
|
|
7 |
from deep_translator.exceptions import ServerException, MicrosoftAPIerror
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.validate import validate_input
|
10 |
+
from typing import Optional, List
|
11 |
|
12 |
|
13 |
class MicrosoftTranslator(BaseTranslator):
|
|
|
17 |
|
18 |
def __init__(
|
19 |
self,
|
20 |
+
api_key: Optional[str] = None,
|
21 |
+
region: Optional[str] = None,
|
22 |
+
source: str = "auto",
|
23 |
+
target: str = "en",
|
24 |
+
proxies: Optional[dict] = None,
|
25 |
**kwargs,
|
26 |
):
|
27 |
"""
|
|
|
61 |
|
62 |
return {translation_dict[k]["name"].lower(): k for k in translation_dict.keys()}
|
63 |
|
64 |
+
def translate(self, text: str, **kwargs) -> str:
|
65 |
"""
|
66 |
function that uses microsoft translate to translate a text
|
67 |
@param text: desired text to translate
|
|
|
98 |
]
|
99 |
return "\n".join(all_translations)
|
100 |
|
101 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
102 |
"""
|
103 |
translate from a file
|
104 |
@param path: path to file
|
|
|
106 |
"""
|
107 |
return self._translate_file(path, **kwargs)
|
108 |
|
109 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
110 |
"""
|
111 |
translate a batch of texts
|
112 |
@param batch: list of texts to translate
|
deep_translator/mymemory.py
CHANGED
@@ -10,6 +10,7 @@ from deep_translator.exceptions import (
|
|
10 |
)
|
11 |
from deep_translator.base import BaseTranslator
|
12 |
import requests
|
|
|
13 |
|
14 |
|
15 |
class MyMemoryTranslator(BaseTranslator):
|
@@ -17,7 +18,13 @@ class MyMemoryTranslator(BaseTranslator):
|
|
17 |
class that uses the mymemory translator to translate texts
|
18 |
"""
|
19 |
|
20 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
"""
|
22 |
@param source: source language to translate from
|
23 |
@param target: target language to translate to
|
@@ -31,7 +38,9 @@ class MyMemoryTranslator(BaseTranslator):
|
|
31 |
payload_key="q",
|
32 |
)
|
33 |
|
34 |
-
def translate(
|
|
|
|
|
35 |
"""
|
36 |
function that uses the mymemory translator to translate a text
|
37 |
@param text: desired text to translate
|
@@ -73,7 +82,7 @@ class MyMemoryTranslator(BaseTranslator):
|
|
73 |
next_match = next(matches)
|
74 |
return next_match if not return_all else list(all_matches)
|
75 |
|
76 |
-
def translate_file(self, path, **kwargs):
|
77 |
"""
|
78 |
translate directly from file
|
79 |
@param path: path to the target file
|
@@ -83,7 +92,7 @@ class MyMemoryTranslator(BaseTranslator):
|
|
83 |
"""
|
84 |
return self._translate_file(path, **kwargs)
|
85 |
|
86 |
-
def translate_batch(self, batch
|
87 |
"""
|
88 |
translate a list of texts
|
89 |
@param batch: list of texts you want to translate
|
|
|
10 |
)
|
11 |
from deep_translator.base import BaseTranslator
|
12 |
import requests
|
13 |
+
from typing import Optional, List, Union
|
14 |
|
15 |
|
16 |
class MyMemoryTranslator(BaseTranslator):
|
|
|
18 |
class that uses the mymemory translator to translate texts
|
19 |
"""
|
20 |
|
21 |
+
def __init__(
|
22 |
+
self,
|
23 |
+
source: str = "auto",
|
24 |
+
target: str = "en",
|
25 |
+
proxies: Optional[dict] = None,
|
26 |
+
**kwargs,
|
27 |
+
):
|
28 |
"""
|
29 |
@param source: source language to translate from
|
30 |
@param target: target language to translate to
|
|
|
38 |
payload_key="q",
|
39 |
)
|
40 |
|
41 |
+
def translate(
|
42 |
+
self, text: str, return_all: bool = False, **kwargs
|
43 |
+
) -> Union[str, List[str]]:
|
44 |
"""
|
45 |
function that uses the mymemory translator to translate a text
|
46 |
@param text: desired text to translate
|
|
|
82 |
next_match = next(matches)
|
83 |
return next_match if not return_all else list(all_matches)
|
84 |
|
85 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
86 |
"""
|
87 |
translate directly from file
|
88 |
@param path: path to the target file
|
|
|
92 |
"""
|
93 |
return self._translate_file(path, **kwargs)
|
94 |
|
95 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
96 |
"""
|
97 |
translate a list of texts
|
98 |
@param batch: list of texts you want to translate
|
deep_translator/papago.py
CHANGED
@@ -6,7 +6,7 @@ from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
|
|
6 |
from deep_translator.exceptions import TranslationNotFound
|
7 |
from deep_translator.base import BaseTranslator
|
8 |
import requests
|
9 |
-
|
10 |
from deep_translator.validate import validate_input
|
11 |
|
12 |
|
@@ -16,7 +16,12 @@ class PapagoTranslator(BaseTranslator):
|
|
16 |
"""
|
17 |
|
18 |
def __init__(
|
19 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
20 |
):
|
21 |
"""
|
22 |
@param source: source language to translate from
|
@@ -37,7 +42,7 @@ class PapagoTranslator(BaseTranslator):
|
|
37 |
**kwargs,
|
38 |
)
|
39 |
|
40 |
-
def translate(self, text, **kwargs):
|
41 |
"""
|
42 |
function that uses google translate to translate a text
|
43 |
@param text: desired text to translate
|
@@ -66,7 +71,7 @@ class PapagoTranslator(BaseTranslator):
|
|
66 |
translated_text = result.get("translatedText")
|
67 |
return translated_text
|
68 |
|
69 |
-
def translate_file(self, path, **kwargs):
|
70 |
"""
|
71 |
translate directly from file
|
72 |
@param path: path to the target file
|
@@ -76,7 +81,7 @@ class PapagoTranslator(BaseTranslator):
|
|
76 |
"""
|
77 |
return self._translate_file(path, **kwargs)
|
78 |
|
79 |
-
def translate_batch(self, batch
|
80 |
"""
|
81 |
translate a list of texts
|
82 |
@param batch: list of texts you want to translate
|
|
|
6 |
from deep_translator.exceptions import TranslationNotFound
|
7 |
from deep_translator.base import BaseTranslator
|
8 |
import requests
|
9 |
+
from typing import Optional, List
|
10 |
from deep_translator.validate import validate_input
|
11 |
|
12 |
|
|
|
16 |
"""
|
17 |
|
18 |
def __init__(
|
19 |
+
self,
|
20 |
+
client_id: Optional[str] = None,
|
21 |
+
secret_key: Optional[str] = None,
|
22 |
+
source: str = "auto",
|
23 |
+
target: str = "en",
|
24 |
+
**kwargs,
|
25 |
):
|
26 |
"""
|
27 |
@param source: source language to translate from
|
|
|
42 |
**kwargs,
|
43 |
)
|
44 |
|
45 |
+
def translate(self, text: str, **kwargs) -> str:
|
46 |
"""
|
47 |
function that uses google translate to translate a text
|
48 |
@param text: desired text to translate
|
|
|
71 |
translated_text = result.get("translatedText")
|
72 |
return translated_text
|
73 |
|
74 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
75 |
"""
|
76 |
translate directly from file
|
77 |
@param path: path to the target file
|
|
|
81 |
"""
|
82 |
return self._translate_file(path, **kwargs)
|
83 |
|
84 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
85 |
"""
|
86 |
translate a list of texts
|
87 |
@param batch: list of texts you want to translate
|
deep_translator/pons.py
CHANGED
@@ -15,6 +15,7 @@ from deep_translator.exceptions import (
|
|
15 |
)
|
16 |
from deep_translator.base import BaseTranslator
|
17 |
from requests.utils import requote_uri
|
|
|
18 |
|
19 |
|
20 |
class PonsTranslator(BaseTranslator):
|
@@ -22,7 +23,9 @@ class PonsTranslator(BaseTranslator):
|
|
22 |
class that uses PONS translator to translate words
|
23 |
"""
|
24 |
|
25 |
-
def __init__(
|
|
|
|
|
26 |
"""
|
27 |
@param source: source language to translate from
|
28 |
@param target: target language to translate to
|
@@ -39,7 +42,9 @@ class PonsTranslator(BaseTranslator):
|
|
39 |
**kwargs,
|
40 |
)
|
41 |
|
42 |
-
def translate(
|
|
|
|
|
43 |
"""
|
44 |
function that uses PONS to translate a word
|
45 |
@param word: word to translate
|
@@ -84,7 +89,7 @@ class PonsTranslator(BaseTranslator):
|
|
84 |
|
85 |
return word_list if return_all else word_list[0]
|
86 |
|
87 |
-
def translate_words(self, words, **kwargs):
|
88 |
"""
|
89 |
translate a batch of words together by providing them in a list
|
90 |
@param words: list of words you want to translate
|
|
|
15 |
)
|
16 |
from deep_translator.base import BaseTranslator
|
17 |
from requests.utils import requote_uri
|
18 |
+
from typing import Optional, Union, List
|
19 |
|
20 |
|
21 |
class PonsTranslator(BaseTranslator):
|
|
|
23 |
class that uses PONS translator to translate words
|
24 |
"""
|
25 |
|
26 |
+
def __init__(
|
27 |
+
self, source: str, target: str = "en", proxies: Optional[dict] = None, **kwargs
|
28 |
+
):
|
29 |
"""
|
30 |
@param source: source language to translate from
|
31 |
@param target: target language to translate to
|
|
|
42 |
**kwargs,
|
43 |
)
|
44 |
|
45 |
+
def translate(
|
46 |
+
self, word: str, return_all: bool = False, **kwargs
|
47 |
+
) -> Union[str, List[str]]:
|
48 |
"""
|
49 |
function that uses PONS to translate a word
|
50 |
@param word: word to translate
|
|
|
89 |
|
90 |
return word_list if return_all else word_list[0]
|
91 |
|
92 |
+
def translate_words(self, words: List[str], **kwargs) -> List[str]:
|
93 |
"""
|
94 |
translate a batch of words together by providing them in a list
|
95 |
@param words: list of words you want to translate
|
deep_translator/qcri.py
CHANGED
@@ -2,6 +2,7 @@ import requests
|
|
2 |
from deep_translator.constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
|
3 |
from deep_translator.exceptions import ServerException, TranslationNotFound
|
4 |
from deep_translator.base import BaseTranslator
|
|
|
5 |
|
6 |
|
7 |
class QcriTranslator(BaseTranslator):
|
@@ -9,7 +10,13 @@ class QcriTranslator(BaseTranslator):
|
|
9 |
class that wraps functions, which use the QRCI translator under the hood to translate word(s)
|
10 |
"""
|
11 |
|
12 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
"""
|
14 |
@param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
|
15 |
"""
|
@@ -32,7 +39,9 @@ class QcriTranslator(BaseTranslator):
|
|
32 |
**kwargs,
|
33 |
)
|
34 |
|
35 |
-
def _get(
|
|
|
|
|
36 |
if not params:
|
37 |
params = self.params
|
38 |
try:
|
@@ -56,7 +65,7 @@ class QcriTranslator(BaseTranslator):
|
|
56 |
def domains(self):
|
57 |
return self.get_domains()
|
58 |
|
59 |
-
def translate(self, text, **kwargs):
|
60 |
params = {
|
61 |
"key": self.api_key,
|
62 |
"langpair": f"{self._source}-{self._target}",
|
@@ -78,10 +87,10 @@ class QcriTranslator(BaseTranslator):
|
|
78 |
raise TranslationNotFound(text)
|
79 |
return translation
|
80 |
|
81 |
-
def translate_file(self, path, **kwargs):
|
82 |
return self._translate_file(path, **kwargs)
|
83 |
|
84 |
-
def translate_batch(self, batch, **kwargs):
|
85 |
"""
|
86 |
translate a batch of texts
|
87 |
@domain: domain
|
|
|
2 |
from deep_translator.constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
|
3 |
from deep_translator.exceptions import ServerException, TranslationNotFound
|
4 |
from deep_translator.base import BaseTranslator
|
5 |
+
from typing import Optional, List
|
6 |
|
7 |
|
8 |
class QcriTranslator(BaseTranslator):
|
|
|
10 |
class that wraps functions, which use the QRCI translator under the hood to translate word(s)
|
11 |
"""
|
12 |
|
13 |
+
def __init__(
|
14 |
+
self,
|
15 |
+
api_key: Optional[str] = None,
|
16 |
+
source: str = "en",
|
17 |
+
target: str = "en",
|
18 |
+
**kwargs,
|
19 |
+
):
|
20 |
"""
|
21 |
@param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
|
22 |
"""
|
|
|
39 |
**kwargs,
|
40 |
)
|
41 |
|
42 |
+
def _get(
|
43 |
+
self, endpoint: str, params: Optional[dict] = None, return_text: bool = True
|
44 |
+
):
|
45 |
if not params:
|
46 |
params = self.params
|
47 |
try:
|
|
|
65 |
def domains(self):
|
66 |
return self.get_domains()
|
67 |
|
68 |
+
def translate(self, text: str, **kwargs) -> str:
|
69 |
params = {
|
70 |
"key": self.api_key,
|
71 |
"langpair": f"{self._source}-{self._target}",
|
|
|
87 |
raise TranslationNotFound(text)
|
88 |
return translation
|
89 |
|
90 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
91 |
return self._translate_file(path, **kwargs)
|
92 |
|
93 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
94 |
"""
|
95 |
translate a batch of texts
|
96 |
@domain: domain
|
deep_translator/validate.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
from deep_translator.exceptions import NotValidPayload, NotValidLength
|
2 |
|
3 |
|
4 |
-
def is_empty(text: str):
|
5 |
return text == ""
|
6 |
|
7 |
|
8 |
-
def validate_input(text: str, min_chars: int = 0, max_chars: int = 5000):
|
9 |
"""
|
10 |
validate the target text to translate
|
11 |
@param min_chars: min characters
|
|
|
1 |
from deep_translator.exceptions import NotValidPayload, NotValidLength
|
2 |
|
3 |
|
4 |
+
def is_empty(text: str) -> bool:
|
5 |
return text == ""
|
6 |
|
7 |
|
8 |
+
def validate_input(text: str, min_chars: int = 0, max_chars: int = 5000) -> bool:
|
9 |
"""
|
10 |
validate the target text to translate
|
11 |
@param min_chars: min characters
|
deep_translator/yandex.py
CHANGED
@@ -11,6 +11,7 @@ from deep_translator.exceptions import (
|
|
11 |
)
|
12 |
from deep_translator.base import BaseTranslator
|
13 |
from deep_translator.validate import validate_input
|
|
|
14 |
|
15 |
|
16 |
class YandexTranslator(BaseTranslator):
|
@@ -18,7 +19,13 @@ class YandexTranslator(BaseTranslator):
|
|
18 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
19 |
"""
|
20 |
|
21 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
"""
|
23 |
@param api_key: your yandex api key
|
24 |
"""
|
@@ -43,7 +50,7 @@ class YandexTranslator(BaseTranslator):
|
|
43 |
return self.get_supported_languages()
|
44 |
|
45 |
@property
|
46 |
-
def dirs(self, proxies=None):
|
47 |
|
48 |
try:
|
49 |
url = self._base_url.format(version=self.api_version, endpoint="getLangs")
|
@@ -58,7 +65,7 @@ class YandexTranslator(BaseTranslator):
|
|
58 |
raise ServerException(response.status_code)
|
59 |
return data.get("dirs")
|
60 |
|
61 |
-
def detect(self, text, proxies=None):
|
62 |
response = None
|
63 |
params = {
|
64 |
"text": text,
|
@@ -85,7 +92,7 @@ class YandexTranslator(BaseTranslator):
|
|
85 |
raise ServerException(501)
|
86 |
return language
|
87 |
|
88 |
-
def translate(self, text, proxies=None, **kwargs):
|
89 |
if validate_input(text):
|
90 |
params = {
|
91 |
"text": text,
|
@@ -116,7 +123,7 @@ class YandexTranslator(BaseTranslator):
|
|
116 |
|
117 |
return response["text"]
|
118 |
|
119 |
-
def translate_file(self, path, **kwargs):
|
120 |
"""
|
121 |
translate from a file
|
122 |
@param path: path to file
|
@@ -124,7 +131,7 @@ class YandexTranslator(BaseTranslator):
|
|
124 |
"""
|
125 |
return self._translate_file(path, **kwargs)
|
126 |
|
127 |
-
def translate_batch(self, batch, **kwargs):
|
128 |
"""
|
129 |
translate a batch of texts
|
130 |
@param batch: list of texts to translate
|
|
|
11 |
)
|
12 |
from deep_translator.base import BaseTranslator
|
13 |
from deep_translator.validate import validate_input
|
14 |
+
from typing import Optional, List
|
15 |
|
16 |
|
17 |
class YandexTranslator(BaseTranslator):
|
|
|
19 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
20 |
"""
|
21 |
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
api_key: Optional[str] = None,
|
25 |
+
source: str = "en",
|
26 |
+
target: str = "de",
|
27 |
+
**kwargs
|
28 |
+
):
|
29 |
"""
|
30 |
@param api_key: your yandex api key
|
31 |
"""
|
|
|
50 |
return self.get_supported_languages()
|
51 |
|
52 |
@property
|
53 |
+
def dirs(self, proxies: Optional[dict] = None):
|
54 |
|
55 |
try:
|
56 |
url = self._base_url.format(version=self.api_version, endpoint="getLangs")
|
|
|
65 |
raise ServerException(response.status_code)
|
66 |
return data.get("dirs")
|
67 |
|
68 |
+
def detect(self, text: str, proxies: Optional[dict] = None):
|
69 |
response = None
|
70 |
params = {
|
71 |
"text": text,
|
|
|
92 |
raise ServerException(501)
|
93 |
return language
|
94 |
|
95 |
+
def translate(self, text: str, proxies: Optional[dict] = None, **kwargs) -> str:
|
96 |
if validate_input(text):
|
97 |
params = {
|
98 |
"text": text,
|
|
|
123 |
|
124 |
return response["text"]
|
125 |
|
126 |
+
def translate_file(self, path: str, **kwargs) -> str:
|
127 |
"""
|
128 |
translate from a file
|
129 |
@param path: path to file
|
|
|
131 |
"""
|
132 |
return self._translate_file(path, **kwargs)
|
133 |
|
134 |
+
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
135 |
"""
|
136 |
translate a batch of texts
|
137 |
@param batch: list of texts to translate
|