1、网页版:https://translate.google.cn/?hl=zh-CN&tab=TT
2、自己封装API,参考https://ctrlq.org/code/19909-google-translate-api:
import requests
from urllib import parse
def trans(sourceText, sourceLang, targetLang):
url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="+ sourceLang + "&tl=" + targetLang + "&dt=t&q=" + parse.quote(sourceText)
response = requests.get(url)
trans_text = eval(response.text.replace("null", "\'null\'"))
# print(trans_text[0][0][0])
return trans_text[0][0][0]
def back_trans(sourceText, sourceLang, midLang="en"):
transText = trans(sourceText, sourceLang, midLang)
backTransText = trans(transText, midLang, sourceLang)
return backTransText
# sourceText= "Under what circumstances are civil applications not accepted?"
# sourceLang="en"
# targetLang = "zh-cn"
# response=trans(sourceText, sourceLang, targetLang)
# trans_text = eval(response.text.replace("null", "\'null\'"))
# print(trans_text[0][0][0])
# sourceText = "哪些情形下,不予受理民事诉讼申请?"
# sourceText="民事诉讼中对哪些情形的起诉法院不予受理"
sourceText="我被人家车撞了,但是他的车也是借的别人的,那我是不是可以去找原车主索要赔偿呢"
sourceLang="zh-cn"
ret = back_trans(sourceText, sourceLang)
print(ret)
3、旧版的 googletrans
from googletrans import Translator
translator = Translator()
# cn=u"哪些情形下,不予受理民事诉讼申请?"
# cn="民事诉讼中对哪些情形的起诉法院不予受理"
# cn="我被车撞了,肇事车是借的,我能找车主索赔吗?"
cn="我被人家车撞了,但是他的车也是借的别人的,那我是不是可以去找原车主索要赔偿呢"
en = translator.translate(cn, src='zh-cn', dest='en').text
print(en)
cn = translator.translate(en, src='en', dest='zh-cn').text
print(cn)
经过回译发现两个版本(1,2是同一个版本,3是旧版本)的API效果都不是很好。
4、用tensor2tensor自己训练模型