1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Python3 调用谷歌翻译 英汉互译

Python3 调用谷歌翻译 英汉互译

时间:2019-01-04 16:33:32

相关推荐

Python3 调用谷歌翻译 英汉互译

Python3 调用谷歌翻译 将英文翻译成中文

代码如下:

import urllib.requestimport execjsclass Py4Js():def __init__(self):self.ctx = pile(""" function TL(a) { var k = ""; var b = 406644; var b1 = 3293161072; var jd = "."; var $b = "+-a^+6"; var Zb = "+-3^+b+-f"; for (var e = [], f = 0, g = 0; g < a.length; g++) { var m = a.charCodeAt(g); 128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128) } a = b; for (f = 0; f < e.length; f++) a += e[f], a = RL(a, $b); a = RL(a, Zb); a ^= b1 || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + jd + (a ^ b) }; function RL(a, b) { var t = "a"; var Yb = "+"; for (var c = 0; c < b.length - 2; c += 3) { var d = b.charAt(c + 2), d = d >= t ? d.charCodeAt(0) - 87 : Number(d), d = b.charAt(c + 1) == Yb ? a >>> d: a << d; a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d } return a } """)def getTk(self, text):return self.ctx.call("TL", text)def open_url(url):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/0101 Firefox/23.0'}req = urllib.request.Request(url=url, headers=headers)response = urllib.request.urlopen(req)data = response.read().decode('utf-8')return datadef translate(content, tk):if len(content) > 4891:print("翻译的长度超过限制!!!")returncontent = urllib.parse.quote(content)url = "/translate_a/single?client=t" + "&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" + "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" + "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (tk, content)# 返回值是一个多层嵌套列表的字符串形式,解析起来还相当费劲,写了几个正则,发现也很不理想,# 后来感觉,使用正则简直就是把简单的事情复杂化,这里直接切片就Ok了result = open_url(url)end = result.find("\",")if end > 4:print(result[4:end])def main():js = Py4Js()while 1:content = input("输入待翻译内容:")if content == 'q!':breaktk = js.getTk(content)translate(content, tk)if __name__ == "__main__":main()

运行结果:

C:\Users\DELL\Anaconda3\python.exe D:/Pycharm/NLPCC/GoogleTranslate.py输入待翻译内容:hello你好输入待翻译内容:I'll walk through fire我会穿越火海输入待翻译内容:I'm not an angel,I'm not a saint我不是天使,我不是圣人# 上面的短句翻译的还行,但是翻译长句时,翻译出来的句子并不通顺输入待翻译内容:You get the best out of others when you get the best out of yourself你得到最好的别人,当你得到最好的自己输入待翻译内容:It is during our darkest moments that we must focus to see the light这是在我们最黑暗的时刻,我们必须关注到见光# 以下是更长的句子,可以发现,当翻译的英文句子中含有句号,就会被隔断。句号后面的句子不被翻译。输入待翻译内容:You see, in life, lots of people know what to do, but few people actually do what they know. Knowing is not enough! You must take action.你看,在生活中,很多人都知道该怎么做,但很少有人真正做到他们所知道的。输入待翻译内容:The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with.第一步是认识一个。输入待翻译内容:

上面代码是实现了英文翻译成中文,要将中文翻译成英文,只需要将url中sl=en&tl=zh-CN交换一下,变成sl=zh-CN&tl=en即可。

url = "/translate_a/single?client=t" + "&sl=zh-CN&tl=en&hl=&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" + "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" + "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (tk, content)

下面是运行结果。与英译中存在同样的问题。

C:\Users\DELL\Anaconda3\python.exe D:/Pycharm/NLPCC/GoogleTranslate.py输入待翻译内容:你好Hello there输入待翻译内容:很高兴见到你Nice to meet you# 翻译中文长句 遇到句号就不翻了输入待翻译内容:阳光,不只来自太阳,也来自我们的心。心里有阳光,能看到到世界美好的一面;心里有阳光,能与有缘的人心心相映。Sunshine, not only from the sun, but also from our hearts. # 把上述文本的中文标点符号,换成英文标点。虽然可以全部翻译出来,但是在句号部分翻译出错。输入待翻译内容:阳光,不只来自太阳,也来自我们的心.心里有阳光,能看到到世界美好的一面;心里有阳光,能与有缘的人心心相映。Sunshine, not only from the sun, but also from our heart of hearts there is sunshine, the world can see the bright side;And my heart there is sunshine, people can be matched with the affinity of the heart.

由以上中英翻译效果可知,此代码还有很大的改进空间。

1、长句翻译效果不好。

2、中英文标点符号问题。

3、只能逐句翻译,没有实现翻译整个文本。

原文链接:

使用python3调用谷歌翻译成中文

Python 爬虫之Google翻译实现

下面的代码能实现很好的翻译效果:

#coding=utf-8#-*- coding: UTF-8 -*- import sysimport requestsimport refrom bs4 import BeautifulSoupdef tidySentence(inputs) :inputs = re.sub(u'&quot;', '"', inputs)inputs = re.sub(u'&lt;', '<', inputs)inputs = re.sub(u'&gt;', '>', inputs)inputs = re.sub(u'&.*?( |;)', ' ', inputs)return inputsdef getHTMLText(url):try:r = requests.get(url, timeout=30)r.raise_for_status()return r.textexcept:print("Get HTML Text Failed!")return 0def google_translate_EtoC(to_translate, from_language="en", to_language="ch-CN"):#根据参数生产提交的网址base_url = u"/m?hl={}&sl={}&ie=UTF-8&q={}"url = base_url.format(to_language, from_language, tidySentence(to_translate))#获取网页html = getHTMLText(url)if html:soup = BeautifulSoup(html, "html.parser")#解析网页得到翻译结果 try:result = soup.find_all("div", {"class":"t0"})[0].textexcept:try :print (len(to_translate), str(to_translate))except :print (len(to_translate), 'Not_a_str')print("Translation Failed!")result = to_translatereturn resultdef google_translate_CtoE(to_translate, from_language="ch-CN", to_language="en"):#根据参数生产提交的网址base_url = u"/m?hl={}&sl={}&ie=UTF-8&q={}"url = base_url.format(to_language, from_language, tidySentence(to_translate))#获取网页html = getHTMLText(url)if html:soup = BeautifulSoup(html, "html.parser")#解析网页得到翻译结果 try:result = soup.find_all("div", {"class":"t0"})[0].textexcept:try :print (len(to_translate), str(to_translate))except :print (len(to_translate), 'Not_a_str')print("Translation Failed!")result = to_translatereturn resultif __name__ == '__main__':print (google_translate_EtoC(u'You see, in life, lots of people know what to do, but few people actually do what they know. Knowing is not enough! You must take action.'))print (google_translate_CtoE(u'阳光,不只来自太阳,也来自我们的心。心里有阳光,能看到到世界美好的一面;心里有阳光,能与有缘的人心心相映。'))

参考链接:

Python Google Translate API

NLPCC

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。