private void InitData() { string sql = "select * from word"; DataTable dt = SqliteHelper.GetDataTable(sql); if (dt != null && dt.Rows.Count > 0) { List <EnglishWord> list = EnglishWord.DataTableToList(dt); if (list != null && list.Count > 0) { foreach (EnglishWord word in list) { if (word != null && !string.IsNullOrEmpty(word.word) && !dicAllwords.ContainsKey(word.word)) { dicAllwords.Add(word.word, word); } } } } }
private void btAnalyze_Click(object sender, EventArgs e) { if (File.Exists(tbPath.Text)) { string[] sarrs = File.ReadAllLines(tbPath.Text, Encoding.Default); Dictionary <string, string> allwords = new Dictionary <string, string>(); Dictionary <string, string> notTrans = new Dictionary <string, string>(); foreach (string srow in sarrs) { if (!string.IsNullOrEmpty(srow)) { string[] words = srow.Split(' '); if (words != null && words.Length > 0) { foreach (string word in words) { string s = word.TrimEnd('.').TrimEnd(',').TrimEnd(';').TrimEnd('?').TrimEnd(':'); if (!allwords.ContainsKey(s)) { allwords.Add(s, s); } } } } } List <EnglishWord> listExists = new List <EnglishWord>(); foreach (string key in allwords.Keys) { if (dicAllwords.ContainsKey(key)) { if (!listExists.Contains(dicAllwords[key])) { listExists.Add(dicAllwords[key]); } } else if (dicAllwords.ContainsKey(key.ToLower())) { if (!listExists.Contains(dicAllwords[key.ToLower()])) { listExists.Add(dicAllwords[key.ToLower()]); } } else { string prototype1 = string.Empty; string prototype2 = string.Empty; if (key.EndsWith("ing")) { prototype1 = GetRemoveSuffixString(key, "ing"); } else if (key.EndsWith("s")) { prototype1 = GetRemoveSuffixString(key, "s"); prototype2 = GetRemoveSuffixString(key, "es"); } else if (key.EndsWith("ed")) { prototype1 = GetRemoveSuffixString(key, "ed"); prototype2 = GetRemoveSuffixString(key, "d"); } else if (key.EndsWith("er")) { prototype1 = GetRemoveSuffixString(key, "er"); prototype2 = GetRemoveSuffixString(key, "r"); } else if (key.EndsWith("est")) { prototype1 = GetRemoveSuffixString(key, "est"); prototype2 = GetRemoveSuffixString(key, "st"); } if (!string.IsNullOrEmpty(prototype1) && dicAllwords.ContainsKey(prototype1)) { if (!listExists.Contains(dicAllwords[prototype1])) { listExists.Add(dicAllwords[prototype1]); } } else if (!string.IsNullOrEmpty(prototype2) && dicAllwords.ContainsKey(prototype2)) { if (!listExists.Contains(dicAllwords[prototype2])) { listExists.Add(dicAllwords[prototype2]); } } else { if (Regex.IsMatch(key, RegexStr) && !notTrans.ContainsKey(key)) { notTrans.Add(key, key); } } } } foreach (string notexistword in notTrans.Keys) { EnglishWord wordnoexist = new EnglishWord() { word = notexistword }; if (notexistword[0] >= 'A' && notexistword[0] <= 'Z') { wordnoexist.trans = "人名或者地名"; } else { wordnoexist.trans = "未识别"; } listExists.Add(wordnoexist); } dgvList.DataSource = listExists; if (cbCSV.Checked) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel File (*.xls)|*.xls"; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { OutExcel(listExists, sfd.FileName); } } } else { if (string.IsNullOrEmpty(tbPath.Text.Trim())) { MessageHelper.ShowError("请选择文件!"); } else { MessageHelper.ShowError("文件不存在!"); } } }