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

时间:2021-07-22 18:51:24

之前做过一个桌面翻译工具,桌面每日一句--桌面翻译工具(有道翻译,微软翻译,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 = ;//设置超时等待
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使用方法

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

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

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

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

下载地址:XQSimpleTranslation.zip

简单翻译工具--必应词典第三方api使用方法的更多相关文章

  1. 微软必应词典客户端的案例分析——个人Week3作业

    第一部分 调研,评测 Bug探索 Bug No1.高亮语义匹配错位 环境: windows8,使用必应词典版本PC版:3.5.0 重现步骤: 1. 搜索"funny face"这一 ...

  2. python实战===国内很简单实用的一些开源的api以及开源项目

    原创 2017年03月25日 15:40:59 标签: api / 开源项目 / app / 免费接口   声明 以下所有 API 均由产品公司自身提供,本人皆从网络获取.获取与共享之行为或有侵犯产品 ...

  3. property - 必应词典 美['prɑpərti]英['prɒpə(r)ti] n.属性;财产;财产权;【戏】道具

    英语 (已检测) 自动检测 阿拉伯语 自动检测 爱尔兰语 自动检测 爱沙尼亚语 自动检测 保加利亚语 自动检测 冰岛语 自动检测 波兰语 自动检测 波斯尼亚语(拉丁语) 自动检测 波斯语 自动检测 丹 ...

  4. 原生js简单调用百度翻译API实现的翻译工具

    先来个在线demo: js翻译工具 或者百度搜索js简单调用百度翻译API工具(不过有个小小的界面显示bug,我想细心的人应该会发现) 或者直接前往该网址:js翻译工具 或者前往我的github:gi ...

  5. 基于百度翻译API开发属于自己的翻译工具

    你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...

  6. 用Python做一个简单的翻译工具

    编程本身是跟年龄无关的一件事,不论你现在是十四五岁,还是四五十岁,如果你热爱它,并且愿意持续投入其中,必定会有所收获. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过 ...

  7. Q promise API简单翻译

    详细API:https://github.com/kriskowal/q/wiki/API-Reference Q提供了promise的一种实现方式,现在在node中用的已经比较多了.因为没有中文的a ...

  8. 免费翻译API破解(简易翻译工具)

    思路:选取有道翻译,用fiddler抓取接口请求信息,提取相关请求参数,破解加密部分. 主要请求数据: i  :翻译文本 ts:时间戳 salt:ts +随机数 sign:加密信息,经过抓取信息,发现 ...

  9. 必应词典UWP版-开发小结

    摘要 必应词典UWP版已经上线2周了!相信有不少用户都已经体验过了吧!得益于Win10全新.强大的API,新版词典在性能上.UI体验上都有了大幅的提升,今天,小编就为大家讲讲必应词典UWP开发的故事. ...

随机推荐

  1. CSS魔法堂:重拾Border之——图片作边框

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  2. salesforce 零基础学习(三十三)通过REST方式访问外部数据以及JAVA通过rest方式访问salesforce

    本篇参考Trail教程: https://developer.salesforce.com/trailhead/force_com_dev_intermediate/apex_integration_ ...

  3. SQL 实现,如果存在就更新,如果不存在就添加

    alter proc proc_DataSummary as begin begin try begin tran --PV --统计的信息存入临时表 ), CreateDate, ) ), Crea ...

  4. cs ip 通过jmp转移命令间接赋值。无法直接对其赋值。

    jmp 寄存器 命令 对IP间接赋值.

  5. C primer plus 读书笔记第十四章

    这一章主要介绍C语言的结构和其他数据形式,是学习算法和数据结构的重点. 1.示例代码 /*book.c -- 仅包含一本书的图书目录*/ #include <stdio.h> #defin ...

  6. vmware三种网络格式

    网络地址转换(NAT) 这种访问模式指的是虚拟机不占用主机所在局域网的ip,通过使用主机的NAT功能访问局域网和互联网,意味着虚拟机可以访问局域网中的其他电脑,但是其他电脑不知道虚拟机的存在. 使用这 ...

  7. CSS中的颜色问题

    css颜色: CSS 颜色 颜色是通过对红.绿和蓝光的组合来显示的 颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十 ...

  8. daemon&lowbar;init函数:调用该函数把普通进程转变为守护进程

    #include <unistd.h> #include <syslog.h> #include <fcntl.h> #include <signal.h&g ...

  9. iOS Keychain 跨应用

    Keychain 可以用来持久保存一些信息.通常每个应用都有自己的 Keychain Access.但有时你会需要多个应用共用一些信息.这时需要创建 Keychain Access Group. Ke ...

  10. Codeforces Round &num;406 &lpar;Div&period; 2&rpar;滚粗记

    A 一看到题,不是一道解不定方程的裸题吗,调了好久exgcd. 其实一个for就好了啊 B 一直WA ON TEST 7真是烦,一想会不会是编号太大了,又写了一个map版本,无用. 调了好久好久才发现 ...