自己写的一个英汉互翻译的词典,附源码
支持:
1.英汉互翻译
2.历史记录
3.句子翻译
4.CTRL+F 自动呼出
先上图:
关键源代码(很简单):
- private void FrmTranslation_Load(object sender, EventArgs e)
- {
- this.txtWord.Focus();
- this.txtWord.Select();
- }
- void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- this.txtResult.Text = Result;
- }
- void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- var word = this.txtWord.Text.Trim();
- System.Threading.Thread.Sleep(1000);
- if (this.txtWord.Text.Trim() != word)
- {
- return;
- }
- Result = IcibaAPI.Translate(this.txtWord.Text.Trim());
- }
- private void txtWork_Click(object sender, EventArgs e)
- {
- this.txtWord.SelectAll();
- }
- private void txtWork_TextChanged(object sender, EventArgs e)
- {
- Result = "";
- var word = this.txtWord.Text.Trim();
- if (string.IsNullOrWhiteSpace(word))
- {
- this.txtResult.Text = "";
- return;
- }
- bw = new BackgroundWorker();
- bw.DoWork += bw_DoWork;
- bw.RunWorkerCompleted += bw_RunWorkerCompleted;
- bw.RunWorkerAsync();
- }
- internal void FocusInput()
- {
- this.txtWord.Focus();
- //this.txtWord.Select();
- }
API源码:
- public class IcibaAPI
- {
- private static string APIURL = "http://www.iciba.com/index.php?a=getWordMean>c=search>list=1>word={0}>_={1}";
- static MySqlConnection con;
- static IcibaAPI()
- {
- string conn = "Data Source=127.0.0.1;User ID=root;Password=123456;DataBase=yinhan_dict";
- con = new MySqlConnection(conn);
- }
- public static string Translate(string words)
- {
- string newword = words;
- string result = "";
- string en = "";
- string am = "";
- string read = "";
- using (System.Net.WebClient web = new System.Net.WebClient())
- {
- web.Encoding = Encoding.UTF8;
- words = System.Web.HttpUtility.UrlEncode(words);
- var url = string.Format(APIURL, words, DateTime.Now.Ticks);
- var apiResult = Regex.Unescape(web.DownloadString(url));
- //{"errno":404,"errmsg":"无释义"}
- if (apiResult.Contains("404"))
- {
- return "Not Found";
- }
- JObject json = JObject.Parse(apiResult);
- string translate_type = json.SelectToken("baesInfo.translate_type").ToString();
- if (translate_type == "2")
- {
- result = json.SelectToken("baesInfo.translate_result").ToString();
- }
- else if (translate_type == "1")
- {
- en = json.SelectToken("baesInfo.symbols[0].ph_en").ToStringOrEmpty();
- am = json.SelectToken("baesInfo.symbols[0].ph_am").ToStringOrEmpty();
- if (am != "" || en != "")
- {
- read = "英[" + en + "] 美[" + am + "]\r\n";
- }
- //[0].means
- var parts = json.SelectToken("baesInfo.symbols[0].parts");
- foreach (var part in parts)
- {
- var means = part.SelectToken("means");
- var meanString = "";
- foreach (var mean in means)
- {
- meanString += mean.ToString() + ",";
- }
- result += part.SelectToken("part") + " " + meanString + "\r\n";
- }
- }
- addHistory(newword, result, en, am);
- return read + result;
- }
- }
- public static void addHistory(string word, string means, string en, string am)
- {
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- word = word.Replace("'","''");
- string sql = "select 1 from history where word='" + word + "'";
- MySqlCommand cmd = new MySqlCommand(sql, con);
- Object obj = cmd.ExecuteScalar();
- if (obj != null)
- {
- sql = "update history set count=count+1,time = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where word='" + word + "'";
- }
- else
- {
- sql = "insert into history values('" + word + "',1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + means + "','" + en + "','" + am + "')";
- }
- cmd = new MySqlCommand(sql, con);
- cmd.ExecuteNonQuery();
- }
- public static Dictionary<string, Dict> History(int page, int count)
- {
- Dictionary<string, Dict> result = new Dictionary<string, Dict>();
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- 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 + "";
- MySqlCommand cmd = new MySqlCommand(sql, con);
- MySqlDataReader dr = cmd.ExecuteReader();
- //操作dr中的数据
- while (dr.HasRows)
- {
- if (dr.Read())
- {
- result.Add(dr["word"].ToString(), new Dict()
- {
- word = dr["word"].ToString(),
- means = dr["means"].ToString(),
- en = dr["en"].ToString(),
- am = dr["am"].ToString(),
- });
- }
- else
- {
- break;
- }
- }
- dr.Close();
- con.Close();
- return result;
- }
- }
无网络情况下,本地翻译:
- public class DictAPI
- {
- static MySqlConnection con;
- static DictAPI()
- {
- string conn = "Data Source=127.0.0.1;User ID=root;Password=123456;DataBase=yinhan_dict";
- con = new MySqlConnection(conn);
- }
- public static Dictionary<string, string> History(int page, int count)
- {
- Dictionary<string, string> result = new Dictionary<string, string>();
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- string sql = "select b.word,b.explain from history a left join mydict b on a.word = b.word limit " + (page - 1) * count + "," + count + "";
- MySqlCommand cmd = new MySqlCommand(sql, con);
- MySqlDataReader dr = cmd.ExecuteReader();
- //操作dr中的数据
- while (dr.HasRows)
- {
- if (dr.Read())
- {
- result.Add(dr["word"].ToString(), dr["explain"].ToString());
- }
- else
- {
- break;
- }
- }
- dr.Close();
- con.Close();
- return result;
- }
- public static List<string> Select(string word)
- {
- List<string> result = new List<string>();
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- MySqlCommand cmd = new MySqlCommand("select * from mydict where word like '" + word + "%'", con);
- MySqlDataReader dr = cmd.ExecuteReader();
- //操作dr中的数据
- while (dr.HasRows)
- {
- if (dr.Read())
- {
- result.Add(dr["word"].ToString());
- }
- }
- dr.Close();
- con.Close();
- return result;
- }
- public static string Translate(string word)
- {
- string result = "";
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- MySqlCommand cmd = new MySqlCommand("select * from mydict where word like '" + word + "%'", con);
- MySqlDataReader dr = cmd.ExecuteReader();
- //操作dr中的数据
- while (dr.HasRows)
- {
- if (dr.Read())
- {
- result = dr["explain"].ToString();
- string db_word = dr["word"].ToString().Trim();
- dr.Close();
- if (word == db_word)
- {
- addHistory(word);
- }
- break;
- }
- }
- con.Close();
- return result;
- }
- public static void addHistory(string word)
- {
- string sql = "select 1 from history where word='" + word + "'";
- MySqlCommand cmd = new MySqlCommand(sql, con);
- Object obj = cmd.ExecuteScalar();
- if (obj != null)
- {
- sql = "update history set count=count+1,time = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where word='" + word + "'";
- }
- else
- {
- sql = "insert into history values('" + word + "',1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
- }
- cmd = new MySqlCommand(sql, con);
- cmd.ExecuteNonQuery();
- }
- }
完整源代码:
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP