Form1.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.使用wap.iciba.com网站作为数据源。
  167. 4.全选、复制、粘贴到Excel中可以多列显示。
  168. ---作者:Hansi
  169. 2014年11月5日
  170. ";
  171. }
  172. private void Save(string fileName, string content)
  173. {
  174. try
  175. {
  176. string f = System.IO.Path.Combine(subDirName, fileName + ".txt");
  177. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(f)))
  178. {
  179. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(f));
  180. }
  181. System.IO.File.WriteAllText(f, content);
  182. }
  183. catch
  184. {
  185. MessageBox.Show("尝试保存" + filename + "失败");
  186. }
  187. //System.Diagnostics.Process.Start(f);
  188. }
  189. private void btn_Stop_Click(object sender, EventArgs e)
  190. {
  191. if (th!=null)
  192. {
  193. this.th.Abort();
  194. this.txt_result.AppendText("\r\n用户终止!");
  195. }
  196. }
  197. private void btn_QuickStart_Click(object sender, EventArgs e)
  198. {
  199. reset();
  200. if (init())
  201. {
  202. th = new Thread(new ParameterizedThreadStart(process));
  203. th.IsBackground = true;
  204. th.Start(txt_result.Text);
  205. }
  206. }
  207. }
  208. }