def translate_text(self, text): if self.translator == "google": return ts.google(text, to_language=self.to_language) elif self.translator == "bing": return ts.bing(text, to_language=self.to_language) elif self.translator == "google_separate": return self.google_separate.translate(text=text, dest=self.to_language).text elif self.translator == "microsoft": params = {'appid': 'Bearer ' + self.token, 'text': text, 'to': self.to_language} requestHeader = {'Accept': 'application/xml'} responseResult = requests.get(self.translateUrl, params=params, headers=requestHeader) soup = BeautifulSoup(responseResult.text, "lxml") return colored(soup.get_text()) elif self.translator == "sogou": return ts.sogou(text, to_language=self.to_language) elif self.translator == "baidu": return ts.baidu(text, to_language=self.to_language) elif self.translator == "alibaba": return ts.alibaba(text, to_language=self.to_language) elif self.translator == "tencent": return ts.tencent(text, to_language=self.to_language) elif self.translator == "youdao": return ts.youdao(text, to_language=self.to_language) elif self.translator == "deepl": return ts.deepl(text, to_language=self.to_language)
def youdao_block(eng_qns): # translate the English question to Chinese using Youdao eng_chi_youdao = ts.youdao(eng_qns, 'en', 'zh-CN') # the list of questions translated from Chinese to English the_list = [] # Google chi_eng_google = ts.google(eng_chi_youdao, 'zh', 'en') the_list.append(chi_eng_google) # Bing translator = Translator(from_lang="zh", to_lang="en") chi_eng_bing = translator.translate(eng_chi_youdao) the_list.append(chi_eng_bing) # Youdao chi_eng_youdao = ts.youdao(eng_chi_youdao, 'zh', 'en') the_list.append(chi_eng_youdao) # Tencent chi_eng_tencent = ts.tencent(eng_chi_youdao, 'zh', 'en') the_list.append(chi_eng_tencent) return the_list