csharp:百度翻译
参考:http://api.fanyi.baidu.com/api/trans/product/index
http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91API
winform:
/// <summary>
/// 涂聚文
/// </summary>
public partial class Form5 : Form
{
TranClass tranClass = new TranClass();
string url = "";
string youAPI_Id = "geovindu";
/// <summary>
///
/// </summary>
/// <returns></returns>
DataTable setLanguage()
{ DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "中文--英文");
dt.Rows.Add(2, "英文--中文");
dt.Rows.Add(3, "中文--日文");
dt.Rows.Add(4, "日文--中文");
dt.Rows.Add(5, "中文--韩文");
dt.Rows.Add(6, "韩文--中文");
dt.Rows.Add(7, "西班牙文--中文");
dt.Rows.Add(8, "中文--西班牙文");
return dt; }
/// <summary>
///
/// </summary>
public Form5()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form5_Load(object sender, EventArgs e)
{
this.cbxTranslateChange.DataSource = setLanguage();
this.cbxTranslateChange.ValueMember = "id";
this.cbxTranslateChange.DisplayMember = "name"; }
/// <summary>
/// 翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient(); //引用System.Net
string fromTranslate = tbxFromContent.Text; //翻译前的内容
if (!string.IsNullOrEmpty(fromTranslate))
{
//client_id为自己的api_id,q为翻译对象,from为翻译语言,to为翻译后语言
string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", youAPI_Id, fromTranslate, tranClass.From, tranClass.To);
var buffer = client.DownloadData(url);
string result = Encoding.UTF8.GetString(buffer);
StringReader sr = new StringReader(result);
JsonTextReader jsonReader = new JsonTextReader(sr); //引用Newtonsoft.Json 自带
JsonSerializer serializer = new JsonSerializer();
var r = serializer.Deserialize<TranClass>(jsonReader); //因为获取后的为json对象 ,实行转换
tbxToContent.Text = r.Trans_result[0].dst; //dst为翻译后的值
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxTranslateChange_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
switch (comboBox.SelectedIndex)
{
case 1:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 2:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 3:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 4:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 5:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 6:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 7:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 8:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; }
} } public class TranClass
{
public string From { get; set; }
public string To { get; set; }
public List<Trans_result> Trans_result { get; set; }
}
public class Trans_result
{
public string src { get; set; }
public string dst { get; set; }
} /* 语言简写 名称
auto 自动检测
zh 中文
en 英语
yue 粤语
wyw 文言文
jp 日语
kor 韩语
fra 法语
spa 西班牙语
th 泰语
ara 阿拉伯语
ru 俄语
pt 葡萄牙语
de 德语
it 意大利语
el 希腊语
nl 荷兰语
pl 波兰语
bul 保加利亚语
est 爱沙尼亚语
dan 丹麦语
fin 芬兰语
cs 捷克语
rom 罗马尼亚语
slo 斯洛文尼亚语
swe 瑞典语
hu 匈牙利语
cht 繁体中文
*/
}
webform:
TranClass tranClass = new TranClass();
string url = "www.dusystem.com";
string youAPI_Id = "geovindu";
/// <summary>
/// 涂聚文
/// </summary>
/// <returns></returns>
DataTable setLanguage()
{ DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "中文--英文");
dt.Rows.Add(2, "英文--中文");
dt.Rows.Add(3, "中文--日文");
dt.Rows.Add(4, "日文--中文");
dt.Rows.Add(5, "中文--韩文");
dt.Rows.Add(6, "韩文--中文");
dt.Rows.Add(7, "西班牙文--中文");
dt.Rows.Add(8, "中文--西班牙文");
return dt; }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.DataSource = setLanguage();
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataBind();
//DropDownList1.SelectedIndex = 0;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
int k = int.Parse(DropDownList1.SelectedValue);
switch (k)
{
case 1:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 2:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 3:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 4:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 5:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 6:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 7:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 8:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; } WebClient client = new WebClient(); //引用System.Net
string fromTranslate = this.TextBox1.Text; //翻译前的内容 if (object.Equals(tranClass, null))
{
tranClass.From = "zh";
tranClass.To = "en";
} if (!string.IsNullOrEmpty(fromTranslate))
{
//client_id为自己的api_id,q为翻译对象,from为翻译语言,to为翻译后语言
string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", youAPI_Id, fromTranslate, tranClass.From, tranClass.To);
var buffer = client.DownloadData(url);
string result = Encoding.UTF8.GetString(buffer);
StringReader sr = new StringReader(result);
JsonTextReader jsonReader = new JsonTextReader(sr); //引用Newtonsoft.Json 自带
while (jsonReader.Read())
{
//Response.Write(jsonReader.Value.ToString() + "<br/>");//jsonReader.TokenType.ToString() + "" + jsonReader.ValueType.ToString() + "" +
}
Response.Write(CallInterface(fromTranslate, tranClass.From, tranClass.To));
var j = JsonConvert.DeserializeObject<TranClass>(result); //JsonSerializer serializer = new JsonSerializer(); //TranClass p1 = (TranClass)serializer.Deserialize(new JsonTextReader(sr), typeof(TranClass));
//TranClass r = serializer.Deserialize<TranClass>(jsonReader); //因为获取后的为json对象 ,实行转换 反序列化 错误
//this.TextBox2.Text = r.Trans_result[0].dst; //dst为翻译后的值
if (!object.Equals(j, null))
{
this.TextBox2.Text = j.Trans_result[0].dst;
}
}
} /// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ switch (DropDownList1.SelectedIndex)
{
case 0:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 1:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 2:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 3:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 4:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 5:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 6:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 7:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; }
}
/// <summary>
/// 请求百度接口
///
/// https://github.com/lbv/litjson
/// </summary>
/// <param name="sub">要翻译的文本</param>
/// <param name="from">from语言</param>
/// <param name="to">翻译为语言</param>
/// <returns></returns>
private string CallInterface(string sub, string from, string to)
{ HttpContext.Current.Application.Lock();
if (HttpContext.Current.Application["baidu"] == null)
{
HttpContext.Current.Application["baidu"] = 1;
}
else
{
HttpContext.Current.Application["baidu"] = (int)HttpContext.Current.Application["baidu"] + 1;
}
HttpContext.Current.Application.UnLock();
string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", youAPI_Id, sub, from, to);
string down = "", result = "";
try
{
WebClient wc = new WebClient();
down = wc.DownloadString(url);
}
catch (Exception)
{ }
JsonData jd = JsonMapper.ToObject(down);
if (JsonDataContainsKey(jd, "error_code"))
{
result += sub;
}
else
{
JsonData jdResult = jd["trans_result"];
for (int j = 0; j < jdResult.Count; j++)
{
JsonData jdDst = jdResult[j]["dst"];
result += jdDst.ToString();
}
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public bool JsonDataContainsKey(JsonData data, string key)
{
bool result = false;
if (data == null)
return result;
if (!data.IsObject)
{
return result;
}
IDictionary tdictionary = data as IDictionary;
if (tdictionary == null)
return result;
if (tdictionary.Contains(key))
{
result = true;
}
return result;
} } /// <summary>
///
/// </summary>
public class TranClass
{
public string From { get; set; }
public string To { get; set; }
public List<Trans_result> Trans_result { get; set; }
}
/// <summary>
///
/// </summary>
public class Trans_result
{
/// <summary>
/// 原文
/// </summary>
public string src { get; set; }
/// <summary>
/// 翻译后的值
/// </summary>
public string dst { get; set; }
} /*
{"from":"zh","to":"en","trans_result":[{"src":"\u4e2d\u56fd","dst":"China"}]}
*/
}
csharp:百度翻译的更多相关文章
- Android开发学习之路-机器学习库(图像识别)、百度翻译
对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...
- C# 调用百度翻译Api
这是简单的界面.用的是wpf,winform也可以 具体的操作类 public partial class MainWindow : Window { string url = "" ...
- 基于百度翻译API开发属于自己的翻译工具
你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
- 百度翻译&&金山词霸API
#/usr/bin/env python3 #coding=utf8 """百度翻译api功能实现函数,本模块基于Python3.x实现,getTransResult(q ...
- 百度翻译word-wrap,页面错乱原因查找过程(已修复)
今天群里有人发问, 进入百度翻译http://fanyi.baidu.com/#auto/zh/, 输入word-wrap,发现页面错乱. 寻找错乱原因. 上图 开始查找原因: 1.从请求入手 从ch ...
- 百度翻译API
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- Asp.NET调用百度翻译
Asp.NET调用百度翻译,图示: HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
随机推荐
- Android xml 格式 随笔
打包的时候Android xml文件会由字符格式(utf-8编码)转换为二进制格式.具体如:http://blog.csdn.net/jiangwei0910410003/article/detail ...
- 点击短信中的url打开某个应用
实现功能: 短信内容中含有url(例如,http://youngo.com/app/),点击后打开apk 遗留问题: 点击url后,会出现选择框,让用户选择是用浏览器打开还是用该apk打开----没有 ...
- saiku 分布式实践
saiku比较吃内存,一旦人多了,那么内存可能不够,所以会考虑主从结构,分担压力.为了保证数据的稳定性,也会有类似的考虑,那么问题来了,如何实现saiku的分布式搭建哪? 我阅读了一些国内的文章,没有 ...
- pentaho saiku 安装全过程
公司希望也开发一套多维分析系统,以解决运营/产品无休止的需求和技术人力不足的矛盾! 一.开发选型: 一.BIRT:易用性差,所以没再使用 二.JasperReport+ireport:文档收费,不支持 ...
- Mac终端常用命令收集
删除非空目录 rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 终端修改hosts文件 sudo vi /etc/hosts 切换到su ...
- Android Studio配置和使用OpenCV3.x,不需要OpencvManager
转载声明,本文转自CSDN:http://blog.csdn.net/qq_22033759/article/details/51156121 ps:本来在贴吧上有人问,想自己写的,但时间有限,当初自 ...
- HMM MEMM & label bias
(http://blog.csdn.net/xum2008/article/details/38147425) 隐马尔科夫模型(HMM): 图1. 隐马尔科夫模型 隐马尔科夫模型的缺点: 1.HMM只 ...
- Chart 点击获取坐标
private void chart2_MouseMove(object sender, MouseEventArgs e) { if (!this.DesignMode) { ].AxisX.Sca ...
- java 动态创建数据库和动态连接数据库
项目中有一个需求要动态创建数据库并且要动态连接数据库,本来以为还很难实现呢,在网上找了好久,都不是很理想,最后看到有人说创建数据库时,先连接到任意一个数据库,获得连接后用createStatement ...
- Legolas工业自动化平台入门(二)数据响应动作
在上一篇文章Legolas工业自动化平台入门(一)搭建应用里,我们简单地提到了"动作"的概念.不清楚的童鞋不要着急,这篇我们就来介绍如何在Legolas平台上添加动作,并应用动作. ...