baidu开放的翻译接口http://api.fanyi.baidu.com/api/trans/product/apidoc支持每月200万字的免费翻译,提供了各种语言的使用demo。python语言的demo是2版本的,稍作修改在python3跑了下,并将结果写入文件中。这样可以获得一些中英翻译的语料,进行翻译,分类等很多自然语言的任务。
#coding=utf-8
import http.client
import hashlib
from urllib import parse
import random
appid = '注册的id'
secretKey = '注册获得的key'
httpClient = None
myurl = '/api/trans/vip/translate'
q = '苹果是一家很棒的公司\n我爱北京天安门'
fromLang = 'zh'
toLang = 'en'
salt = random.randint(32768, 65536)
sign = appid+q+str(salt)+secretKey
m1 = hashlib.md5()
m1.update(sign.encode(encoding='utf-8'))
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
file = open('result.txt','w')
try:
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
response = httpClient.getresponse()
str = response.read().decode('utf-8')
str = eval(str)
for line in str['trans_result']:
file.write(line['dst']+'\n')
except Exception as e:
print(e)
finally:
if httpClient:
httpClient.close()
file.close()
翻译结果:
Apple is a great company
I Love Beijing Tiananmen