欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

三大Python翻译神器再也不用担心学不好英语

程序员文章站 2022-06-24 07:54:47
导语​hello everyone! i'm kimiko!​miss me???嘿!中秋结束了,开始正式营业给大家继续送福利啦!​中秋在家...

导语

三大Python翻译神器再也不用担心学不好英语

hello everyone!  i'm kimiko!

​miss me???

嘿!中秋结束了,开始正式营业给大家继续送福利啦!

三大Python翻译神器再也不用担心学不好英语

中秋在家发现:家里的小孩子在最后上学那天开始赶作业了!果然每个孩子的童年都是一样的!哈哈哈啊~

看了一眼侄女儿正在写英语翻译题目, 啊这一看不得了题目写的乱七八糟!!!

这让我有点儿上头啊,刚巧直接给我姐说了:‘'‘这英语还要加强学习撒'

老姐说:‘'回家我也不会教不了,学校的也没学好,一句完整的句子都讲不出来.......‘“

三大Python翻译神器再也不用担心学不好英语

那肯定是拿上我30米的鼠标键盘,开始敲代码!这款中英文翻译神器,小孩子学习英文很轻松啊~回家就给我侄女儿装上电脑了!

三大Python翻译神器再也不用担心学不好英语

正文

你是不是也经常遇到这些问题:往下看!!今天教大家写一款三大翻译神器,你值得拥有!

三大Python翻译神器再也不用担心学不好英语

环境安装:python3.6、pycharm2021,以及自带的模块。

pip install -i https://pypi.douban.com/simple/ requests 
pip install -i https://pypi.douban.com/simple/ pyqt5

​简单的界面可优化哈:

class translator(qwidget):
	def __init__(self, parent=none, **kwargs):
		super(translator, self).__init__(parent)
		self.setwindowtitle('三大翻译软件')
		self.setwindowicon(qicon('data/icon.jpg'))
		self.label1 = qlabel('原文')
		self.label2 = qlabel('译文')
		self.lineedit1 = qlineedit()
		self.lineedit2 = qlineedit()
		self.translatebutton1 = qpushbutton()
		self.translatebutton2 = qpushbutton()
		self.translatebutton3 = qpushbutton()
		self.translatebutton1.settext('百度翻译')
		self.translatebutton2.settext('有道翻译')
		self.translatebutton3.settext('谷歌翻译')
		self.grid = qgridlayout()
		self.grid.setspacing(12)
		self.grid.addwidget(self.label1, 1, 0)
		self.grid.addwidget(self.lineedit1, 1, 1)
		self.grid.addwidget(self.label2, 2, 0)
		self.grid.addwidget(self.lineedit2, 2, 1)
		self.grid.addwidget(self.translatebutton1, 1, 2)
		self.grid.addwidget(self.translatebutton2, 2, 2)
		self.grid.addwidget(self.translatebutton3, 3, 2)
		self.setlayout(self.grid)
		self.resize(400, 150)
		self.translatebutton1.clicked.connect(lambda : self.translate(api='baidu'))
		self.translatebutton2.clicked.connect(lambda : self.translate(api='youdao'))
		self.translatebutton3.clicked.connect(lambda : self.translate(api='google'))
		self.bd_translate = baidu()
		self.yd_translate = youdao()
		self.gg_translate = google()
	def translate(self, api='baidu'):
		word = self.lineedit1.text()
		if not word:
			return
		if api == 'baidu':
			results = self.bd_translate.translate(word)
		elif api == 'youdao':
			results = self.yd_translate.translate(word)
		elif api == 'google':
			results = self.gg_translate.translate(word)
		else:
			raise runtimeerror('api should be <baidu> or <youdao> or <google>...')
		self.lineedit2.settext(';'.join(results))

三大翻译之一:百度翻译类。

class baidu():
	def __init__(self):
		self.session = requests.session()
		self.session.cookies.set('baiduid', '19288887a223954909730262637d1deb:fg=1;')
		self.session.cookies.set('pstm', '%d;' % int(time.time()))
		self.headers = {
							'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.102 safari/537.36'
						}
		self.data = {
						'from': '',
						'to': '',
						'query': '',
						'transtype': 'translang',
						'simple_means_flag': '3',
						'sign': '',
						'token': '',
						'domain': 'common'
					}
		self.url = 'https://fanyi.baidu.com/v2transapi'
		self.langdetect_url = 'https://fanyi.baidu.com/langdetect'
	def translate(self, word):
		self.data['from'] = self.detectlanguage(word)
		self.data['to'] = 'en' if self.data['from'] == 'zh' else 'zh'
		self.data['query'] = word
		self.data['token'], gtk = self.gettokengtk()
		self.data['token'] = '6482f137ca44f07742b2677f5ffd39e1'
		self.data['sign'] = self.getsign(gtk, word)
		res = self.session.post(self.url, data=self.data)
		return [res.json()['trans_result']['data'][0]['result'][0][1]]
	def gettokengtk(self):
		url = 'https://fanyi.baidu.com/'
		res = requests.get(url, headers=self.headers)
		token = re.findall(r"token: '(.*?)'", res.text)[0]
		gtk = re.findall(r";window.gtk = ('.*?');", res.text)[0]
		return token, gtk
	def getsign(self, gtk, word):
		evaljs = js2py.evaljs()
		js_code = js.bd_js_code
		js_code = js_code.replace('null !== i ? i : (i = window[l] || "") || ""', gtk)
		evaljs.execute(js_code)
		sign = evaljs.e(word)
		return sign
	def detectlanguage(self, word):
		data = {
				'query': word
				}
		res = self.session.post(self.langdetect_url, headers=self.headers, data=data)
		return res.json()['lan']

​三大翻译之二:有道翻译类。

class youdao():
	def __init__(self):
		self.headers = {
						'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.77 safari/537.36',
						'referer': 'http://fanyi.youdao.com/',
						'cookie': 'outfox_search_user_id=-481680322@10.169.0.83;'
					}
		self.data = {
						'i': none,
						'from': 'auto',
						'to': 'auto',
						'smartresult': 'dict',
						'client': 'fanyideskweb',
						'salt': none,
						'sign': none,
						'ts': none,
						'bv': none,
						'doctype': 'json',
						'version': '2.1',
						'keyfrom': 'fanyi.web',
						'action': 'fy_by_realtlme'
					}
		self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
	def translate(self, word):
		ts = str(int(time.time()*10000))
		salt = ts + str(int(random.random()*10))
		sign = 'fanyideskweb' + word + salt + '97_3(jkmyg@t[kzqmqjtk'
		sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
		bv = '5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.77 safari/537.36'
		bv = hashlib.md5(bv.encode('utf-8')).hexdigest()
		self.data['i'] = word
		self.data['salt'] = salt
		self.data['sign'] = sign
		self.data['ts'] = ts
		self.data['bv'] = bv
		res = requests.post(self.url, headers=self.headers, data=self.data)
		return [res.json()['translateresult'][0][0].get('tgt')]

​三大翻译之三:google翻译类。

class google():
	def __init__(self):
		self.headers = {
						'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.77 safari/537.36',
					}
		self.url = 'https://translate.google.cn/translate_a/single?client=t&sl=auto&tl={}&hl=zh-cn&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&tk={}&q={}'
	def translate(self, word):
		if len(word) > 4891:
			raise runtimeerror('the length of word should be less than 4891...')
		languages = ['zh-cn', 'en']
		if not self.ischinese(word):
			target_language = languages[0]
		else:
			target_language = languages[1]
		res = requests.get(self.url.format(target_language, self.gettk(word), word), headers=self.headers)
		return [res.json()[0][0][0]]
	def gettk(self, word):
		evaljs = js2py.evaljs()
		js_code = js.gg_js_code
		evaljs.execute(js_code)
		tk = evaljs.tl(word)
		return tk
	def ischinese(self, word):
		for w in word:
			if '\u4e00' <= w <= '\u9fa5':
				return true
		return false

效果如下:中英文各一句哈!

三大Python翻译神器再也不用担心学不好英语

三大Python翻译神器再也不用担心学不好英语

​总结

每天进步一点点!记得“三连哦~爱你,坚持学习!!!

三大Python翻译神器再也不用担心学不好英语

到此这篇关于三大python翻译神器再也不用担心学不好英语的文章就介绍到这了,更多相关python 翻译内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!