分类

链接

2018 年 3 月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
自己写的一个英汉互翻译的词典,附源码
.NET 暂无评论 阅读(557)

支持:

1.英汉互翻译

2.历史记录

3.句子翻译

4.CTRL+F 自动呼出

 

先上图:

 

 

关键源代码(很简单):

  1.     private void FrmTranslation_Load(object sender, EventArgs e)
  2.         {
  3.             this.txtWord.Focus();
  4.             this.txtWord.Select();
  5.         }
  6.  
  7.         void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  8.         {
  9.             this.txtResult.Text = Result;
  10.         }
  11.  
  12.         void bw_DoWork(object sender, DoWorkEventArgs e)
  13.         {
  14.             var word = this.txtWord.Text.Trim();
  15.  
  16.             System.Threading.Thread.Sleep(1000);
  17.  
  18.             if (this.txtWord.Text.Trim() != word)
  19.             {
  20.                 return;
  21.             }
  22.  
  23.             Result = IcibaAPI.Translate(this.txtWord.Text.Trim());
  24.         }
  25.  
  26.         private void txtWork_Click(object sender, EventArgs e)
  27.         {
  28.             this.txtWord.SelectAll();
  29.  
  30.  
  31.         }
  32.         private void txtWork_TextChanged(object sender, EventArgs e)
  33.         {
  34.             Result = "";
  35.             var word = this.txtWord.Text.Trim();
  36.             if (string.IsNullOrWhiteSpace(word))
  37.             {
  38.                 this.txtResult.Text = "";
  39.                 return;
  40.             }
  41.             bw = new BackgroundWorker();
  42.             bw.DoWork += bw_DoWork;
  43.             bw.RunWorkerCompleted += bw_RunWorkerCompleted;
  44.             bw.RunWorkerAsync();
  45.  
  46.         }
  47.  
  48.  
  49.         internal void FocusInput()
  50.         {
  51.             this.txtWord.Focus();
  52.             //this.txtWord.Select();
  53.         }

 

API源码:

  1.  public class IcibaAPI
  2.     {
  3.         private static string APIURL = "http://www.iciba.com/index.php?a=getWordMean>c=search>list=1>word={0}>_={1}";
  4.  
  5.  
  6.         static MySqlConnection con;
  7.         static IcibaAPI()
  8.         {
  9.             string conn = "Data Source=127.0.0.1;User ID=root;Password=123456;DataBase=yinhan_dict";
  10.             con = new MySqlConnection(conn);
  11.  
  12.         }
  13.  
  14.  
  15.  
  16.         public static string Translate(string words)
  17.         {
  18.             string newword = words;
  19.             string result = "";
  20.             string en = "";
  21.             string am = "";
  22.             string read = "";
  23.  
  24.  
  25.             using (System.Net.WebClient web = new System.Net.WebClient())
  26.             {
  27.                 web.Encoding = Encoding.UTF8;
  28.                 words = System.Web.HttpUtility.UrlEncode(words);
  29.                 var url = string.Format(APIURL, words, DateTime.Now.Ticks);
  30.  
  31.                 var apiResult = Regex.Unescape(web.DownloadString(url));
  32.                 //{"errno":404,"errmsg":"无释义"}
  33.                 if (apiResult.Contains("404"))
  34.                 {
  35.                     return "Not Found";
  36.                 }
  37.  
  38.  
  39.                 JObject json = JObject.Parse(apiResult);
  40.  
  41.                 string translate_type = json.SelectToken("baesInfo.translate_type").ToString();
  42.                 if (translate_type == "2")
  43.                 {
  44.                     result = json.SelectToken("baesInfo.translate_result").ToString();
  45.                 }
  46.                 else if (translate_type == "1")
  47.                 {
  48.                     en = json.SelectToken("baesInfo.symbols[0].ph_en").ToStringOrEmpty();
  49.                     am = json.SelectToken("baesInfo.symbols[0].ph_am").ToStringOrEmpty();
  50.                     if (am != "" || en != "")
  51.                     {
  52.                         read = "英[" + en + "]  美[" + am + "]\r\n";
  53.                     }
  54.                     //[0].means
  55.                     var parts = json.SelectToken("baesInfo.symbols[0].parts");
  56.                     foreach (var part in parts)
  57.                     {
  58.                         var means = part.SelectToken("means");
  59.                         var meanString = "";
  60.                         foreach (var mean in means)
  61.                         {
  62.                             meanString += mean.ToString() + ",";
  63.                         }
  64.                         result += part.SelectToken("part") + " " + meanString + "\r\n";
  65.                     }
  66.  
  67.                 }
  68.  
  69.                 addHistory(newword, result, en, am);
  70.  
  71.                 return read + result;
  72.             }
  73.         }
  74.  
  75.  
  76.         public static void addHistory(string word, string means, string en, string am)
  77.         {
  78.             if (con.State != System.Data.ConnectionState.Open)
  79.             {
  80.                 con.Open();
  81.             }
  82.             word = word.Replace("'","''");
  83.             string sql = "select 1 from history where word='" + word + "'";
  84.             MySqlCommand cmd = new MySqlCommand(sql, con);
  85.             Object obj = cmd.ExecuteScalar();
  86.             if (obj != null)
  87.             {
  88.                 sql = "update history set count=count+1,time = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where word='" + word + "'";
  89.             }
  90.             else
  91.             {
  92.                 sql = "insert into history values('" + word + "',1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + means + "','" + en + "','" + am + "')";
  93.             }
  94.  
  95.             cmd = new MySqlCommand(sql, con);
  96.             cmd.ExecuteNonQuery();
  97.         }
  98.  
  99.  
  100.         public static Dictionary<string, Dict> History(int page, int count)
  101.         {
  102.             Dictionary<string, Dict> result = new Dictionary<string, Dict>();
  103.             if (con.State != System.Data.ConnectionState.Open)
  104.             {
  105.                 con.Open();
  106.             }
  107.  
  108.             string sql = "select word,means,en,am from history where time >'" + DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss") + "' order by time desc limit " + (page - 1) * count + "," + count + "";
  109.  
  110.             MySqlCommand cmd = new MySqlCommand(sql, con);
  111.             MySqlDataReader dr = cmd.ExecuteReader();
  112.             //操作dr中的数据
  113.             while (dr.HasRows)
  114.             {
  115.                 if (dr.Read())
  116.                 {
  117.                     result.Add(dr["word"].ToString(), new Dict()
  118.                     {
  119.                         word = dr["word"].ToString(),
  120.                         means = dr["means"].ToString(),
  121.                         en = dr["en"].ToString(),
  122.                         am = dr["am"].ToString(),
  123.                     });
  124.                 }
  125.                 else
  126.                 {
  127.                     break;
  128.                 }
  129.             }
  130.  
  131.             dr.Close();
  132.             con.Close();
  133.             return result;
  134.         }
  135.     }

无网络情况下,本地翻译:

  1.   public class DictAPI
  2.     {
  3.         static MySqlConnection con;
  4.         static DictAPI()
  5.         {
  6.             string conn = "Data Source=127.0.0.1;User ID=root;Password=123456;DataBase=yinhan_dict";
  7.             con = new MySqlConnection(conn);
  8.  
  9.         }
  10.  
  11.         public static Dictionary<string, string> History(int page, int count)
  12.         {
  13.             Dictionary<string, string> result = new Dictionary<string, string>();
  14.             if (con.State != System.Data.ConnectionState.Open)
  15.             {
  16.                 con.Open();
  17.             }
  18.  
  19.             string sql = "select b.word,b.explain from history a left join mydict b on a.word = b.word limit " + (page - 1) * count + "," + count + "";
  20.  
  21.             MySqlCommand cmd = new MySqlCommand(sql, con);
  22.             MySqlDataReader dr = cmd.ExecuteReader();
  23.             //操作dr中的数据
  24.             while (dr.HasRows)
  25.             {
  26.                 if (dr.Read())
  27.                 {
  28.                     result.Add(dr["word"].ToString(), dr["explain"].ToString());
  29.                 }
  30.                 else
  31.                 {
  32.                     break;
  33.                 }
  34.             }
  35.  
  36.             dr.Close();
  37.             con.Close();
  38.             return result;
  39.         }
  40.  
  41.         public static List<string> Select(string word)
  42.         {
  43.             List<string> result = new List<string>();
  44.             if (con.State != System.Data.ConnectionState.Open)
  45.             {
  46.                 con.Open();
  47.             }
  48.  
  49.             MySqlCommand cmd = new MySqlCommand("select * from mydict where word like '" + word + "%'", con);
  50.             MySqlDataReader dr = cmd.ExecuteReader();
  51.             //操作dr中的数据
  52.             while (dr.HasRows)
  53.             {
  54.                 if (dr.Read())
  55.                 {
  56.                     result.Add(dr["word"].ToString());
  57.                 }
  58.             }
  59.  
  60.             dr.Close();
  61.             con.Close();
  62.             return result;
  63.         }
  64.  
  65.         public static string Translate(string word)
  66.         {
  67.             string result = "";
  68.             if (con.State != System.Data.ConnectionState.Open)
  69.             {
  70.                 con.Open();
  71.             }
  72.  
  73.             MySqlCommand cmd = new MySqlCommand("select * from mydict where word like '" + word + "%'", con);
  74.             MySqlDataReader dr = cmd.ExecuteReader();
  75.             //操作dr中的数据
  76.             while (dr.HasRows)
  77.             {
  78.                 if (dr.Read())
  79.                 {
  80.                     result = dr["explain"].ToString();
  81.  
  82.                     string db_word = dr["word"].ToString().Trim();
  83.  
  84.                     dr.Close();
  85.  
  86.                     if (word == db_word)
  87.                     {
  88.                         addHistory(word);
  89.                     }
  90.  
  91.                     break;
  92.                 }
  93.             }
  94.  
  95.             con.Close();
  96.             return result;
  97.  
  98.         }
  99.  
  100.         public static void addHistory(string word)
  101.         {
  102.             string sql = "select 1 from history where word='" + word + "'";
  103.             MySqlCommand cmd = new MySqlCommand(sql, con);
  104.             Object obj = cmd.ExecuteScalar();
  105.             if (obj != null)
  106.             {
  107.                 sql = "update history set count=count+1,time = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where word='" + word + "'";
  108.             }
  109.             else
  110.             {
  111.                 sql = "insert into history values('" + word + "',1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
  112.             }
  113.  
  114.             cmd = new MySqlCommand(sql, con);
  115.             cmd.ExecuteNonQuery();
  116.         } 
  117.     }

 

完整源代码:

MyTranslation.zip

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:自己写的一个英汉互翻译的词典,附源码 | Bruce's Blog

发表评论

留言无头像?