天天看點

C# 有道API翻譯 查詢單詞詳細資訊

有道雲官方文檔

有道雲翻譯API簡介:

http://ai.youdao.com/docs/doc-trans-api.s#p01

有道雲C#Demo :

http://ai.youdao.com/docs/doc-trans-api.s#p08

查詢單詞資訊

在有道雲的Demo中,已經很完整的給出了相應的代碼~

但是針對是的英-漢等翻譯,如何隻查詢單個單詞的詳細資訊,包含單詞的翻譯/音标/釋義等?例如下圖示例資訊:

C# 有道API翻譯 查詢單詞詳細資訊

下面修改後封裝的查詢單詞詳細資訊服務(僅供參考): 

1     /// <summary>
 2     /// 有道詞典API
 3     /// </summary>
 4     internal class YouDaoApiService
 5     {
 6         const string AppKey = "131b76a4ee1ecd13";//AppKey和AppSecret是本人@Winter申請的賬号,僅供測試使用
 7         const string LangEn = "en";
 8         const string AppSecret = "KX9hLrgSMhfKkvIqS6nhwtwMcRymJqEA";
 9 
10         public static async Task<YouDaoTranslationResponse> GetTranslatioAsync(string queryText, string from = LangEn, string to = LangEn)
11         {
12             var requestUrl = GetRequestUrl(queryText, from, to);
13 
14             WebRequest translationWebRequest = WebRequest.Create(requestUrl);
15 
16             var response = await translationWebRequest.GetResponseAsync();
17 
18             using (Stream stream = response.GetResponseStream())
19             {
20                 using (StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException("有道Api查詢出錯!"), Encoding.GetEncoding("utf-8")))
21                 {
22                     string result = reader.ReadToEnd();
23                     var youDaoTranslationResponse = JsonConvert.DeserializeObject<YouDaoTranslationResponse>(result);
24 
25                     return youDaoTranslationResponse;
26                 }
27             }
28         }
29 
30         private static string GetRequestUrl(string queryText, string from, string to)
31         {
32             string salt = DateTime.Now.Millisecond.ToString();
33 
34             MD5 md5 = new MD5CryptoServiceProvider();
35             string md5Str = AppKey + queryText + salt + AppSecret;
36             byte[] output = md5.ComputeHash(Encoding.UTF8.GetBytes(md5Str));
37             string sign = BitConverter.ToString(output).Replace("-", "");
38 
39             var requestUrl = string.Format(
40                 "http://openapi.youdao.com/api?appKey={0}&q={1}&from={2}&to={3}&sign={4}&salt={5}",
41                 AppKey,
42                 HttpUtility.UrlDecode(queryText, System.Text.Encoding.GetEncoding("UTF-8")),
43                 from, to, sign, salt);
44 
45             return requestUrl;
46         }
47     }      

 序列化解析的資料類:

C# 有道API翻譯 查詢單詞詳細資訊
C# 有道API翻譯 查詢單詞詳細資訊
1     [DataContract]
 2     public class YouDaoTranslationResponse
 3     {
 4         [DataMember(Name = "errorCode")]
 5         public string ErrorCode { get; set; }
 6 
 7         [DataMember(Name = "query")]
 8         public string QueryText { get; set; }
 9 
10         [DataMember(Name = "speakUrl")]
11         public string InputSpeakUrl { get; set; }
12 
13         [DataMember(Name = "tSpeakUrl")]
14         public string TranslationSpeakUrl { get; set; }
15 
16         /// <summary>
17         /// 首選翻譯
18         /// </summary>
19         [DataMember(Name = "translation")]
20         public List<string> FirstTranslation { get; set; }
21 
22         /// <summary>
23         /// 基本釋義
24         /// </summary>
25         [DataMember(Name = "basic")]
26         public TranslationBasicData BasicTranslation { get; set; }
27 
28         ///// <summary>
29         ///// 網絡釋義,該結果不一定存在,暫時不使用
30         ///// </summary>
31         //[DataMember(Name = "web")]
32         //public TranslationWebData WebTranslation { get; set; }
33     }
34 
35     /// <summary>
36     /// 基本釋義
37     /// </summary>
38     [DataContract]
39     public class TranslationBasicData
40     {
41         [DataMember(Name = "phonetic")]
42         public string Phonetic { get; set; }
43 
44         /// <summary>
45         /// 英式發音
46         /// </summary>
47         [DataMember(Name = "uk-phonetic")]
48         public string UkPhonetic { get; set; }
49 
50         /// <summary>
51         /// 美式發音
52         /// </summary>
53         [DataMember(Name = "us-phonetic")]
54         public string UsPhonetic { get; set; }
55 
56         /// <summary>
57         /// 翻譯
58         /// </summary>
59         [DataMember(Name = "explains")]
60         public List<string> Explains { get; set; }
61     }
62 
63     /// <summary>
64     /// 網絡釋義
65     /// </summary>
66     [DataContract]
67     public class TranslationWebData
68     {
69         [DataMember(Name = "key")]
70         public string Key { get; set; }
71 
72         [DataMember(Name = "value")]
73         public List<string> Explains { get; set; }
74     }      

View Code

作者:

唐宋元明清2188

出處:

http://www.cnblogs.com/kybs0/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須在文章頁面給出原文連接配接,否則保留追究法律責任的權利。

繼續閱讀