2021-02-02 21:05:09 +00:00
|
|
|
"""
|
|
|
|
linguee translator API
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
from typing import List, Optional, Union
|
2021-02-02 21:05:09 +00:00
|
|
|
|
|
|
|
import requests
|
2022-05-01 12:00:20 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2021-02-02 21:05:09 +00:00
|
|
|
from requests.utils import requote_uri
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from deep_translator.base import BaseTranslator
|
|
|
|
from deep_translator.constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES
|
|
|
|
from deep_translator.exceptions import (
|
|
|
|
ElementNotFoundInGetRequest,
|
|
|
|
NotValidPayload,
|
|
|
|
RequestError,
|
|
|
|
TooManyRequests,
|
|
|
|
TranslationNotFound,
|
|
|
|
)
|
|
|
|
from deep_translator.validate import is_empty, is_input_valid
|
|
|
|
|
2021-02-02 21:05:09 +00:00
|
|
|
|
|
|
|
class LingueeTranslator(BaseTranslator):
|
|
|
|
"""
|
|
|
|
class that wraps functions, which use the linguee translator under the hood to translate word(s)
|
|
|
|
"""
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def __init__(
|
2022-11-07 18:06:49 +00:00
|
|
|
self, source: str = "en", target: str = "de", proxies: Optional[dict] = None, **kwargs
|
2022-05-01 12:00:20 +00:00
|
|
|
):
|
2021-02-02 21:05:09 +00:00
|
|
|
"""
|
|
|
|
@param source: source language to translate from
|
|
|
|
@param target: target language to translate to
|
|
|
|
"""
|
2021-11-29 19:28:30 +00:00
|
|
|
self.proxies = proxies
|
2022-05-01 12:00:20 +00:00
|
|
|
super().__init__(
|
|
|
|
base_url=BASE_URLS.get("LINGUEE"),
|
|
|
|
source=source,
|
|
|
|
target=target,
|
|
|
|
languages=LINGUEE_LANGUAGES_TO_CODES,
|
|
|
|
element_tag="a",
|
|
|
|
element_query={"class": "dictLink featured"},
|
|
|
|
payload_key=None, # key of text in the url
|
|
|
|
)
|
|
|
|
|
|
|
|
def translate(
|
|
|
|
self, word: str, return_all: bool = False, **kwargs
|
|
|
|
) -> Union[str, List[str]]:
|
2021-02-02 21:05:09 +00:00
|
|
|
"""
|
|
|
|
function that uses linguee to translate a word
|
|
|
|
@param word: word to translate
|
|
|
|
@type word: str
|
|
|
|
@param return_all: set to True to return all synonym of the translated word
|
|
|
|
@type return_all: bool
|
|
|
|
@return: str: translated word
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
if self._same_source_target() or is_empty(word):
|
|
|
|
return word
|
|
|
|
|
|
|
|
if is_input_valid(word, max_chars=50):
|
2021-02-02 21:05:09 +00:00
|
|
|
# %s-%s/translation/%s.html
|
2022-05-01 12:00:20 +00:00
|
|
|
url = (
|
|
|
|
f"{self._base_url}{self._source}-{self._target}/translation/{word}.html"
|
|
|
|
)
|
2021-02-02 21:05:09 +00:00
|
|
|
url = requote_uri(url)
|
2021-11-29 19:28:30 +00:00
|
|
|
response = requests.get(url, proxies=self.proxies)
|
2021-02-02 21:05:09 +00:00
|
|
|
|
|
|
|
if response.status_code == 429:
|
|
|
|
raise TooManyRequests()
|
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
raise RequestError()
|
2022-05-01 12:00:20 +00:00
|
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
2021-02-02 21:05:09 +00:00
|
|
|
elements = soup.find_all(self._element_tag, self._element_query)
|
2022-11-07 18:06:49 +00:00
|
|
|
response.close()
|
|
|
|
|
2021-02-02 21:05:09 +00:00
|
|
|
if not elements:
|
|
|
|
raise ElementNotFoundInGetRequest(elements)
|
|
|
|
|
|
|
|
filtered_elements = []
|
|
|
|
for el in elements:
|
|
|
|
try:
|
2022-05-01 12:00:20 +00:00
|
|
|
pronoun = el.find("span", {"class": "placeholder"}).get_text(
|
|
|
|
strip=True
|
|
|
|
)
|
2021-02-02 21:05:09 +00:00
|
|
|
except AttributeError:
|
2022-05-01 12:00:20 +00:00
|
|
|
pronoun = ""
|
|
|
|
filtered_elements.append(el.get_text(strip=True).replace(pronoun, ""))
|
2021-02-02 21:05:09 +00:00
|
|
|
|
|
|
|
if not filtered_elements:
|
|
|
|
raise TranslationNotFound(word)
|
|
|
|
|
|
|
|
return filtered_elements if return_all else filtered_elements[0]
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate_words(self, words: List[str], **kwargs) -> List[str]:
|
2021-02-02 21:05:09 +00:00
|
|
|
"""
|
|
|
|
translate a batch of words together by providing them in a list
|
|
|
|
@param words: list of words you want to translate
|
|
|
|
@param kwargs: additional args
|
|
|
|
@return: list of translated words
|
|
|
|
"""
|
|
|
|
if not words:
|
|
|
|
raise NotValidPayload(words)
|
|
|
|
|
|
|
|
translated_words = []
|
|
|
|
for word in words:
|
2021-11-29 19:28:30 +00:00
|
|
|
translated_words.append(self.translate(word=word, **kwargs))
|
2021-02-02 21:05:09 +00:00
|
|
|
return translated_words
|