Form1.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace TxtDictionary
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. ProgressBar.CheckForIllegalCrossThreadCalls = false;
  19. }
  20. string filename = "";
  21. iciba ciba = new iciba();
  22. int CountInPage = 0;
  23. string subDirName;
  24. Thread th;
  25. List<Word> l_result = new List<Word>();
  26. List<Word> l_ignore = new List<Word>();
  27. Dictionary<string, Word> dicResult = new Dictionary<string, Word>();
  28. void reset()
  29. {
  30. progressBar1.Value = 0;
  31. txt_messageBox.Text = "";
  32. l_result.Clear();
  33. l_ignore.Clear();
  34. dicResult.Clear();
  35. }
  36. bool init()
  37. {
  38. if (!int.TryParse(txt_numberPerPage.Text, out CountInPage))
  39. {
  40. MessageBox.Show("请填写设置每个文件的单词数");
  41. return false;
  42. }
  43. if (txt_FileName.Text == "")
  44. {
  45. MessageBox.Show("请填写文件名");
  46. return false;
  47. }
  48. else
  49. {
  50. filename = txt_FileName.Text;
  51. }
  52. subDirName = AppDomain.CurrentDomain.BaseDirectory + @"生成结果" + DateTime.Now.ToString("yyyyMMddHHmmss");
  53. return true;
  54. }
  55. private void btn_onlyword_Click(object sender, EventArgs e)
  56. {
  57. reset();
  58. if (!init())
  59. {
  60. return;
  61. }
  62. OpenFileDialog ofd = new OpenFileDialog();
  63. ofd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  64. ofd.Multiselect = false;
  65. if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  66. {
  67. return;
  68. }
  69. this.label1.Text = ofd.FileName;
  70. this.Update();
  71. var s1 = System.IO.File.ReadAllText(label1.Text);
  72. th = new Thread(new ParameterizedThreadStart(process));
  73. th.IsBackground = true;
  74. th.Start(s1);
  75. }
  76. void process(object obj)
  77. {
  78. string strSource = obj as string;
  79. if (string.IsNullOrEmpty(strSource))
  80. {
  81. return;
  82. }
  83. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"[a-zA-Z' -]+");
  84. var ms = r.Matches(strSource);
  85. StringBuilder sbWords = new StringBuilder();
  86. foreach (System.Text.RegularExpressions.Match item in ms)
  87. {
  88. sbWords.AppendLine(item.ToString().Trim());
  89. }
  90. //写单词表
  91. Save("已查的单词表", sbWords.ToString());
  92. this.progressBar1.Maximum = ms.Count;
  93. int numberInPage = 0;
  94. int A = 0;
  95. int B = 0;
  96. StringBuilder sbNotFound = new StringBuilder();
  97. StringBuilder sbIgnore = new StringBuilder();
  98. StringBuilder sbResult = new StringBuilder();
  99. StringBuilder sbChongfu = new StringBuilder();
  100. foreach (System.Text.RegularExpressions.Match w in ms)
  101. {
  102. string wStr = w.ToString().Trim();
  103. var wd = ciba.QueryWord(wStr);
  104. if (wd != null)
  105. {
  106. if (wd.Text.Contains('\'') && cb_removeP.Checked)
  107. {
  108. sbIgnore.Append(wd.ToString());
  109. l_ignore.Add(wd);
  110. txt_messageBox.AppendText("已忽略:" + wd.Text + "\r\n");
  111. }
  112. else
  113. {
  114. txt_result.AppendText(wd.ToString());
  115. sbResult.Append(wd.ToString());
  116. l_result.Add(wd);
  117. try
  118. {
  119. dicResult.Add(wd.Text, wd);
  120. }
  121. catch (ArgumentException ex)
  122. {
  123. this.txt_messageBox.AppendText("重复:" + wd.Text + "\r\n");
  124. sbChongfu.Append(wd.ToString());
  125. }
  126. numberInPage++;
  127. }
  128. }
  129. else
  130. {
  131. sbNotFound.AppendLine(wStr);
  132. this.txt_messageBox.AppendText("未找到:" + wStr + "\r\n");
  133. }
  134. this.progressBar1.Value = this.progressBar1.Value + 1;
  135. this.lbl_n.Text = this.progressBar1.Value.ToString() + "/" + this.progressBar1.Maximum.ToString();
  136. this.Update();
  137. if (CountInPage == numberInPage)
  138. {
  139. B = B + CountInPage;
  140. Save(filename + "_" + (A + 1) + "-" + B, sbResult.ToString());
  141. txt_result.Text = "";
  142. sbResult.Clear();
  143. numberInPage = 0;
  144. A = B;
  145. }
  146. }
  147. //写结果
  148. Save(filename + "_" + (A + 1) + "-" + (A + numberInPage), sbResult.ToString());
  149. //写未找到单词
  150. Save("未找到的单词", sbNotFound.ToString());
  151. //写已经忽略的单词
  152. Save("已忽略的单词", sbIgnore.ToString());
  153. //写结果Excel文档
  154. new ExcelTool().Save(subDirName + "\\Result.xlsx", l_result, CountInPage);
  155. //写去重复的结果Excel文档
  156. new ExcelTool().Save(subDirName + "\\Result_Distinct.xlsx", dicResult.Values.ToList<Word>(), CountInPage);
  157. //l_result.Distinct();
  158. MessageBox.Show("完成!结果文件在程序所在的目录! 确定后打开目录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  159. System.Diagnostics.Process.Start("explorer", subDirName);
  160. }
  161. private void btn_Info_Click(object sender, EventArgs e)
  162. {
  163. this.txt_result.Text = @"
  164. 1.填写设置,写入每个文件的单词数,如果只要一个文件,请填写一个足够大的数字,比如99999.写入文件名。
  165. 2.选择导入文件,导入文件为单词列表,支持除'单引号 -连字符 空格 以外的任意符号分割、干扰字符可以在整理过程中去除。
  166. 3.使用iciba.com网站作为数据源。
  167. 4.全选、复制、粘贴到Excel中可以多列显示。
  168. ---作者:Hansi
  169. 2014年11月5日
  170. 修改了一下,原来的不好使了。
  171. 嘿嘿 2017年6月2日
  172. 公众号:hansi-zt
  173. 微信号:hansi7
  174. 微博:@麦田呱呱
  175. 有问题加微信慢慢聊哦~
  176. ";
  177. }
  178. private void Save(string fileName, string content)
  179. {
  180. try
  181. {
  182. string f = System.IO.Path.Combine(subDirName, fileName + ".txt");
  183. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(f)))
  184. {
  185. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(f));
  186. }
  187. System.IO.File.WriteAllText(f, content);
  188. }
  189. catch
  190. {
  191. MessageBox.Show("尝试保存" + filename + "失败");
  192. }
  193. //System.Diagnostics.Process.Start(f);
  194. }
  195. private void btn_Stop_Click(object sender, EventArgs e)
  196. {
  197. if (th!=null)
  198. {
  199. this.th.Abort();
  200. this.txt_result.AppendText("\r\n用户终止!");
  201. }
  202. }
  203. private void btn_QuickStart_Click(object sender, EventArgs e)
  204. {
  205. reset();
  206. if (init())
  207. {
  208. th = new Thread(new ParameterizedThreadStart(process));
  209. th.IsBackground = true;
  210. th.Start(txt_result.Text);
  211. }
  212. }
  213. private void button1_Click(object sender, EventArgs e)
  214. {
  215. txt_result.Text = string.Empty;
  216. }
  217. }
  218. }