def translate(self, word, return_all=False, **kwargs): if self._validate_payload(word): url = "{}{}-{}/{}".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') elements = soup.findAll(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(word) filtered_elements = [] for el in elements: temp = '' for e in el.findAll('a'): if e.parent.name == 'div': if e and "/translate/{}-{}/".format( self._target, self._source) in e.get('href'): temp += e.get_text() + ' ' filtered_elements.append(temp) if not filtered_elements: raise ElementNotFoundInGetRequest(word) word_list = [ word for word in filtered_elements if word and len(word) > 1 ] if not word_list: raise TranslationNotFound(word) return word_list if return_all else word_list[0]
def translate(self, word, return_all=False, **kwargs): """ function that uses PONS 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 """ if self._validate_payload(word, max_chars=50): url = "{}{}-{}/{}".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') elements = soup.findAll(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(word) filtered_elements = [] for el in elements: temp = '' for e in el.findAll('a'): if e.parent.name == 'div': if e and "/translate/{}-{}/".format( self._target, self._source) in e.get('href'): temp += e.get_text() + ' ' filtered_elements.append(temp) if not filtered_elements: raise ElementNotFoundInGetRequest(word) word_list = [ word for word in filtered_elements if word and len(word) > 1 ] if not word_list: raise TranslationNotFound(word) return word_list if return_all else word_list[0]
def translate(self, word, **kwargs): if self._validate_payload(word): url = "{}{}-{}/{}".format(self.__base_url, self._source, self._target, quote(word)) response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') elements = soup.findAll(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(elements) eof = [] for el in elements: temp = '' for e in el.findAll('a'): if e.parent.name == 'div': if e and "/translate/{}-{}/".format( self._target, self._source) in e.get('href'): temp += e.get_text() + ' ' if not kwargs.get('return_all'): return temp eof.append(temp) if 'return_all' in kwargs and kwargs.get('return_all'): return [word for word in eof if word and len(word) > 1]
def translate(self, word, return_all=False, **kwargs): if self._validate_payload(word): # %s-%s/translation/%s.html url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') elements = soup.find_all(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(elements) filtered_elements = [] for el in elements: try: pronoun = el.find('span', { 'class': 'placeholder' }).get_text(strip=True) except AttributeError: pronoun = '' filtered_elements.append( el.get_text(strip=True).replace(pronoun, '')) if not filtered_elements: raise TranslationNotFound(word) return filtered_elements if return_all else filtered_elements[0]
def translate(self, word, **kwargs): if self._validate_payload(word): # %s-%s/translation/%s.html url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') elements = soup.find_all(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(elements) if 'return_all' in kwargs and kwargs.get('return_all'): return [el.get_text(strip=True) for el in elements] else: return elements[0].get_text(strip=True)
def translate(self, word, return_all=False, **kwargs): """ 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 """ if self._validate_payload(word, max_chars=50): # %s-%s/translation/%s.html url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') elements = soup.find_all(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(elements) filtered_elements = [] for el in elements: try: pronoun = el.find('span', { 'class': 'placeholder' }).get_text(strip=True) except AttributeError: pronoun = '' filtered_elements.append( el.get_text(strip=True).replace(pronoun, '')) if not filtered_elements: raise TranslationNotFound(word) return filtered_elements if return_all else filtered_elements[0]
def translate(self, payload, **kwargs): """ main function that uses google translate to translate a text @param payload: desired text to translate @return: str: translated text """ if self._validate_payload(payload): payload = payload.strip() if self.payload_key: self._url_params[self.payload_key] = payload response = requests.get(self.__base_url, params=self._url_params) soup = BeautifulSoup(response.text, 'html.parser') element = soup.find(self._element_tag, self._element_query) if not element: raise ElementNotFoundInGetRequest(element) return element.get_text(strip=True)