[C#]使用 Bing Sharp 來做 Bing 翻譯[转]

1. 簡介

在之前,曾發過此篇文章,[Windows Mobile]修練筆記(9) - 在智慧型裝置專案、Windows Form 應用程式與 Widget 使用 Bing API 達成 Bing 圖片搜尋,文章中,我們必須對 Bing API 所回傳的 XML、JSON 或 Soap 做處理,而 Bing Sharp 的出現,就省去了這道手續。

Bing Sharp 是一個包含 Bing API 2.x XML 請求的 C# API,提供了與 Bing API 互動的物件模型,使用了 Bing Sharp,則不必去處理 XML、JSON、Soap。

*註 : Bing Sharp 作者為 Joseph Guadagno,是 Micosoft Visual C# MVP

2. 使用 Bing Sharp

(1) 下載

下載網址

image  

(2) 加入參考

imageimage

在程式中 using

1using Bing;

(3) 撰寫程式

要如何使用 Bing Sharp 來做 翻譯,作者有提供 help 檔案可以下載。而要使用 Bing,就必須要有 AppID,申請的方式請參考 [Windows Mobile]修練筆記(9) - 在智慧型裝置專案、Windows Form 應用程式與 Widget 使用 Bing API 達成 Bing 圖片搜尋

image

以下是程式碼

01using System;
02using System.Collections.Generic;
03using System.ComponentModel;
04using System.Data;
05using System.Drawing;
06using System.Linq;
07using System.Text;
08using System.Windows.Forms;
09 
10using Bing;
11 
12namespace WindowsFormsBing
13{
14    public partial class Form1 : Form
15    {
16        public Form1()
17        {
18            InitializeComponent();
19 
20            AddItem(cboFrom);
21            AddItem(cboTo);
22        }
23 
24        string AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4";
25 
26        private void btnTranslate_Click(object sender, EventArgs e)
27        {
28            string query = txtQuery.Text.Trim();
29            this.txtResult.Text = String.Empty;
30 
31            if (string.IsNullOrEmpty(query))
32            {
33                MessageBox.Show("請輸入要翻譯的文字");
34                return;
35            }
36 
37            if (this.cboFrom.SelectedIndex < 0)
38            {
39                MessageBox.Show("請選擇來源語系");
40                return;
41            }
42 
43            if (this.cboTo.SelectedIndex < 0)
44            {
45                MessageBox.Show("請選擇目的語系");
46                return;
47            }
48 
49            SearchRequest searchRequest = new SearchRequest() { AppId = AppId, Query = query, Market = "en-US" };
50 
51            TranslationRequest translationRequest = new TranslationRequest();  // 宣告 TranslationRequest 並實例化
52            translationRequest.SourceLanguage = cboFrom.Text.ToString();  // 來源語系
53            translationRequest.TargetLanguage = cboTo.Text.ToString();  // 目的語系
54 
55            TranslationResponse response = API.Translation(searchRequest, translationRequest);
56 
57            if (response.TranslationResults.Count > 0)  // 翻譯回傳值大於0筆時
58            {
59                txtResult.Text = response.TranslationResults[0].TranslatedTerm;
60            }
61        }
62 
63        public void AddItem(ComboBox cbo)
64        {
65            cbo.Items.Add("Ar");    // Arabic
66            cbo.Items.Add("zh-CHS");// Simplified Chinese
67            cbo.Items.Add("zh-CHT");// Traditional Chinese
68            cbo.Items.Add("Nl");    // Dutch
69            cbo.Items.Add("En");    // English
70            cbo.Items.Add("Fr");    // French
71            cbo.Items.Add("De");    // German
72            cbo.Items.Add("It");    // Italian
73            cbo.Items.Add("Ja");    // Japanese
74            cbo.Items.Add("Ko");    // Korean
75            cbo.Items.Add("Pl");    // Polish
76            cbo.Items.Add("Pt");    // Portuguese
77            cbo.Items.Add("Ru");    // Russian
78            cbo.Items.Add("Es");    //"Spanish
79        }
80    }
81}

(4) 執行結果

image

3. 檔案下載

WindowsFormsBing.rar

4. 附註

會使用 Bing Sharp 的原因是因為 Plurk 網友詢問這是微軟新出的程式語言嗎? 而且看到大陸網站上有關於 Bing Sharp 的介紹,不過總覺得這應該不是微軟官方的東西,反正好用的話,大家就會使用了devil_smile.gif

原文:http://www.dotblogs.com.tw/chou/archive/2009/08/11/10002.aspx

转载于:https://www.cnblogs.com/zqonline/archive/2011/03/31/2000998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值