zoukankan      html  css  js  c++  java
  • C# 有道API翻译

    原文: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中,已经很完整的给出了相应的代码~

    但是针对是的英-汉等翻译,如何只查询单个单词的详细信息,包含单词的翻译/音标/释义等?例如下图示例信息:

     

    下面修改后封装的查询单词详细信息服务(仅供参考): 

    复制代码
     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     }
    复制代码

     注:值得一提的是,查询单词信息,en->en路径有道提供的数据不完整,会返回301错误码。联系有道开发后,提供的方案是auto->zhs。

    序列化解析的数据类:

    [DataContract]
        public class YouDaoTranslationResponse
        {
            [DataMember(Name = "errorCode")]
            public string ErrorCode { get; set; }
     
            [DataMember(Name = "query")]
            public string QueryText { get; set; }
     
            [DataMember(Name = "speakUrl")]
            public string InputSpeakUrl { get; set; }
     
            [DataMember(Name = "tSpeakUrl")]
            public string TranslationSpeakUrl { get; set; }
     
            /// <summary>
            /// 首选翻译
            /// </summary>
            [DataMember(Name = "translation")]
            public List<string> FirstTranslation { get; set; }
     
            /// <summary>
            /// 基本释义
            /// </summary>
            [DataMember(Name = "basic")]
            public TranslationBasicData BasicTranslation { get; set; }
     
            ///// <summary>
            ///// 网络释义,该结果不一定存在,暂时不使用
            ///// </summary>
            //[DataMember(Name = "web")]
            //public TranslationWebData WebTranslation { get; set; }
        }
     
        /// <summary>
        /// 基本释义
        /// </summary>
        [DataContract]
        public class TranslationBasicData
        {
            [DataMember(Name = "phonetic")]
            public string Phonetic { get; set; }
     
            /// <summary>
            /// 英式发音
            /// </summary>
            [DataMember(Name = "uk-phonetic")]
            public string UkPhonetic { get; set; }
     
            /// <summary>
            /// 美式发音
            /// </summary>
            [DataMember(Name = "us-phonetic")]
            public string UsPhonetic { get; set; }
     
            /// <summary>
            /// 翻译
            /// </summary>
            [DataMember(Name = "explains")]
            public List<string> Explains { get; set; }
        }
     
        /// <summary>
        /// 网络释义
        /// </summary>
        [DataContract]
        public class TranslationWebData
        {
            [DataMember(Name = "key")]
            public string Key { get; set; }
     
            [DataMember(Name = "value")]
            public List<string> Explains { get; set; }
        }
     
    分类: .NET-C#学习

    有道云官方文档

    有道云翻译API简介:http://ai.youdao.com/docs/doc-trans-api.s#p01

    有道云C#Demo : http://ai.youdao.com/docs/doc-trans-api.s#p08

    查询单词信息

    在有道云的Demo中,已经很完整的给出了相应的代码~

    但是针对是的英-汉等翻译,如何只查询单个单词的详细信息,包含单词的翻译/音标/释义等?例如下图示例信息:

     

    下面修改后封装的查询单词详细信息服务(仅供参考): 

    复制代码
     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     }
    复制代码

     注:值得一提的是,查询单词信息,en->en路径有道提供的数据不完整,会返回301错误码。联系有道开发后,提供的方案是auto->zhs。

    序列化解析的数据类:

     View Code
     
    分类: .NET-C#学习
  • 相关阅读:
    springboot-项目属性配置
    网络编程
    HttpServlet---getLastModified与缓存
    TCP的三次握手和四次挥手
    数据库事务隔离级别-- 脏读、幻读、不可重复读
    js 数组,字符串,json互相转换
    表格单元格td设置宽度无效的解决办法
    判断IE版本的HTML语句详解,如:<!--[if IE 9]> 仅IE9可识别 <![endif]-->
    IE主页被篡改为360导航终极处理办法
    前端常见问题
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/12495060.html
Copyright © 2011-2022 走看看