|
import ctranslate2
|
|
from split import split_string
|
|
|
|
translator_zh2en = ctranslate2.Translator("zh-en_model/", device="cpu")
|
|
translator2_zh2en = ctranslate2.Translator("zh2en_cmodel/", device="cpu")
|
|
translator_en2zh = ctranslate2.Translator("en-zh_model/", device="cpu")
|
|
translator2_en2zh = ctranslate2.Translator("en2zh_cmodel", device="cpu")
|
|
|
|
def translate(input_tokens, input_tokens2, mode):
|
|
|
|
source = split_string(input_tokens)
|
|
lenth = len(source)
|
|
|
|
source2 = split_string(input_tokens2)
|
|
lenth2 = len(source2)
|
|
|
|
if mode == "汉译英" :
|
|
results = translator_zh2en.translate_batch(source)
|
|
results2 = translator2_zh2en.translate_batch(source2)
|
|
else :
|
|
results = translator_en2zh.translate_batch(source)
|
|
results2 = translator2_en2zh.translate_batch(source2)
|
|
|
|
target = []
|
|
target2 = []
|
|
|
|
for i in range(0, lenth, 1):
|
|
target = target + results[i].hypotheses[0]
|
|
for i in range(0, lenth2, 1):
|
|
target2 = target2 + results2[i].hypotheses[0]
|
|
|
|
|
|
|
|
|
|
return target,target2 |