123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using log = Log.Log;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Threading;
- namespace frequency
- {
- public partial class Form1 : Form
- {
- public long currentIndex;
- public long totalNum;
- public Dictionary<string, int> finalResult;
- public Form1()
- {
- // init logger
- log.Init("frequency.log");
- // init params
- path = System.Environment.CurrentDirectory;
- // init component
- InitializeComponent();
- // init ShowProgress
- Thread.Sleep(1000);
- Thread thread = new Thread(this.ShowProgress);
- thread.IsBackground = true;
- thread.Start();
- }
- private string[] originTextsOrder;
- private string[] originTextsInverse;
- private string[] targetTexts;
- // progress status
- const string CAL_WAIT = "wait";
- const string CAL_RUNNING = "running";
- const string CAL_ERROR = "error";
- const string CAL_SUCCESS = "success";
- const string CAL_EXPORT = "exporting";
- public delegate void ChangeProgressStatus();
- public delegate void CallBackDelegate();
- private void SelectOriginButton(object sender, EventArgs e)
- {
- var fileDialog = new OpenFileDialog();
- fileDialog.Multiselect = false;
- fileDialog.Title = "选择源文件";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- this.originTextPath = fileDialog.FileName;
- this.originText.Text = originTextPath;
- }
- }
- private void ShowOriginText(object sender, EventArgs e)
- {
- }
- private void SelectTargetButton(object sender, EventArgs e)
- {
- var fileDialog = new OpenFileDialog();
- fileDialog.Multiselect = false;
- fileDialog.Title = "选择目标文件";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- this.targetTextPath = fileDialog.FileName;
- this.targetText.Text = this.targetTextPath;
- }
- }
- private void ShowTargetText(object sender, EventArgs e)
- {
- }
- private void Calculate_Click(object sender, EventArgs e)
- {
- if (null == this.originTextPath || null == this.targetTextPath || "" == this.originTextPath.Trim() || "" == this.targetTextPath.Trim())
- {
- MessageBox.Show("请先选择文件");
- return;
- }
- //this.progress.Text = "计算中...";
- //this.progress.Refresh();
- this.progressStatus = CAL_RUNNING;
- this.calculate.Enabled = false;
- var code = this.Calculate();
- Application.DoEvents();
- }
- private int Calculate()
- {
- int code;
- // read origin file
- code = this.ReadOriginText();
- if (code != 0)
- {
- log.E("Calculate_Click read origin file error with code: {0}", code);
- return 1;
- }
- // read target file
- code = this.ReadTargetText();
- if (code != 0)
- {
- log.E("Calculate_Click read target file error with code: {0}", code);
- return 1;
- }
- // clear target file
- this.CleanTarget();
- // look up
- Thread runLoopUp = new Thread(Lookup);
- CallBackDelegate cbd = LoopUpCallBack;
- runLoopUp.Start(cbd);
- log.D("Calculate_Click look up success");
- // write data to file
- //this.progress.Text = "导出数据中...";
- //this.progress.Refresh();
- //this.progressStatus = CAL_EXPORT;
- //var msg = this.Write2Xlsx(result);
- //if ("" != msg)
- //{
- // log.E("Calculate_Click write data to xlsx error: {0}", msg);
- // return 1;
- //}
- log.D("Calculate_Click write data to xlsx success");
- return 0;
- }
- private void LoopUpCallBack()
- {
- progressStatus = CAL_EXPORT;
- var msg = this.Write2Xlsx(finalResult);
- if ("" != msg)
- {
- progressStatus = CAL_ERROR;
- calculate.Enabled = true;
- log.E("Calculate_Click write data to xlsx error: {0}", msg);
- return ;
- }
- progressStatus = CAL_SUCCESS;
- log.D("Calculate_Click write data to xlsx success with length: {0}", finalResult.Count);
- }
- private void SeeResult_Click(object sender, EventArgs e)
- {
- try
- {
- System.Diagnostics.Process.Start("explorer.exe", path);
- }
- catch (Exception err)
- {
- MessageBox.Show(err.ToString());
- }
- }
- // 读取原始文件
- private int ReadOriginText()
- {
- string[] texts;
- try
- {
- texts = File.ReadAllLines(this.originTextPath);
- }
- catch (Exception err)
- {
- log.E("readOriginText with path: {0} error: {1}", this.originTextPath, err.ToString());
- MessageBox.Show(err.ToString());
- return 1;
- }
- if (texts.Length < 1)
- {
- log.E("readOriginText with path: {0}, read file is empty", this.originTextPath);
- MessageBox.Show("file is empty");
- return 1;
- }
- log.D("ReadOriginText received origin text with length: {0}", texts.Length);
- // check
- int length = 0;
- for (int i = 0; i < texts.Length; i++)
- {
- var val = texts[i].Trim();
- if (val.Length < 1)
- {
- continue;
- }
- length++;
- }
- log.D("ReadOriginText received check text with length: {0}", length);
- // append
- var tempTexts = new string[length];
- int index = 0;
- for (int i = 0; i < texts.Length; i++)
- {
- var val = texts[i].Trim();
- if (val.Length < 1)
- {
- continue;
- }
- tempTexts[index++] = texts[i];
- }
- // combine
- index = 0;
- this.originTextsOrder = new string[(length * (length - 1)) / 2];
- for (int i = 0; i < length; i++)
- {
- for (int j = i + 1; j < length; j++)
- {
- this.originTextsOrder[index++] = tempTexts[i] + "-" + tempTexts[j];
- }
- }
- index = 0;
- this.originTextsInverse = new string[(length * (length - 1)) / 2];
- for (int i = length - 1; i >= 0; i--)
- {
- for (int j = i - 1; j >= 0; j--)
- {
- this.originTextsInverse[index++] = tempTexts[i] + "-" + tempTexts[j];
- }
- }
- log.D("ReadOriginText read origin file success with order length: {0}, inverse length; {1}", this.originTextsOrder.Length, this.originTextsInverse.Length);
- return 0;
- }
- // 读取目标文件
- private int ReadTargetText()
- {
- string[] texts;
- try
- {
- texts = File.ReadAllLines(this.targetTextPath);
- }
- catch (Exception err)
- {
- log.E("ReadTargetText with path: {0} error: {1}", this.targetTextPath, err.ToString());
- MessageBox.Show(err.ToString());
- return 1;
- }
- if (texts.Length < 1)
- {
- log.E("ReadTargetText with path: {0}, read file is empty", this.targetTextPath);
- MessageBox.Show("file is empty");
- return 1;
- }
- log.D("ReadTargetText read texts: {0}", texts);
- // check
- int length = 0;
- for (int i = 0; i < texts.Length; i++)
- {
- var val = texts[i].Trim();
- if (val.Length < 1 || !val.Contains("-"))
- {
- continue;
- }
- length++;
- }
- // append
- this.targetTexts = new string[length];
- var index = 0;
- for (int i = 0; i < texts.Length; i++)
- {
- var val = texts[i].Trim();
- if (val.Length < 1 || !val.Contains("-"))
- {
- continue;
- }
- this.targetTexts[index++] = texts[i];
- }
- return 0;
- }
- /*
- * desc
- * 1. 目标文件中的字符串形如54#01(100#)--54#02,其中括号以及括号的内容清除不计。
- * 2. 源文件两两组合的后的字符串再到目标文件中查找,所有单个的字符串都可以忽略不计算;如58#01可以忽略不计。
- */
- private void CleanTarget()
- {
- var originLength = this.targetTexts.Length;
- string[] tempStrings = new string[this.targetTexts.Length];
- var index = 0;
- for (int i = 0; i < this.targetTexts.Length; i++)
- {
- var val = this.targetTexts[i].Trim();
- if (val.Length < 1)
- {
- continue;
- }
- if (!val.Contains("-"))
- {
- continue;
- }
- var clearStr = ClearString(val);
- tempStrings[index++] = clearStr;
- }
- this.targetTexts = new string[index];
- for (int i = 0; i < index; i++)
- {
- this.targetTexts[i] = tempStrings[i];
- }
- log.D("CleanTarget clean target origin length {0} success with last length: {1}", originLength, this.targetTexts.Length);
- }
- private string ClearString(string str)
- {
- if (!str.Contains("("))
- {
- return str;
- }
- string result = "";
- bool had = false;
- for (int i = 0; i < str.Length; i++)
- {
- if (had && str[i] != ')')
- {
- continue;
- }
- if (had && str[i] == ')')
- {
- had = false;
- continue;
- }
- if (str[i] == '(')
- {
- had = true;
- continue;
- }
- result += str[i];
- }
- return ClearString(result);
- }
- private void Lookup(object o)
- {
- this.currentIndex = 0;
- this.totalNum = this.targetTexts.Length * this.originTextsOrder.Length + this.originTextsInverse.Length * this.targetTexts.Length;
- Dictionary<string, int> result = new Dictionary<string, int>();
- for (int i = 0; i < this.originTextsOrder.Length; i++)
- {
- for (int j = 0; j < this.targetTexts.Length; j++)
- {
- this.currentIndex++;
- if (judge(this.originTextsOrder[i], this.targetTexts[j]))
- {
- var key = this.originTextsOrder[i];
- if (result.ContainsKey(key))
- {
- result[key] = result[key] + 1;
- }
- else
- {
- result[key] = 1;
- }
- }
- }
- }
- for (int i = 0; i < this.originTextsInverse.Length; i++)
- {
- for (int j = 0; j < this.targetTexts.Length; j++)
- {
- this.currentIndex++;
- if (judge(this.originTextsInverse[i], this.targetTexts[j]))
- {
- var key = this.originTextsInverse[i];
- if (result.ContainsKey(key))
- {
- result[key] = result[key] + 1;
- }
- else
- {
- result[key] = 1;
- }
- }
- }
- }
- finalResult = result;
- log.D("Lookup calcate success with length: {0}", result.Count);
- var cbd = o as CallBackDelegate;
- cbd();
- return ;
- }
- private bool judge(string origin, string target)
- {
- var strs = origin.Split(new char[] { '-' }, 2, StringSplitOptions.None);
- if (strs.Length < 2)
- {
- return false;
- }
- var originStr1 = strs[0] + "-" + strs[1];
- var originStr2 = strs[0] + "--" + strs[1];
- if (judgeDetail(originStr1, target))
- {
- return true;
- } else if(judgeDetail(originStr2, target))
- {
- return true;
- }
- return false;
- }
- /*
- * 解决形如 ea#b-c#d 中查找a#b-c#d,但是返回true的bug
- */
- private bool judgeDetail(string origin, string target)
- {
- var index = target.IndexOf(origin);
- if (index < 0)
- {
- return false;
- }
- if (index != 0)
- {
- if ('-' != target[index - 1])
- {
- return false;
- }
- }
- if (index + origin.Length < target.Length)
- {
- if ('-' != target[index + origin.Length])
- {
- return false;
- }
- }
- return true;
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private string Write2Xlsx(Dictionary<string, int> data)
- {
- var suffix = DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + ".xlsx";
- var filename = Path.Combine(path, suffix);
- Excel.Application app = new Excel.Application();
- Excel.Workbook doc = app.Workbooks.Add(Type.Missing);
- if (doc.Worksheets.Count < 1)
- {
- doc.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing);
- }
- Excel.Worksheet sheet = doc.ActiveSheet;
- var index = 1;
- foreach (var val in data)
- {
- sheet.Cells[index, 1] = val.Key;
- sheet.Cells[index, 2] = val.Value;
- index++;
- }
- try
- {
- sheet.SaveAs(filename);
- doc.Save();
- doc.Close(Type.Missing, filename, Type.Missing);
- app.Quit();
- }
- catch (Exception err)
- {
- log.E("Write2Xlsx write file({0}) error: {1}", filename, err.ToString());
- return err.ToString();
- }
- log.D("Write2Xlsx write file({0}) success", filename);
- return "";
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //this.Write2Xlsx();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void ShowProgress()
- {
- log.D("ShowProgress start successful");
- while (true)
- {
- if (this.IsHandleCreated)
- {
- Invoke(new ChangeProgressStatus(ChangeStatus));
- }
- //log.D("currentIndex: {0}, totalNum: {1}", currentIndex, totalNum);
- Thread.Sleep(1000);
- }
- log.D("ShowProgress had finished");
- }
- private void ChangeStatus()
- {
- string status = "";
- if (CAL_WAIT == progressStatus || "" == progressStatus)
- {
- status = "计算未开始";
- }
- if (CAL_RUNNING == progressStatus)
- {
- status = string.Format("计算中... {0}/{1}", currentIndex, totalNum);
- }
- if (CAL_ERROR == progressStatus)
- {
- status = "计算出错";
- }
- if (CAL_EXPORT == progressStatus)
- {
- status = "导出数据中...";
- }
- if (CAL_SUCCESS == progressStatus)
- {
- status = "计算完成";
- }
- this.progress.Text = status;
- this.progress.Refresh();
- if (progressStatus == CAL_ERROR || progressStatus == CAL_SUCCESS)
- {
- this.calculate.Enabled = true;
- this.calculate.Refresh();
- }
-
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- System.Environment.Exit(0);
- }
- }
- }
|