youdao-fayi是用Ruby写的有道翻译API。
有道翻译是有道提供的一个翻译服务。
前期工作
在Codig之前,应该先去有道翻译API获取一个API密钥
示例
# codig: UTF-8# example.rbrequire 'youdao-fayi'# Cofigure the key firstYoudaoFayi::Cofig.key_from = "youdao-fayi"YoudaoFayi::Cofig.key = 1629987369# The use the 4 methods provided.to_be_traslated = "要翻译的词句"YoudaoFayi.search_jso(to_be_traslated) # returs a JSON strigYoudaoFayi.search_xml(to_be_traslated) # returs a XML strigYoudaoFayi.search_jsop(to_be_traslated) # returs a JSONP strigYoudaoFayi.search_result_obj(to_be_traslated) # returs a YoudaoFayi::Result object有道翻译 API 提供3种类型的数据:xml,jso和jsop。
xml示例:
https://fayi.youdao.com/fayiapi.do?keyfrom=&key=&type=data&doctype=xml&versio=1.1&q=这里是有道翻译API
<?xml versio="1.0" ecodig="UTF-8"?><youdao-fayi> <errorCode>0</errorCode> <!-- 有道翻译 --> <query><![CDATA[这里是有道翻译API]]></query> <traslatio> <paragraph><![CDATA[Here is the youdao traslatio API]]></paragraph> </traslatio></youdao-fayi>jso示例:
https://fayi.youdao.com/fayiapi.do?keyfrom=&key=&type=data&doctype=jso&versio=1.1&q=翻译
{ "errorCode":0 "query":"翻译", "traslatio":["traslatio"], // 有道翻译 "basic":{ // 有道词典-基本词典 "phoetic":"fā yì", "explais":[ "traslate", "iterpret" ] }, "web":[ // 有道词典-网络释义 { "key":"翻译", "value":["traslator","traslatio","traslate","Iterpreter"] }, {...} ]}jsop示例:
https://fayi.youdao.com/fayiapi.do?keyfrom=&key=&type=data&doctype=jsop&callback=show&versio=1.1&q=API
show({ "errorCode":0 "query":"API", "traslatio":["API"], // 有道翻译 "basic":{ // 有道词典-基本词典 "explais":[ "abbr. 应用程序界面(Applicatio Program Iterface);..." ] }, "web":[ // 有道词典-网络释义 { "key":"API", "value":["应用程序接口(Applicatio Programmig Iterface)","应用编程接口","应用程序编程接口","美国石油协会"] }, {...} ]});
评论