def bdtr_list(seq, from_lang='en', to_lang='zh'): """Translate (bdtr) a list of text. :in: list of text :out: list of translated text """ # record the # of func calls if not isinstance(seq, list): LOGGER.error("Input not a list, return the original") return seq try: bdtr_list.counter += 1 except Exception as exc: bdtr_list.counter = 1 del exc # len0 = len(seq) # introduce extra delay 0.5 + .5 after 300 calls seqout = [] for elm in tqdm(seq, desc='converting...', unit='pars'): seqout += [bdtr(elm, from_lang=from_lang, to_lang=to_lang)] if bdtr_list.counter > 300: sleep(0.25 + random()) bdtr_list.counter += 1 return seqout
def test_en2de_long_random(): ''' test text longer than 30 ''' rint = str(randint(1, 10000)) text = 'A second Canadian plane carrying 185 passengers from Wuhan arrived in Vancouver just before 1 a.m. Eastern Time, according to CTV. ' res = bdtr(text + rint, 'en', 'de') assert rint in res
def test_long_test(): ''' test text longer than 30 ''' text = 'A second Canadian plane carrying 185 passengers from Wuhan arrived in Vancouver just before 1 a.m. Eastern Time, according to CTV. ' res = bdtr(text) # 据中央电视台报道,第二架载有185名武汉乘客的加拿大飞机于东部时间凌晨1点前抵达温哥华。 assert len(res) > 35 assert res, 'To see res'
def test_def(): ''' test1 ''' text = '为乐为魂之语与通〜' from_lang = 'wyw' to_lang = 'en' assert bdtr( text, from_lang, to_lang ) == 'For music is the language and communication of soul ~' # NOQA
def test_pressure(): '''pressure_test''' from time import perf_counter from tqdm import trange for _ in trange(10): tick = perf_counter() res = bdtr('test ' + str(randint(1, maxsize))) print(res, f'time: {(perf_counter() - tick):.2f} s') assert res, 'probably need to increase the value in SESS.hooks = {\'response\': make_throttle_hook(0.6)}'
def test_to_chinese(): ''' test test_to_chinese random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint, to_lang='chinese')
def test_from_zhcn(): ''' test test_from_zhcn random ''' rint = str(randint(1, 10000)) assert rint in bdtr('测试 ' + rint, 'zh-cn', 'en')
def test_to_zhcn(): ''' test test_to_zhcn random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint, to_lang='zh-cn')
def test_from_english_cache_disabled(): ''' test test_from_english random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint, 'english', 'zh', cache=0)
def test_to_english(): ''' test test_to_english random ''' rint = str(randint(1, 10000)) assert rint in bdtr('测试 ' + rint, 'zh', 'english')
def test_from_chinese(): ''' test test_from_chinese random ''' rint = str(randint(1, 10000)) assert rint in bdtr('测试 ' + rint, 'chinese', 'en')
def test_en2zh_def_random(): ''' test en2zh random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint)
def test_en2fr_random(): ''' test en2fr random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint, to_lang='fr')
def test_en2de_random(): ''' test en2de random ''' rint = str(randint(1, 10000)) assert rint in bdtr('test ' + rint, to_lang='de')
def test_empty(): ''' test empty ''' text = ' ' from_lang = 'wyw' to_lang = 'en' assert bdtr(text, from_lang, to_lang) == ''