Programmer & artist.

命令行查单词

需求来源

这两天做一个拍卖网站的项目,偶尔会有一些单词想不起来,顺手花五分钟就写了一个查单词的,命令行简单点就它了。

源码

class result
    {
        //{"from":"en","to":"zh","trans_result":[{"src":"find","dst":"\u627e\u5230"}]}
        public string from { get; set; }
        public string to { get; set; }
        public trans_result[] trans_result { get; set; }
    }

    class trans_result
    {
        public string src { get; set; }
        public string dst { get; set; }
    }
    class Program
    {
        private static String DecodeUnicode(String dataStr)
        {
            Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
            return reg.Replace(dataStr, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });


        }
        static void Main(string[] args)
        {
            if (args.Length>0)
            {
                string s1 = args[0].ToString();
                string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", "这里填写百度appid", s1, "auto", "auto"); 
                WebClient wc = new WebClient();
                result r = JsonConvert.DeserializeObject<result>(wc.DownloadString(url));
                Console.WriteLine(DecodeUnicode(r.trans_result[0].dst));
                Console.WriteLine("......");
            }
            else
            {
                while (true)
                {

                    string s = Console.ReadLine();
                    if (s == "over" || s == "quit" || s == "exit")
                    {
                        break;
                    }
                    string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", "这里填写百度appid", s, "auto", "auto"); 
            WebClient wc
= new WebClient();
            result r
= JsonConvert.DeserializeObject<result>(wc.DownloadString(url));
            Console.WriteLine(DecodeUnicode(r.trans_result[
0].dst)); Console.WriteLine("......"); } }
            Console.WriteLine(
"退出查单词,感谢使用!");
          }
        }

没什么技术难度,方便生活而已,最后想在命令行直接调用,别忘配置path环境变量,大约就是酱

 

posted @ 2014-04-15 22:40  未命铭  阅读(693)  评论(1编辑  收藏  举报

Copyright ©2015 未命铭