简单翻译工具--必应词典第三方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/
直接贴代码:
调用方法:
翻译效果:
工具附带其它翻译api,不再一一说明,原理也很简单,想研究的可以使用反汇编工具查看
使用也很简单,喜欢的可以下载使用:
启动后,在图盘图标中调出设计界面,设置好快捷键(默认ctrl+T) 按快捷键就能调出翻译界面
下载地址:XQSimpleTranslation.zip
另外,最近在逛知乎发现有人分享了必应词典的第三方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
相关文章推荐
- 使用strace工具故障排查的5种简单方法
- 【百度地图API】百度API卫星图使用方法和卫星图对比工具
- 使用strace工具故障排查的5种简单方法
- Qt-Linguist工具简单介绍和使用方法
- 【百度地图API】百度API卫星图使用方法和卫星图对比工具
- 使用 Google API 进行翻译的 Python 小工具
- [zz]使用strace工具故障排查的5种简单方法
- 使用strace工具故障排查的5种简单方法
- Myeclipse for spring9.0破解方法试验过同样适用于10-超简单明白 (使用网上流传的自动破解工具无法破解的可以使用这个) 亲测可用
- Qt第三方图形控件qwt的编译安装与使用的简单方法(Win7+Qt4.8.6+MinGW4.8.2)
- VC++开发中最简单的Multi-Byte(ANSI)和Unicode互转方法,灵活使用Windows基本API:wsprintfA, wsprintfW
- jqPlot——基于jquery的图表绘制工具简单使用,jqPlot 在firefox 火狐浏览器里面,图表右侧的标题,变形的解决方法
- 【百度地图API】百度API卫星图使用方法和卫星图对比工具
- APIDemo学习笔记——Android上几种简单的Animation使用方法(一)
- MyEclipse8.0与SVN版本工具集成及简单使用方法介绍
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition
- VC++开发中最简单的Multi-Byte(ANSI)和Unicode互转方法,灵活使用Windows基本API:wsprintfA, wsprintfW
- 使用 Google API 进行翻译的 Python 小工具
- pdf分割的工具使用的简单方法
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)