之前在维护更新
时,发现之前写的调用google去翻译的逻辑,早已失效。
现在要去换成其他,更稳定的逻辑
能想到的是有道翻译,在线的网页,或者是API
在线翻译
在线翻译 api
->
->
http://ai.youdao.com/doc.s#docs
->
去登录和注册
去创建应用,再去创建服务
再去绑定。
先点击右上角的新消息看看:
再去创建服务:
自然语言翻译-》翻译实例
trans_en_to_cn
目前看起来适合用:
按字符收费 | 在线翻译 | 48元/百万字符 |
对于默认的100元,大概够用200万的字符的翻译。
-》够撑一段时间的了。
-》等费用用完了,再去改为用在线网站
(或:
)
调用的方式。
然后参考文档:
去试试api调用
最开始使用时,建议参考:
http://ai.youdao.com/doc.s#guide
开发的话,参考:
http://ai.youdao.com/docs/doc-trans-api.s#p04
然后再去:
然后去试试,是可以的,得到了翻译结果了:
请求示例接口地址:
返回:
{ "query": "纠删码(EC)的学习", "translation": [ "The study of correcting code (EC)" ], "errorCode": "0", "dict": { "url": "yddict://m.youdao.com/dict?le=eng&q=%E7%BA%A0%E5%88%A0%E7%A0%81%28EC%29%E7%9A%84%E5%AD%A6%E4%B9%A0" }, "webdict": { "url": "http://m.youdao.com/dict?le=eng&q=%E7%BA%A0%E5%88%A0%E7%A0%81%28EC%29%E7%9A%84%E5%AD%A6%E4%B9%A0"; }, "l": "zh-CHS2EN" } |
(
但是
免费 MD5 散列计算器 – ChromeFans.org
http://md5calculator.chromefans.org/?langid=zh-cn
计算出来的值是:
2c7476cebc495a18869cf88f1b0746f2
用:
结果报错:
{
"errorCode": "202",
"l": "zh_CHS2EN"
}
202根据:
错误代码列表
http://ai.youdao.com/docs/doc-trans-api.s#p06
是:签名检验失败
)
所以,接着可以去代码中,使用整个API了
【总结】
相关翻译的代码为:
<---------------------------------------------------------------------------- # translate strToTranslate from fromLanguage to toLanguage # return the translated unicode string # supported languages can refer: # 有道智云 -> 帮助与文档 > 产品文档 > 自然语言翻译 > API 文档 > 支持的语言表 def translateString(strToTranslate, fromLanguage="zh-CHS", toLanguage="EN"): logging.debug("translateString: strToTranslate=%s, from=%s, to=%s", strToTranslate, fromLanguage, toLanguage) errorCodeDict = { 101 : "缺少必填的参数,出现这个情况还可能是et的值和实际加密方式不对应", 102 : "不支持的语言类型", 103 : "翻译文本过长", 104 : "不支持的API类型", 105 : "不支持的签名类型", 106 : "不支持的响应类型", 107 : "不支持的传输加密类型", 108 : "appKey无效,注册账号, 登录后台创建应用和实例并完成绑定, 可获得应用ID和密钥等信息,其中应用ID就是appKey( 注意不是应用密钥)", 109 : "batchLog格式不正确", 110 : "无相关服务的有效实例", 111 : "开发者账号无效,可能是账号为欠费状态", 201 : "解密失败,可能为DES,BASE64,URLDecode的错误", 202 : "签名检验失败", 203 : "访问IP地址不在可访问IP列表", 301 : "辞典查询失败", 302 : "翻译查询失败", 303 : "服务端的其它异常", 401 : "账户已经欠费停" } transOK = False translatedStr = strToTranslate transErr = ” appKey = "152e0e77723a0026" saltStr = str(random.randint(1, 65536)) secretKey = "sYmnnOaisQgZZrlrBFozWAtsaRyyJg4N" logging.debug("appKey=%s,strToTranslate=%s,saltStr=%s,secretKey=%s", appKey, strToTranslate, saltStr, secretKey) strToMd5 = appKey + strToTranslate + saltStr + secretKey logging.debug("strToMd5=%s", strToMd5) md5Sign = generateMd5(strToMd5) try : quotedQueryStr = urllib.quote(strToTranslate) #transUrl = "http://openapi.youdao.com/api?q=纠删码(EC)的学习&from=zh_CHS&to=EN&appKey=152e0e77723a0026&salt=4&sign=6BE15F1868019AD71C442E6399DB1FE4"; # transUrl = "http://openapi.youdao.com/api?q=%s&from=zh_CHS&to=EN&appKey=152e0e77723a0026&salt=4&sign=6BE15F1868019AD71C442E6399DB1FE4"; % (quotedQueryStr) transUrl = "http://openapi.youdao.com/api?q=%s&from=%s&to=%s&appKey=%s&salt=%s&sign=%s"; \ % (quotedQueryStr, fromLanguage, toLanguage, appKey, saltStr, md5Sign) logging.debug("transUrl=%s", transUrl) respJsonStr = getUrlRespHtml(transUrl) logging.debug("respJsonStr=%s", respJsonStr) # respJsonStr={"query":"纠删码(EC)的学习","translation":["The study of correcting code (EC)"],"errorCode":"0","dict":{"url":"yddict://m.youdao.com/dict?le=eng&q=%E7%BA%A0%E5%88%A0%E7%A0%81%28EC%29%E7%9A%84%E5%AD%A6%E4%B9%A0"},"webdict":{"url":"http://m.youdao.com/dict?le=eng&q=%E7%BA%A0%E5%88%A0%E7%A0%81%28EC%29%E7%9A%84%E5%AD%A6%E4%B9%A0"},"l":"zh-CHS2en"} translatedDict = json.loads(respJsonStr) logging.debug("translatedDict=%s", translatedDict) # translatedDict={u’l’: u’zh-CHS2EN’, u’errorCode’: u’0′, u’dict’: {u’url’: u’yddict://m.youdao.com/dict?le=eng&q=%E9%9B%A2%E5%B3%B6%E9%A2%A8%E5%85%89%E7%9A%84%E6%B5%81%E9%80%A3%E5%BF%98%E8%BF%94′}, u’webdict’: {u’url’: u’http://m.youdao.com/dict?le=eng&q=%E9%9B%A2%E5%B3%B6%E9%A2%A8%E5%85%89%E7%9A%84%E6%B5%81%E9%80%A3%E5%BF%98%E8%BF%94′;}, u’query’: u’\u96e2\u5cf6\u98a8\u5149\u7684\u6d41\u9023\u5fd8\u8fd4′, u’translation’: [u’Away from the island scenery’]} errorCode = int(translatedDict["errorCode"]) # logging.debug("errorCode=%s", errorCode) errorCodeDictKeys = errorCodeDict.keys() # logging.debug("errorCodeDictKeys=%s", errorCodeDictKeys) if errorCode != 0 : if errorCode in errorCodeDictKeys : # logging.info("errorCode=%s in errorCodeDictKeys=%s", errorCode, errorCodeDictKeys) transOK = False transErr = errorCodeDict[errorCode] # logging.info("transErr=%s ", transErr) else : transOK = False transErr = "未知错误" else : queryUnicode = translatedDict["query"] translationUnicode = translatedDict["translation"][0] logging.debug(u"queryUnicode=%s, translationUnicode=%s", queryUnicode, translationUnicode) transOK = True translatedStr = translationUnicode.encode("utf-8") logging.debug("translatedStr=%s ", translatedStr) except urllib2.URLError,reason : transOK = False transErr = reason except urllib2.HTTPError,code : transOK = False transErr = code if transOK : logging.info("Translate OK: %s -> %s", strToTranslate, translatedStr) return (transOK, translatedStr) else : logging.info("Translate fail for %s", transErr) return (transOK, transErr) <---------------------------------------------------------------------------- # translate the Chinese(zh-CHS) string to English(EN) def transZhcnToEn(strToTrans) : translatedStr = strToTrans transOK = False transErr = ” if strIsAscii(strToTrans) : transOK = True translatedStr = strToTrans else : # (transOK, translatedStr) = translateString(strToTrans, "zh-CN", "en") (transOK, translatedStr) = translateString(strToTrans, "zh-CHS", "EN") return (transOK, translatedStr) |
完整的库文件参见:
https://github.com/crifan/crifanLib/blob/master/python/crifanLib.py
完整的项目代码参考: