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

作者:顾木子吖 时间:2022-02-13 07:27:19 

导语

三大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翻译神器再也不用担心学不好英语

来源:https://blog.csdn.net/weixin_55822277/article/details/120411770

标签:Python,翻译,英语
0
投稿

猜你喜欢

  • python tkinter实现学生信息管理系统

    2021-11-01 18:46:16
  • Python time模块时间获取和转换方法

    2022-06-07 11:14:30
  • pytorch进行上采样的种类实例

    2023-11-28 14:02:03
  • python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】

    2023-07-09 01:15:42
  • PHP三层结构(上) 简单三层结构

    2023-06-20 06:36:07
  • js怎么覆盖原有方法实现重写

    2024-05-09 10:39:22
  • SQL触发器实例讲解

    2012-04-13 11:52:48
  • vue中@keyup.enter失效问题及解决

    2023-07-02 17:01:38
  • JavaScript Event学习第四章 传统的事件注册模型

    2024-05-09 10:38:04
  • SQL Server 2005 Management Studio Express企业管理器将英文变成简体中文版的实现方法

    2024-01-27 23:35:38
  • 很酷的JQuery Solar System

    2007-12-15 08:09:00
  • 一文秒懂python读写csv xml json文件各种骚操作

    2023-08-03 23:50:42
  • asp中InstrRev的语法

    2008-01-22 18:14:00
  • JavaScript+Node.js写一款markdown解析器

    2024-04-18 09:36:06
  • PHP iconv 解决utf-8和gb2312编码转换问题

    2024-04-29 13:56:58
  • 关于SSD目标检测模型的人脸口罩识别

    2023-06-20 05:20:56
  • php动态生成版权所有信息的方法

    2024-05-02 17:17:30
  • 在ubuntu18.04版本安装vscode(2种)

    2022-07-23 12:05:41
  • python标准库 datetime的astimezone设置时区遇到的坑及解决

    2022-05-01 23:54:56
  • 从零学python系列之从文件读取和保存数据

    2021-02-11 01:51:29
  • asp之家 网络编程 m.aspxhome.com