nidhal baccouri
commited on
Commit
·
76aa3b2
1
Parent(s):
7ade3be
renamed input validation func
Browse files- deep_translator/deepl.py +2 -2
- deep_translator/google.py +4 -4
- deep_translator/libre.py +2 -2
- deep_translator/linguee.py +2 -2
- deep_translator/microsoft.py +2 -2
- deep_translator/mymemory.py +2 -2
- deep_translator/papago.py +2 -2
- deep_translator/pons.py +2 -2
- deep_translator/validate.py +1 -1
- deep_translator/yandex.py +2 -2
- tests/test_deepl.py +1 -1
- tests/test_google.py +2 -2
- tests/test_libre.py +4 -4
deep_translator/deepl.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import requests
|
2 |
from typing import Optional, List
|
3 |
-
from deep_translator.validate import is_empty,
|
4 |
from deep_translator.constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
5 |
from deep_translator.exceptions import (
|
6 |
ServerException,
|
@@ -51,7 +51,7 @@ class DeeplTranslator(BaseTranslator):
|
|
51 |
@param text: text to translate
|
52 |
@return: translated text
|
53 |
"""
|
54 |
-
if
|
55 |
if self._same_source_target() or is_empty(text):
|
56 |
return text
|
57 |
|
|
|
1 |
import requests
|
2 |
from typing import Optional, List
|
3 |
+
from deep_translator.validate import is_empty, is_input_valid
|
4 |
from deep_translator.constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
5 |
from deep_translator.exceptions import (
|
6 |
ServerException,
|
|
|
51 |
@param text: text to translate
|
52 |
@return: translated text
|
53 |
"""
|
54 |
+
if is_input_valid(text):
|
55 |
if self._same_source_target() or is_empty(text):
|
56 |
return text
|
57 |
|
deep_translator/google.py
CHANGED
@@ -9,7 +9,7 @@ from deep_translator.exceptions import (
|
|
9 |
RequestError,
|
10 |
)
|
11 |
from deep_translator.base import BaseTranslator
|
12 |
-
from deep_translator.validate import
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
15 |
from typing import Optional, List
|
@@ -17,7 +17,7 @@ from typing import Optional, List
|
|
17 |
|
18 |
class GoogleTranslator(BaseTranslator):
|
19 |
"""
|
20 |
-
class that wraps functions, which use
|
21 |
"""
|
22 |
|
23 |
def __init__(
|
@@ -46,11 +46,11 @@ class GoogleTranslator(BaseTranslator):
|
|
46 |
|
47 |
def translate(self, text: str, **kwargs) -> str:
|
48 |
"""
|
49 |
-
function
|
50 |
@param text: desired text to translate
|
51 |
@return: str: translated text
|
52 |
"""
|
53 |
-
if
|
54 |
text = text.strip()
|
55 |
if self._same_source_target() or is_empty(text):
|
56 |
return text
|
|
|
9 |
RequestError,
|
10 |
)
|
11 |
from deep_translator.base import BaseTranslator
|
12 |
+
from deep_translator.validate import is_input_valid, is_empty
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
15 |
from typing import Optional, List
|
|
|
17 |
|
18 |
class GoogleTranslator(BaseTranslator):
|
19 |
"""
|
20 |
+
class that wraps functions, which use Google Translate under the hood to translate text(s)
|
21 |
"""
|
22 |
|
23 |
def __init__(
|
|
|
46 |
|
47 |
def translate(self, text: str, **kwargs) -> str:
|
48 |
"""
|
49 |
+
function to translate a text
|
50 |
@param text: desired text to translate
|
51 |
@return: str: translated text
|
52 |
"""
|
53 |
+
if is_input_valid(text):
|
54 |
text = text.strip()
|
55 |
if self._same_source_target() or is_empty(text):
|
56 |
return text
|
deep_translator/libre.py
CHANGED
@@ -4,7 +4,7 @@ LibreTranslate API
|
|
4 |
|
5 |
import requests
|
6 |
from typing import Optional, List
|
7 |
-
from deep_translator.validate import is_empty,
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.constants import BASE_URLS, LIBRE_LANGUAGES_TO_CODES
|
10 |
from deep_translator.exceptions import (
|
@@ -48,7 +48,7 @@ class LibreTranslator(BaseTranslator):
|
|
48 |
@param text: desired text to translate
|
49 |
@return: str: translated text
|
50 |
"""
|
51 |
-
if
|
52 |
if self._same_source_target() or is_empty(text):
|
53 |
return text
|
54 |
|
|
|
4 |
|
5 |
import requests
|
6 |
from typing import Optional, List
|
7 |
+
from deep_translator.validate import is_empty, is_input_valid
|
8 |
from deep_translator.base import BaseTranslator
|
9 |
from deep_translator.constants import BASE_URLS, LIBRE_LANGUAGES_TO_CODES
|
10 |
from deep_translator.exceptions import (
|
|
|
48 |
@param text: desired text to translate
|
49 |
@return: str: translated text
|
50 |
"""
|
51 |
+
if is_input_valid(text):
|
52 |
if self._same_source_target() or is_empty(text):
|
53 |
return text
|
54 |
|
deep_translator/linguee.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
"""
|
2 |
linguee translator API
|
3 |
"""
|
4 |
-
from deep_translator.validate import
|
5 |
from deep_translator.constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES
|
6 |
from deep_translator.exceptions import (
|
7 |
TranslationNotFound,
|
@@ -54,7 +54,7 @@ class LingueeTranslator(BaseTranslator):
|
|
54 |
if self._same_source_target() or is_empty(word):
|
55 |
return word
|
56 |
|
57 |
-
if
|
58 |
# %s-%s/translation/%s.html
|
59 |
url = (
|
60 |
f"{self._base_url}{self._source}-{self._target}/translation/{word}.html"
|
|
|
1 |
"""
|
2 |
linguee translator API
|
3 |
"""
|
4 |
+
from deep_translator.validate import is_input_valid, is_empty
|
5 |
from deep_translator.constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES
|
6 |
from deep_translator.exceptions import (
|
7 |
TranslationNotFound,
|
|
|
54 |
if self._same_source_target() or is_empty(word):
|
55 |
return word
|
56 |
|
57 |
+
if is_input_valid(word, max_chars=50):
|
58 |
# %s-%s/translation/%s.html
|
59 |
url = (
|
60 |
f"{self._base_url}{self._source}-{self._target}/translation/{word}.html"
|
deep_translator/microsoft.py
CHANGED
@@ -6,7 +6,7 @@ import sys
|
|
6 |
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
|
10 |
from typing import Optional, List
|
11 |
|
12 |
|
@@ -70,7 +70,7 @@ class MicrosoftTranslator(BaseTranslator):
|
|
70 |
# a body must be a list of dicts to process multiple texts;
|
71 |
# I have not added multiple text processing here since it is covered by the translate_batch method
|
72 |
|
73 |
-
if
|
74 |
self._url_params["from"] = self._source
|
75 |
self._url_params["to"] = self._target
|
76 |
|
|
|
6 |
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 is_input_valid
|
10 |
from typing import Optional, List
|
11 |
|
12 |
|
|
|
70 |
# a body must be a list of dicts to process multiple texts;
|
71 |
# I have not added multiple text processing here since it is covered by the translate_batch method
|
72 |
|
73 |
+
if is_input_valid(text):
|
74 |
self._url_params["from"] = self._source
|
75 |
self._url_params["to"] = self._target
|
76 |
|
deep_translator/mymemory.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
"""
|
2 |
mymemory translator API
|
3 |
"""
|
4 |
-
from deep_translator.validate import is_empty,
|
5 |
from deep_translator.constants import BASE_URLS
|
6 |
from deep_translator.exceptions import (
|
7 |
TranslationNotFound,
|
@@ -48,7 +48,7 @@ class MyMemoryTranslator(BaseTranslator):
|
|
48 |
@param return_all: set to True to return all synonym/similars of the translated text
|
49 |
@return: str or list
|
50 |
"""
|
51 |
-
if
|
52 |
text = text.strip()
|
53 |
if self._same_source_target() or is_empty(text):
|
54 |
return text
|
|
|
1 |
"""
|
2 |
mymemory translator API
|
3 |
"""
|
4 |
+
from deep_translator.validate import is_empty, is_input_valid
|
5 |
from deep_translator.constants import BASE_URLS
|
6 |
from deep_translator.exceptions import (
|
7 |
TranslationNotFound,
|
|
|
48 |
@param return_all: set to True to return all synonym/similars of the translated text
|
49 |
@return: str or list
|
50 |
"""
|
51 |
+
if is_input_valid(text, max_chars=500):
|
52 |
text = text.strip()
|
53 |
if self._same_source_target() or is_empty(text):
|
54 |
return text
|
deep_translator/papago.py
CHANGED
@@ -7,7 +7,7 @@ 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
|
11 |
|
12 |
|
13 |
class PapagoTranslator(BaseTranslator):
|
@@ -48,7 +48,7 @@ class PapagoTranslator(BaseTranslator):
|
|
48 |
@param text: desired text to translate
|
49 |
@return: str: translated text
|
50 |
"""
|
51 |
-
if
|
52 |
payload = {"source": self._source, "target": self._target, "text": text}
|
53 |
headers = {
|
54 |
"X-Naver-Client-Id": self.client_id,
|
|
|
7 |
from deep_translator.base import BaseTranslator
|
8 |
import requests
|
9 |
from typing import Optional, List
|
10 |
+
from deep_translator.validate import is_input_valid
|
11 |
|
12 |
|
13 |
class PapagoTranslator(BaseTranslator):
|
|
|
48 |
@param text: desired text to translate
|
49 |
@return: str: translated text
|
50 |
"""
|
51 |
+
if is_input_valid(text):
|
52 |
payload = {"source": self._source, "target": self._target, "text": text}
|
53 |
headers = {
|
54 |
"X-Naver-Client-Id": self.client_id,
|
deep_translator/pons.py
CHANGED
@@ -4,7 +4,7 @@ pons translator API
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
6 |
|
7 |
-
from deep_translator.validate import
|
8 |
from deep_translator.constants import BASE_URLS, PONS_CODES_TO_LANGUAGES
|
9 |
from deep_translator.exceptions import (
|
10 |
TranslationNotFound,
|
@@ -53,7 +53,7 @@ class PonsTranslator(BaseTranslator):
|
|
53 |
@type return_all: bool
|
54 |
@return: str: translated word
|
55 |
"""
|
56 |
-
if
|
57 |
if self._same_source_target() or is_empty(word):
|
58 |
return word
|
59 |
url = f"{self._base_url}{self._source}-{self._target}/{word}"
|
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
6 |
|
7 |
+
from deep_translator.validate import is_input_valid, is_empty
|
8 |
from deep_translator.constants import BASE_URLS, PONS_CODES_TO_LANGUAGES
|
9 |
from deep_translator.exceptions import (
|
10 |
TranslationNotFound,
|
|
|
53 |
@type return_all: bool
|
54 |
@return: str: translated word
|
55 |
"""
|
56 |
+
if is_input_valid(word, max_chars=50):
|
57 |
if self._same_source_target() or is_empty(word):
|
58 |
return word
|
59 |
url = f"{self._base_url}{self._source}-{self._target}/{word}"
|
deep_translator/validate.py
CHANGED
@@ -5,7 +5,7 @@ def is_empty(text: str) -> bool:
|
|
5 |
return text == ""
|
6 |
|
7 |
|
8 |
-
def
|
9 |
"""
|
10 |
validate the target text to translate
|
11 |
@param min_chars: min characters
|
|
|
5 |
return text == ""
|
6 |
|
7 |
|
8 |
+
def is_input_valid(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
@@ -10,7 +10,7 @@ from deep_translator.exceptions import (
|
|
10 |
TooManyRequests,
|
11 |
)
|
12 |
from deep_translator.base import BaseTranslator
|
13 |
-
from deep_translator.validate import
|
14 |
from typing import Optional, List
|
15 |
|
16 |
|
@@ -93,7 +93,7 @@ class YandexTranslator(BaseTranslator):
|
|
93 |
return language
|
94 |
|
95 |
def translate(self, text: str, proxies: Optional[dict] = None, **kwargs) -> str:
|
96 |
-
if
|
97 |
params = {
|
98 |
"text": text,
|
99 |
"format": "plain",
|
|
|
10 |
TooManyRequests,
|
11 |
)
|
12 |
from deep_translator.base import BaseTranslator
|
13 |
+
from deep_translator.validate import is_input_valid
|
14 |
from typing import Optional, List
|
15 |
|
16 |
|
|
|
93 |
return language
|
94 |
|
95 |
def translate(self, text: str, proxies: Optional[dict] = None, **kwargs) -> str:
|
96 |
+
if is_input_valid(text):
|
97 |
params = {
|
98 |
"text": text,
|
99 |
"format": "plain",
|
tests/test_deepl.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import pytest
|
2 |
-
from unittest.mock import Mock,
|
3 |
from deep_translator.deepl import DeeplTranslator
|
4 |
from deep_translator.exceptions import AuthorizationException
|
5 |
|
|
|
1 |
import pytest
|
2 |
+
from unittest.mock import Mock, patch
|
3 |
from deep_translator.deepl import DeeplTranslator
|
4 |
from deep_translator.exceptions import AuthorizationException
|
5 |
|
tests/test_google.py
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
|
5 |
import pytest
|
6 |
from deep_translator import exceptions, GoogleTranslator
|
7 |
-
from deep_translator.constants import
|
8 |
|
9 |
|
10 |
@pytest.fixture
|
@@ -21,7 +21,7 @@ def test_content(google_translator):
|
|
21 |
|
22 |
|
23 |
def test_abbreviations_and_languages_mapping():
|
24 |
-
for abb, lang in
|
25 |
g1 = GoogleTranslator(abb)
|
26 |
g2 = GoogleTranslator(lang)
|
27 |
assert g1._source == g2._source
|
|
|
4 |
|
5 |
import pytest
|
6 |
from deep_translator import exceptions, GoogleTranslator
|
7 |
+
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
|
8 |
|
9 |
|
10 |
@pytest.fixture
|
|
|
21 |
|
22 |
|
23 |
def test_abbreviations_and_languages_mapping():
|
24 |
+
for abb, lang in GOOGLE_LANGUAGES_TO_CODES.items():
|
25 |
g1 = GoogleTranslator(abb)
|
26 |
g2 = GoogleTranslator(lang)
|
27 |
assert g1._source == g2._source
|
tests/test_libre.py
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
|
5 |
import pytest
|
6 |
from deep_translator import exceptions, LibreTranslator
|
7 |
-
from deep_translator.constants import
|
8 |
|
9 |
|
10 |
@pytest.fixture
|
@@ -24,9 +24,9 @@ def test_inputs():
|
|
24 |
|
25 |
|
26 |
def test_abbreviations_and_languages_mapping():
|
27 |
-
for abb, lang in
|
28 |
-
l1 = LibreTranslator(abb, api_key="some_key")
|
29 |
-
l2 = LibreTranslator(lang, api_key="some_key")
|
30 |
assert l1._source == l2._source
|
31 |
|
32 |
|
|
|
4 |
|
5 |
import pytest
|
6 |
from deep_translator import exceptions, LibreTranslator
|
7 |
+
from deep_translator.constants import LIBRE_LANGUAGES_TO_CODES
|
8 |
|
9 |
|
10 |
@pytest.fixture
|
|
|
24 |
|
25 |
|
26 |
def test_abbreviations_and_languages_mapping():
|
27 |
+
for abb, lang in LIBRE_LANGUAGES_TO_CODES.items():
|
28 |
+
l1 = LibreTranslator(source=abb, api_key="some_key")
|
29 |
+
l2 = LibreTranslator(source=lang, api_key="some_key")
|
30 |
assert l1._source == l2._source
|
31 |
|
32 |
|