首页 技术 正文
技术 2022年11月12日
0 收藏 756 点赞 4,150 浏览 5607 个字

最近有个需求,要将几万条数据从日语翻译成中文。因为数据的获取和处理用的是python代码,所以想先尝试翻译部分也用python实现。

目前网上查到的翻译方法有百度、有道以及谷歌翻译,下面会对这三个方法进行简单的测试和分析。如果大家知道有更好的方法(速度快、结果准确),还请分享!

模块导入

 import re
import urllib.parse, urllib.request
import hashlib
import urllib
import random
import json
import time
from translate import Translator

非python自带的库,如python google translator,需要手动安装,命令pip install module_name。

1. 百度翻译

 appid = 'your_appid'
secretKey = 'your_secretKey'
url_baidu = 'http://api.fanyi.baidu.com/api/trans/vip/translate' def translateBaidu(text, f='ja', t='zh'):
salt = random.randint(, )
sign = appid + text + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
url = url_baidu + '?appid=' + appid + '&q=' + urllib.parse.quote(text) + '&from=' + f + '&to=' + t + \
'&salt=' + str(salt) + '&sign=' + sign
response = urllib.request.urlopen(url)
content = response.read().decode('utf-8')
data = json.loads(content)
result = str(data['trans_result'][]['dst'])
print(result)

参数:text–待翻文本,f–初始语言,t–目标语言,后面方法类似。

2. 有道翻译

 url_youdao = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=' \
'http://www.youdao.com/'
dict = {}
dict['type'] = 'AUTO'
dict['doctype'] = 'json'
dict['xmlVersion'] = '1.8'
dict['keyfrom'] = 'fanyi.web'
dict['ue'] = 'UTF-8'
dict['action'] = 'FY_BY_CLICKBUTTON'
dict['typoResult'] = 'true' def translateYoudao(text):
global dict
dict['i'] = text
data = urllib.parse.urlencode(dict).encode('utf-8')
response = urllib.request.urlopen(url_youdao, data)
content = response.read().decode('utf-8')
data = json.loads(content)
result = data['translateResult'][][]['tgt']
print(result)

参数主要由字典dict指定,发现没有地方可以指定语言(可能是我没找到),测试结果是不管输入什么语言的文本,输出均是中文。

3. 谷歌翻译

 url_google = 'http://translate.google.cn'
reg_text = re.compile(r'(?<=TRANSLATED_TEXT=).*?;')
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
r'Chrome/44.0.2403.157 Safari/537.36' def translateGoogle(text, f='ja', t='zh-cn'):
values = {'hl': 'zh-cn', 'ie': 'utf-8', 'text': text, 'langpair': '%s|%s' % (f, t)}
value = urllib.parse.urlencode(values)
req = urllib.request.Request(url_google + '?' + value)
req.add_header('User-Agent', user_agent)
response = urllib.request.urlopen(req)
content = response.read().decode('utf-8')
data = reg_text.search(content)
result = data.group().strip(';').strip('\'')
print(result)

和上面两种方法一样,采用的是访问网页的形式来进行翻译。

还有一种是利用python谷歌翻译模块Translator:

 def translateGoogle2(text):
result = translator.translate(text)
print(result)

4. 测试代码

测试过程:

翻译5个字串为一个小的单位,输出消耗时间;

循环10次为一个大的单位,输出消耗时间;

对不同的语言字串和循环次数做过多次测试,发现情况基本类似,所以这里选择了10次。

 text_list = ['こんにちは', 'こんばんは', 'おはようございます', 'お休(やす)みなさい', 'お元気(げんき)ですか'] time_baidu =
time_youdao =
time_google =
time_google2 = for i in list(range(, )):
time1 = time.time()
for text in text_list:
translateBaidu(text)
time2 = time.time()
print('百度翻译第%s次时间:%s' % (i, time2 - time1))
time_baidu += (time2 - time1) time1 = time.time()
for text in text_list:
translateYoudao(text)
time2 = time.time()
print('有道翻译第%s次时间:%s' % (i, time2 - time1))
time_youdao += (time2 - time1) time1 = time.time()
for text in text_list:
translateGoogle(text)
time2 = time.time()
print('谷歌翻译第%s次时间:%s' % (i, time2 - time1))
time_google += (time2 - time1) time1 = time.time()
for text in text_list:
translateGoogle2(text)
time2 = time.time()
print('谷歌2翻译第%s次时间:%s' % (i, time2 - time1))
time_google2 += (time2 - time1) print('百度翻译时间:%s' % (time_baidu / ))
print('有道翻译时间:%s' % (time_youdao / ))
print('谷歌翻译时间:%s' % (time_google / ))
print('谷歌2翻译时间:%s' % (time_google2 / ))

5. 结果分析

日语字串原意为[‘你好’, ‘晚上好’, ‘早上好’, ‘晚安’, ‘您还好吧’]。

测试代码输出结果:

您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第1次时间:0.5849709510803223
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第1次时间:0.46173906326293945
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第1次时间:3.84399676322937
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第1次时间:6.819758892059326
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第2次时间:0.4968142509460449
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第2次时间:0.3870818614959717
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第2次时间:3.5689375400543213
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第2次时间:6.108794450759888
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第3次时间:0.4832003116607666
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第3次时间:0.40560245513916016
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第3次时间:3.875128984451294
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第3次时间:5.547708034515381
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第4次时间:0.4904344081878662
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第4次时间:0.3860180377960205
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第4次时间:3.5466465950012207
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第4次时间:7.052653551101685
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第5次时间:0.4754292964935303
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第5次时间:0.37929368019104004
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第5次时间:3.503594160079956
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第5次时间:4.944894552230835
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第6次时间:0.4637324810028076
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第6次时间:0.3679838180541992
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第6次时间:3.4939000606536865
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第6次时间:4.786132335662842
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第7次时间:0.4783976078033447
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第7次时间:0.3760185241699219
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第7次时间:3.485666036605835
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第7次时间:6.591272592544556
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第8次时间:0.4756813049316406
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第8次时间:0.4083871841430664
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第8次时间:3.3123676776885986
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第8次时间:5.902927875518799
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第9次时间:0.46607208251953125
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第9次时间:0.5259883403778076
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第9次时间:3.919294834136963
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第9次时间:6.256660223007202
您好
晚上好
早上好!
请您休息。
您身体好吗?
百度翻译第10次时间:0.5158905982971191
你好
晚安
早上好。
您休息吧、)
好(身体)好吗?
有道翻译第10次时间:0.38652658462524414
您好
晚上好
早上好
看看你的假期(康)
小心(元气)是
谷歌翻译第10次时间:3.3273775577545166
你好
问候
问候
请休息
照顾 (玄龟) 吗?
谷歌2翻译第10次时间:6.155425071716309
百度翻译时间:0.4930623292922974
有道翻译时间:0.4084639549255371
谷歌翻译时间:3.5876910209655763
谷歌2翻译时间:6.016622757911682

从打印的内容分析,耗时情况为有道<百度<谷歌<谷歌2,准确度情况为百度>有道>谷歌>谷歌2。

速度先不管,对后面两个文本的翻译结果,谷歌的准确度是较低的,网页版的翻译结果为:

Python有哪些好用的语言翻译方法

其实像有道或百度,5条字串耗时400毫秒左右,对于大量数据是不太可取的。如20万条数据,需要约4.4个小时。

所以,如果你知道python中有更好的翻译方法,欢迎分享。或许,这种大量数据的翻译操作用其他语言实现比较合适?

6. 改进 & 感谢

谷歌翻译,灿烂千阳建议日语->英语->中文的顺序,结果的准确度改进了很多(只是多了一步,速度下降了些);

耗时问题,codegay提出多线程方案,可以将速度提升几倍,取决于线程或服务器的个数;

具体改进的代码就不贴出来了,在原来的基础上做调整即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,840