Merge pull request #12 from nidhaloff/dev
Browse files- deep_translator/exceptions.py +8 -1
- deep_translator/google_trans.py +5 -4
- deep_translator/linguee.py +4 -1
- deep_translator/mymemory.py +2 -2
- deep_translator/pons.py +12 -5
- examples/mymemory.py +7 -0
- examples/pons.py +4 -1
deep_translator/exceptions.py
CHANGED
|
@@ -21,10 +21,17 @@ class NotValidPayload(BaseError):
|
|
| 21 |
super(NotValidPayload, self).__init__(val, message)
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
class ElementNotFoundInGetRequest(BaseError):
|
| 25 |
def __init__(self,
|
| 26 |
val,
|
| 27 |
-
message='
|
| 28 |
super(ElementNotFoundInGetRequest, self).__init__(val, message)
|
| 29 |
|
| 30 |
|
|
|
|
| 21 |
super(NotValidPayload, self).__init__(val, message)
|
| 22 |
|
| 23 |
|
| 24 |
+
class TranslationNotFound(BaseError):
|
| 25 |
+
def __init__(self,
|
| 26 |
+
val,
|
| 27 |
+
message='No translation was found using the current translator. Try another translator?'):
|
| 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 |
|
deep_translator/google_trans.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
|
| 2 |
from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 3 |
-
from deep_translator.exceptions import LanguageNotSupportedException,
|
| 4 |
from deep_translator.parent import BaseTranslator
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import requests
|
|
@@ -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 ElementNotFoundInGetRequest(element)
|
|
|
|
| 80 |
|
| 81 |
return element.get_text(strip=True)
|
| 82 |
|
|
|
|
| 1 |
|
| 2 |
from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 3 |
+
from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload
|
| 4 |
from deep_translator.parent import BaseTranslator
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import requests
|
|
|
|
| 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,
|
| 3 |
from deep_translator.parent import BaseTranslator
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import requests
|
|
@@ -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
|
|
|
|
| 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/mymemory.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
|
| 2 |
from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 3 |
-
from deep_translator.exceptions import NotValidPayload
|
| 4 |
from deep_translator.parent import BaseTranslator
|
| 5 |
import requests
|
| 6 |
|
|
@@ -52,7 +52,7 @@ class MyMemoryTranslator(BaseTranslator):
|
|
| 52 |
headers=self.headers)
|
| 53 |
data = response.json()
|
| 54 |
if not data:
|
| 55 |
-
|
| 56 |
|
| 57 |
translation = data.get('responseData').get('translatedText')
|
| 58 |
if translation:
|
|
|
|
| 1 |
|
| 2 |
from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
| 3 |
+
from deep_translator.exceptions import NotValidPayload, TranslationNotFound
|
| 4 |
from deep_translator.parent import BaseTranslator
|
| 5 |
import requests
|
| 6 |
|
|
|
|
| 52 |
headers=self.headers)
|
| 53 |
data = response.json()
|
| 54 |
if not data:
|
| 55 |
+
TranslationNotFound(text)
|
| 56 |
|
| 57 |
translation = data.get('responseData').get('translatedText')
|
| 58 |
if translation:
|
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,
|
| 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 ElementNotFoundInGetRequest(
|
| 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
|
@@ -4,4 +4,7 @@ from deep_translator import PonsTranslator
|
|
| 4 |
|
| 5 |
res = PonsTranslator(source='de', target='en').translate('übersetzen', return_all=False)
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 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)
|