您的位置:首页 > 其它

简单翻译工具--必应词典第三方api使用方法

2016-09-16 18:38 537 查看
之前做过一个桌面翻译工具,桌面每日一句--桌面翻译工具(有道翻译,微软翻译,Google翻译) 获取金山每日一句,目前因为 金山每日一句页面改变导致每日一句功能失败,不过这工具自己用得最多的还是翻译功能,干脆把翻译独立出来。

另外,最近在逛知乎发现有人分享了必应词典的第三方api,所以顺道拿来完善,api作者分享页面:https://zhuanlan.zhihu.com/p/22421123

这个必应词典用起来很简单直接访问地址http://xtk.azurewebsites.net/BingDictService.aspx?Word=x x是待翻译的词语,返回json,最后就是解析json就行,解析jason用的是开源库:http://dynamicjson.codeplex.com/

直接贴代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Codeplex.Data;

namespace BingTranslate
{
public class BingDictApi
{

private static string GetSource(string PageUrl)
{
try
{
WebRequest request = WebRequest.Create(PageUrl); //WebRequest.Create方法,返回WebRequest的子类HttpWebRequest
request.Timeout = 5000;//设置超时等待
WebResponse response = request.GetResponse(); //WebRequest.GetResponse方法,返回对 Internet 请求的响应
Stream resStream = response.GetResponseStream(); //WebResponse.GetResponseStream 方法,从 Internet 资源返回数据流。
Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312
StreamReader sr = new StreamReader(resStream, enc); //命名空间:System.IO。 StreamReader 类实现一个 TextReader (TextReader类,表示可读取连续字符系列的读取器),使其以一种特定的编码从字节流中读取字符。
string source = sr.ReadToEnd(); //输出(HTML代码),ContentHtml为Multiline模式的TextBox控件
resStream.Close();
//Console.Write(source);
sr.Close();
return source;
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
return "";
}

}

public BingDictobject Translate(string word)
{
BingDictobject dictobject = new BingDictobject();
string encodedStr = HttpUtility.UrlEncode(word);
string url = string.Format("http://xtk.azurewebsites.net/BingDictService.aspx?Word={0}", encodedStr);
string text = GetSource(url);
if (text.Contains("An error occurs."))
{
return null;
}
var json = DynamicJson.Parse(text);
dictobject.word = json.word;
if (json.pronunciation())//判断属性是否存在
{
var jsPronunciation = json.pronunciation;
if (jsPronunciation!= null)
{
Pronunciation pronunciation = new Pronunciation();
pronunciation.AmE = jsPronunciation.AmE;
pronunciation.AmEmp3 = jsPronunciation.AmEmp3;
pronunciation.BrE = jsPronunciation.BrE;
pronunciation.BrEmp3 = jsPronunciation.BrEmp3;
dictobject.pronunciation = pronunciation;
}

}
if (json.defs())
{
var jsdef = json.defs;
if (jsdef!= null)
{
Def[] defs = jsdef;
dictobject.defs = defs;
}

}

if (json.sams())
{
var jssam = json.sams;
if (jssam!=null)
{
Sam[] sams = jssam;
dictobject.sams = sams;
}

}
return dictobject;
}
public class BingDictobject
{
public string word { get; set; }
public Pronunciation pronunciation { get; set; }
public Def[] defs { get; set; }
public Sam[] sams { get; set; }
}

public class Pronunciation
{
public string AmE { get; set; }
public string AmEmp3 { get; set; }
public string BrE { get; set; }
public string BrEmp3 { get; set; }
}

public class Def
{
public string pos { get; set; }
public string def { get; set; }
}

public class Sam
{
public string eng { get; set; }
public string chn { get; set; }
public string mp3Url { get; set; }
public string mp4Url { get; set; }
}

}
}


调用方法:

using BingTranslate;

namespace BingTranslateTest
{
class Program
{
static void Main(string[] args)
{
BingDictApi bing = new BingDictApi();
bing.Translate("china");
}
}
}


翻译效果:



工具附带其它翻译api,不再一一说明,原理也很简单,想研究的可以使用反汇编工具查看

使用也很简单,喜欢的可以下载使用:

启动后,在图盘图标中调出设计界面,设置好快捷键(默认ctrl+T) 按快捷键就能调出翻译界面



下载地址:XQSimpleTranslation.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐