=
commited on
Commit
·
82f3d24
1
Parent(s):
b95447d
catched not found translation in pons
Browse files- deep_translator/exceptions.py +7 -0
- deep_translator/google_trans.py +4 -3
- deep_translator/linguee.py +5 -2
- deep_translator/pons.py +12 -5
- examples/mymemory.py +7 -0
- examples/pons.py +1 -1
deep_translator/exceptions.py
CHANGED
@@ -28,6 +28,13 @@ class TranslationNotFound(BaseError):
|
|
28 |
super(TranslationNotFound, self).__init__(val, message)
|
29 |
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
class NotValidLength(BaseError):
|
32 |
def __init__(self, val, message="Length of text need to be between 0 and 5000"):
|
33 |
super(NotValidLength, self).__init__(val, message)
|
|
|
28 |
super(TranslationNotFound, self).__init__(val, message)
|
29 |
|
30 |
|
31 |
+
class ElementNotFoundInGetRequest(BaseError):
|
32 |
+
def __init__(self,
|
33 |
+
val,
|
34 |
+
message='Required element was not found in the API response'):
|
35 |
+
super(ElementNotFoundInGetRequest, self).__init__(val, message)
|
36 |
+
|
37 |
+
|
38 |
class NotValidLength(BaseError):
|
39 |
def __init__(self, val, message="Length of text need to be between 0 and 5000"):
|
40 |
super(NotValidLength, self).__init__(val, message)
|
deep_translator/google_trans.py
CHANGED
@@ -36,7 +36,7 @@ class GoogleTranslator(BaseTranslator):
|
|
36 |
def get_supported_languages(as_dict=False):
|
37 |
return GoogleTranslator.supported_languages if not as_dict else GoogleTranslator._languages
|
38 |
|
39 |
-
def _map_language_to_code(self, *languages
|
40 |
"""
|
41 |
|
42 |
@param language: type of language
|
@@ -50,7 +50,7 @@ class GoogleTranslator(BaseTranslator):
|
|
50 |
else:
|
51 |
raise LanguageNotSupportedException(language)
|
52 |
|
53 |
-
def is_language_supported(self, *languages
|
54 |
for lang in languages:
|
55 |
if lang != 'auto' and lang not in GOOGLE_LANGUAGES_TO_CODES.keys():
|
56 |
if lang != 'auto' and lang not in GOOGLE_LANGUAGES_TO_CODES.values():
|
@@ -76,7 +76,8 @@ class GoogleTranslator(BaseTranslator):
|
|
76 |
soup = BeautifulSoup(response.text, 'html.parser')
|
77 |
element = soup.find(self._element_tag, self._element_query)
|
78 |
if not element:
|
79 |
-
raise
|
|
|
80 |
|
81 |
return element.get_text(strip=True)
|
82 |
|
|
|
36 |
def get_supported_languages(as_dict=False):
|
37 |
return GoogleTranslator.supported_languages if not as_dict else GoogleTranslator._languages
|
38 |
|
39 |
+
def _map_language_to_code(self, *languages):
|
40 |
"""
|
41 |
|
42 |
@param language: type of language
|
|
|
50 |
else:
|
51 |
raise LanguageNotSupportedException(language)
|
52 |
|
53 |
+
def is_language_supported(self, *languages):
|
54 |
for lang in languages:
|
55 |
if lang != 'auto' and lang not in GOOGLE_LANGUAGES_TO_CODES.keys():
|
56 |
if lang != 'auto' and lang not in GOOGLE_LANGUAGES_TO_CODES.values():
|
|
|
76 |
soup = BeautifulSoup(response.text, 'html.parser')
|
77 |
element = soup.find(self._element_tag, self._element_query)
|
78 |
if not element:
|
79 |
+
# raise ElementNotFoundInGetRequest(element)
|
80 |
+
raise TranslationNotFound(text)
|
81 |
|
82 |
return element.get_text(strip=True)
|
83 |
|
deep_translator/linguee.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from deep_translator.constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES, LINGUEE_CODE_TO_LANGUAGE
|
2 |
-
from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload,
|
3 |
from deep_translator.parent import BaseTranslator
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
@@ -62,7 +62,7 @@ class LingueeTranslator(BaseTranslator):
|
|
62 |
soup = BeautifulSoup(response.text, 'html.parser')
|
63 |
elements = soup.find_all(self._element_tag, self._element_query)
|
64 |
if not elements:
|
65 |
-
raise
|
66 |
|
67 |
filtered_elements = []
|
68 |
for el in elements:
|
@@ -72,6 +72,9 @@ class LingueeTranslator(BaseTranslator):
|
|
72 |
pronoun = ''
|
73 |
filtered_elements.append(el.get_text(strip=True).replace(pronoun, ''))
|
74 |
|
|
|
|
|
|
|
75 |
return filtered_elements if return_all else filtered_elements[0]
|
76 |
|
77 |
def translate_words(self, words, **kwargs):
|
|
|
1 |
from deep_translator.constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES, LINGUEE_CODE_TO_LANGUAGE
|
2 |
+
from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload, ElementNotFoundInGetRequest
|
3 |
from deep_translator.parent import BaseTranslator
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
|
|
62 |
soup = BeautifulSoup(response.text, 'html.parser')
|
63 |
elements = soup.find_all(self._element_tag, self._element_query)
|
64 |
if not elements:
|
65 |
+
raise ElementNotFoundInGetRequest(elements)
|
66 |
|
67 |
filtered_elements = []
|
68 |
for el in elements:
|
|
|
72 |
pronoun = ''
|
73 |
filtered_elements.append(el.get_text(strip=True).replace(pronoun, ''))
|
74 |
|
75 |
+
if not filtered_elements:
|
76 |
+
raise TranslationNotFound(word)
|
77 |
+
|
78 |
return filtered_elements if return_all else filtered_elements[0]
|
79 |
|
80 |
def translate_words(self, words, **kwargs):
|
deep_translator/pons.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
from deep_translator.constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
5 |
-
from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload
|
6 |
from deep_translator.parent import BaseTranslator
|
7 |
from requests.utils import requote_uri
|
8 |
|
@@ -66,18 +66,25 @@ class PonsTranslator(BaseTranslator):
|
|
66 |
soup = BeautifulSoup(response.text, 'html.parser')
|
67 |
elements = soup.findAll(self._element_tag, self._element_query)
|
68 |
if not elements:
|
69 |
-
raise
|
70 |
|
71 |
-
|
72 |
for el in elements:
|
73 |
temp = ''
|
74 |
for e in el.findAll('a'):
|
75 |
if e.parent.name == 'div':
|
76 |
if e and "/translate/{}-{}/".format(self._target, self._source) in e.get('href'):
|
77 |
temp += e.get_text() + ' '
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
word_list = [word for word in eof if word and len(word) > 1]
|
81 |
return word_list if return_all else word_list[0]
|
82 |
|
83 |
def translate_words(self, words, **kwargs):
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
from deep_translator.constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
5 |
+
from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload, ElementNotFoundInGetRequest
|
6 |
from deep_translator.parent import BaseTranslator
|
7 |
from requests.utils import requote_uri
|
8 |
|
|
|
66 |
soup = BeautifulSoup(response.text, 'html.parser')
|
67 |
elements = soup.findAll(self._element_tag, self._element_query)
|
68 |
if not elements:
|
69 |
+
raise ElementNotFoundInGetRequest(word)
|
70 |
|
71 |
+
filtered_elements = []
|
72 |
for el in elements:
|
73 |
temp = ''
|
74 |
for e in el.findAll('a'):
|
75 |
if e.parent.name == 'div':
|
76 |
if e and "/translate/{}-{}/".format(self._target, self._source) in e.get('href'):
|
77 |
temp += e.get_text() + ' '
|
78 |
+
filtered_elements.append(temp)
|
79 |
+
|
80 |
+
if not filtered_elements:
|
81 |
+
raise ElementNotFoundInGetRequest(word)
|
82 |
+
|
83 |
+
word_list = [word for word in filtered_elements if word and len(word) > 1]
|
84 |
+
|
85 |
+
if not word_list:
|
86 |
+
raise TranslationNotFound(word)
|
87 |
|
|
|
88 |
return word_list if return_all else word_list[0]
|
89 |
|
90 |
def translate_words(self, words, **kwargs):
|
examples/mymemory.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from deep_translator import MyMemoryTranslator
|
3 |
+
|
4 |
+
|
5 |
+
res = MyMemoryTranslator(source='ar', target='en').translate('آخُذ اَلْباص.')
|
6 |
+
|
7 |
+
print(res)
|
examples/pons.py
CHANGED
@@ -5,6 +5,6 @@ from deep_translator import PonsTranslator
|
|
5 |
res = PonsTranslator(source='de', target='en').translate('übersetzen', return_all=False)
|
6 |
|
7 |
|
8 |
-
e2a = PonsTranslator('ar', 'en').translate('آخُذ اَلْباص.')
|
9 |
|
10 |
print(e2a)
|
|
|
5 |
res = PonsTranslator(source='de', target='en').translate('übersetzen', return_all=False)
|
6 |
|
7 |
|
8 |
+
e2a = PonsTranslator('ar', 'en').translate('آخُذ اَلْباص.', return_all=True)
|
9 |
|
10 |
print(e2a)
|