轉貼KNN C#

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Xml; using System.Timers; using System.Threading; namespace ChineseTextCategorization { /// /// Form1 的摘要說明。 /// public class MainFrm : System.Windows.Forms.Form { private delegate void MessageHandler(string messageText);//創建一個委託---步驟1 private event MessageHandler NormalMessageArrived;//將創建的委託和特定事件關聯,在這裏特定的事件為MessageArrived ---步驟2*/ private System.Windows.Forms.GroupBox groupBox_LeftMainFrm; private System.Windows.Forms.Button button_Exit; private System.Windows.Forms.Button button_Dictionary; private System.Windows.Forms.OpenFileDialog openFileDialog_AddDictionary; //Hash表裝載詞典 public System.Collections.Hashtable DictionaryHT=new Hashtable(); //用於裝載分類文本的詞典,分詞結束後複製詞典中Value不為0的詞,並將詞典初始化 public System.Collections.Hashtable SmallDictionaryHT=new Hashtable(); //存儲一個檔夾下的所有檔 public string[]MyFile=null; private System.Windows.Forms.Button button_DelDictionary; private System.Windows.Forms.TextBox textBox_TestWord; private System.Windows.Forms.Button button_Search; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.TextBox textBox_Pagraph; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.TextBox textBox_Words; private System.Windows.Forms.Button button_Study; private System.Windows.Forms.GroupBox groupBox6; private System.Windows.Forms.TextBox textBox_Sample; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialogSamplePick; private System.Windows.Forms.Button button_AddNewCategorzation; private System.Windows.Forms.TextBox textBox_NewCategorzationName; private System.Windows.Forms.ComboBox comboBox_Categorzations; private System.Windows.Forms.Button button_AddSampleFolder; private System.Windows.Forms.CheckBox checkBox_Studying; private System.Windows.Forms.Button button_SplitCategorzation; private System.Windows.Forms.Button button_AddCategorzation; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.GroupBox groupBox_Process; private System.Windows.Forms.GroupBox groupBox_ResultShow; private System.Windows.Forms.GroupBox groupBox7; private System.Windows.Forms.GroupBox groupBox8; private System.Windows.Forms.GroupBox groupBox9; private System.Windows.Forms.GroupBox groupBox10; private System.Windows.Forms.GroupBox groupBox11; private System.Windows.Forms.Button button_SystemHelp; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.GroupBox groupBox12; private System.Windows.Forms.Button button_ProgressPragh; private System.Windows.Forms.Button button_ProgressWords; private System.Windows.Forms.TextBox textBox_PagraphRsult; private System.Windows.Forms.Button button_ClearCategorzationDictionary; private System.Windows.Forms.GroupBox groupBox5; private System.Windows.Forms.CheckBox checkBox_Datail; private System.Windows.Forms.Label label2; /// /// 必需的設計器變數。 /// private System.ComponentModel.Container components = null; public MainFrm() { // // Windows 表單設計器支援所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼 // this.NormalMessageArrived+=new MessageHandler(MainFrm_NormalMessageArrived); } /// /// 清理所有正在使用的資源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 表單設計器生成的代碼 /// /// 設計器支援所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// private void InitializeComponent() { this.groupBox_LeftMainFrm = new System.Windows.Forms.GroupBox(); this.groupBox12 = new System.Windows.Forms.GroupBox(); this.button_ProgressWords = new System.Windows.Forms.Button(); this.button_ProgressPragh = new System.Windows.Forms.Button(); this.groupBox11 = new System.Windows.Forms.GroupBox(); this.button_SystemHelp = new System.Windows.Forms.Button(); this.button_Exit = new System.Windows.Forms.Button(); this.groupBox10 = new System.Windows.Forms.GroupBox(); this.button_AddCategorzation = new System.Windows.Forms.Button(); this.button_SplitCategorzation = new System.Windows.Forms.Button(); this.checkBox_Studying = new System.Windows.Forms.CheckBox(); this.groupBox9 = new System.Windows.Forms.GroupBox(); this.button_Dictionary = new System.Windows.Forms.Button(); this.button_DelDictionary = new System.Windows.Forms.Button(); this.groupBox8 = new System.Windows.Forms.GroupBox(); this.button_ClearCategorzationDictionary = new System.Windows.Forms.Button(); this.button_AddNewCategorzation = new System.Windows.Forms.Button(); this.textBox_NewCategorzationName = new System.Windows.Forms.TextBox(); this.groupBox7 = new System.Windows.Forms.GroupBox(); this.comboBox_Categorzations = new System.Windows.Forms.ComboBox(); this.button_Study = new System.Windows.Forms.Button(); this.button_AddSampleFolder = new System.Windows.Forms.Button(); this.openFileDialog_AddDictionary = new System.Windows.Forms.OpenFileDialog(); this.textBox_TestWord = new System.Windows.Forms.TextBox(); this.button_Search = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.textBox_Pagraph = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.textBox_PagraphRsult = new System.Windows.Forms.TextBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.textBox_Words = new System.Windows.Forms.TextBox(); this.groupBox6 = new System.Windows.Forms.GroupBox(); this.textBox_Sample = new System.Windows.Forms.TextBox(); this.folderBrowserDialogSamplePick = new System.Windows.Forms.FolderBrowserDialog(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.groupBox_Process = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.groupBox_ResultShow = new System.Windows.Forms.GroupBox(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.checkBox_Datail = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label(); this.groupBox_LeftMainFrm.SuspendLayout(); this.groupBox12.SuspendLayout(); this.groupBox11.SuspendLayout(); this.groupBox10.SuspendLayout(); this.groupBox9.SuspendLayout(); this.groupBox8.SuspendLayout(); this.groupBox7.SuspendLayout(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.groupBox6.SuspendLayout(); this.groupBox_Process.SuspendLayout(); this.groupBox_ResultShow.SuspendLayout(); this.groupBox5.SuspendLayout(); this.SuspendLayout(); // // groupBox_LeftMainFrm // this.groupBox_LeftMainFrm.Controls.Add(this.groupBox12); this.groupBox_LeftMainFrm.Controls.Add(this.groupBox11); this.groupBox_LeftMainFrm.Controls.Add(this.groupBox10); this.groupBox_LeftMainFrm.Controls.Add(this.groupBox9); this.groupBox_LeftMainFrm.Controls.Add(this.groupBox8); this.groupBox_LeftMainFrm.Controls.Add(this.groupBox7); this.groupBox_LeftMainFrm.Dock = System.Windows.Forms.DockStyle.Bottom; this.groupBox_LeftMainFrm.Location = new System.Drawing.Point(0, 542); this.groupBox_LeftMainFrm.Name = "groupBox_LeftMainFrm"; this.groupBox_LeftMainFrm.Size = new System.Drawing.Size(728, 168); this.groupBox_LeftMainFrm.TabIndex = 1; this.groupBox_LeftMainFrm.TabStop = false; // // groupBox12 // this.groupBox12.Controls.Add(this.button_ProgressWords); this.groupBox12.Controls.Add(this.button_ProgressPragh); this.groupBox12.Location = new System.Drawing.Point(613, 24); this.groupBox12.Name = "groupBox12"; this.groupBox12.Size = new System.Drawing.Size(104, 136); this.groupBox12.TabIndex = 17; this.groupBox12.TabStop = false; this.groupBox12.Text = "過程測試"; // // button_ProgressWords // this.button_ProgressWords.Enabled = false; this.button_ProgressWords.Location = new System.Drawing.Point(8, 96); this.button_ProgressWords.Name = "button_ProgressWords"; this.button_ProgressWords.Size = new System.Drawing.Size(88, 23); this.button_ProgressWords.TabIndex = 1; this.button_ProgressWords.Text = "分詞"; this.button_ProgressWords.Click += new System.EventHandler(this.button_ProgressWords_Click); // // button_ProgressPragh // this.button_ProgressPragh.Location = new System.Drawing.Point(8, 24); this.button_ProgressPragh.Name = "button_ProgressPragh"; this.button_ProgressPragh.Size = new System.Drawing.Size(88, 23); this.button_ProgressPragh.TabIndex = 0; this.button_ProgressPragh.Text = "分句"; this.button_ProgressPragh.Click += new System.EventHandler(this.button_ProgressPragh_Click); // // groupBox11 // this.groupBox11.Controls.Add(this.button_SystemHelp); this.groupBox11.Controls.Add(this.button_Exit); this.groupBox11.Location = new System.Drawing.Point(492, 24); this.groupBox11.Name = "groupBox11"; this.groupBox11.Size = new System.Drawing.Size(104, 136); this.groupBox11.TabIndex = 16; this.groupBox11.TabStop = false; this.groupBox11.Text = "系統"; // // button_SystemHelp // this.button_SystemHelp.Location = new System.Drawing.Point(8, 96); this.button_SystemHelp.Name = "button_SystemHelp"; this.button_SystemHelp.Size = new System.Drawing.Size(88, 23); this.button_SystemHelp.TabIndex = 10; this.button_SystemHelp.Text = "説明"; this.button_SystemHelp.Click += new System.EventHandler(this.button_SystemHelp_Click); // // button_Exit // this.button_Exit.Location = new System.Drawing.Point(8, 24); this.button_Exit.Name = "button_Exit"; this.button_Exit.Size = new System.Drawing.Size(88, 23); this.button_Exit.TabIndex = 9; this.button_Exit.Text = "退出"; this.button_Exit.Click += new System.EventHandler(this.button_Exit_Click); // // groupBox10 // this.groupBox10.Controls.Add(this.button_AddCategorzation); this.groupBox10.Controls.Add(this.button_SplitCategorzation); this.groupBox10.Controls.Add(this.checkBox_Studying); this.groupBox10.Location = new System.Drawing.Point(371, 24); this.groupBox10.Name = "groupBox10"; this.groupBox10.Size = new System.Drawing.Size(104, 136); this.groupBox10.TabIndex = 15; this.groupBox10.TabStop = false; this.groupBox10.Text = "文本分類"; // // button_AddCategorzation // this.button_AddCategorzation.Location = new System.Drawing.Point(8, 24); this.button_AddCategorzation.Name = "button_AddCategorzation"; this.button_AddCategorzation.Size = new System.Drawing.Size(88, 23); this.button_AddCategorzation.TabIndex = 6; this.button_AddCategorzation.Text = "分類檔"; this.button_AddCategorzation.Click += new System.EventHandler(this.button_AddCategorzation_Click); // // button_SplitCategorzation // this.button_SplitCategorzation.Enabled = false; this.button_SplitCategorzation.Location = new System.Drawing.Point(8, 57); this.button_SplitCategorzation.Name = "button_SplitCategorzation"; this.button_SplitCategorzation.Size = new System.Drawing.Size(88, 23); this.button_SplitCategorzation.TabIndex = 11; this.button_SplitCategorzation.Text = "文本分類"; this.button_SplitCategorzation.Click += new System.EventHandler(this.button_SplitCategorzation_Click); // // checkBox_Studying // this.checkBox_Studying.Location = new System.Drawing.Point(8, 95); this.checkBox_Studying.Name = "checkBox_Studying"; this.checkBox_Studying.Size = new System.Drawing.Size(88, 24); this.checkBox_Studying.TabIndex = 5; this.checkBox_Studying.Text = "再學習"; // // groupBox9 // this.groupBox9.Controls.Add(this.button_Dictionary); this.groupBox9.Controls.Add(this.button_DelDictionary); this.groupBox9.Location = new System.Drawing.Point(8, 24); this.groupBox9.Name = "groupBox9"; this.groupBox9.Size = new System.Drawing.Size(104, 136); this.groupBox9.TabIndex = 14; this.groupBox9.TabStop = false; this.groupBox9.Text = "詞典"; // // button_Dictionary // this.button_Dictionary.Location = new System.Drawing.Point(8, 24); this.button_Dictionary.Name = "button_Dictionary"; this.button_Dictionary.Size = new System.Drawing.Size(88, 23); this.button_Dictionary.TabIndex = 0; this.button_Dictionary.Text = "載入詞典"; this.button_Dictionary.Click += new System.EventHandler(this.button_Dictionary_Click); // // button_DelDictionary // this.button_DelDictionary.Enabled = false; this.button_DelDictionary.Location = new System.Drawing.Point(8, 96); this.button_DelDictionary.Name = "button_DelDictionary"; this.button_DelDictionary.Size = new System.Drawing.Size(88, 23); this.button_DelDictionary.TabIndex = 2; this.button_DelDictionary.Text = "清除詞典"; this.button_DelDictionary.Click += new System.EventHandler(this.button_DelDictionary_Click); // // groupBox8 // this.groupBox8.Controls.Add(this.button_ClearCategorzationDictionary); this.groupBox8.Controls.Add(this.button_AddNewCategorzation); this.groupBox8.Controls.Add(this.textBox_NewCategorzationName); this.groupBox8.Location = new System.Drawing.Point(129, 24); this.groupBox8.Name = "groupBox8"; this.groupBox8.Size = new System.Drawing.Size(104, 136); this.groupBox8.TabIndex = 13; this.groupBox8.TabStop = false; this.groupBox8.Text = "添加類別"; // // button_ClearCategorzationDictionary // this.button_ClearCategorzationDictionary.Location = new System.Drawing.Point(8, 64); this.button_ClearCategorzationDictionary.Name = "button_ClearCategorzationDictionary"; this.button_ClearCategorzationDictionary.Size = new System.Drawing.Size(88, 23); this.button_ClearCategorzationDictionary.TabIndex = 4; this.button_ClearCategorzationDictionary.Text = "清空類別詞庫"; this.button_ClearCategorzationDictionary.Click += new System.EventHandler(this.button_ClearCategorzationDictionary_Click); // // button_AddNewCategorzation // this.button_AddNewCategorzation.Location = new System.Drawing.Point(8, 96); this.button_AddNewCategorzation.Name = "button_AddNewCategorzation"; this.button_AddNewCategorzation.Size = new System.Drawing.Size(88, 23); this.button_AddNewCategorzation.TabIndex = 2; this.button_AddNewCategorzation.Text = "添加新類"; this.button_AddNewCategorzation.Click += new System.EventHandler(this.button_AddNewCategorzation_Click); // // textBox_NewCategorzationName // this.textBox_NewCategorzationName.Location = new System.Drawing.Point(8, 24); this.textBox_NewCategorzationName.Name = "textBox_NewCategorzationName"; this.textBox_NewCategorzationName.Size = new System.Drawing.Size(88, 21); this.textBox_NewCategorzationName.TabIndex = 3; this.textBox_NewCategorzationName.Text = ""; // // groupBox7 // this.groupBox7.Controls.Add(this.comboBox_Categorzations); this.groupBox7.Controls.Add(this.button_Study); this.groupBox7.Controls.Add(this.button_AddSampleFolder); this.groupBox7.Location = new System.Drawing.Point(250, 24); this.groupBox7.Name = "groupBox7"; this.groupBox7.Size = new System.Drawing.Size(104, 136); this.groupBox7.TabIndex = 12; this.groupBox7.TabStop = false; this.groupBox7.Text = "樣本訓練"; // // comboBox_Categorzations // this.comboBox_Categorzations.Location = new System.Drawing.Point(8, 24); this.comboBox_Categorzations.Name = "comboBox_Categorzations"; this.comboBox_Categorzations.Size = new System.Drawing.Size(96, 20); this.comboBox_Categorzations.TabIndex = 1; // // button_Study // this.button_Study.Enabled = false; this.button_Study.Location = new System.Drawing.Point(8, 95); this.button_Study.Name = "button_Study"; this.button_Study.Size = new System.Drawing.Size(96, 24); this.button_Study.TabIndex = 10; this.button_Study.Text = "樣本訓練"; this.button_Study.Click += new System.EventHandler(this.button_Study_Click); // // button_AddSampleFolder // this.button_AddSampleFolder.Location = new System.Drawing.Point(8, 57); this.button_AddSampleFolder.Name = "button_AddSampleFolder"; this.button_AddSampleFolder.Size = new System.Drawing.Size(96, 24); this.button_AddSampleFolder.TabIndex = 4; this.button_AddSampleFolder.Text = "添加樣本檔夾"; this.button_AddSampleFolder.Click += new System.EventHandler(this.button_AddSampleFolder_Click); // // textBox_TestWord // this.textBox_TestWord.Location = new System.Drawing.Point(0, 16); this.textBox_TestWord.Name = "textBox_TestWord"; this.textBox_TestWord.Size = new System.Drawing.Size(192, 21); this.textBox_TestWord.TabIndex = 3; this.textBox_TestWord.Text = ""; // // button_Search // this.button_Search.Enabled = false; this.button_Search.Location = new System.Drawing.Point(216, 16); this.button_Search.Name = "button_Search"; this.button_Search.Size = new System.Drawing.Size(56, 23); this.button_Search.TabIndex = 4; this.button_Search.Text = "搜索"; this.button_Search.Click += new System.EventHandler(this.button_Search_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.textBox_TestWord); this.groupBox1.Controls.Add(this.button_Search); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox1.Location = new System.Drawing.Point(3, 17); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(282, 40); this.groupBox1.TabIndex = 5; this.groupBox1.TabStop = false; this.groupBox1.Text = "詞典測試 查閱詞典"; // // groupBox2 // this.groupBox2.Controls.Add(this.textBox_Pagraph); this.groupBox2.Location = new System.Drawing.Point(0, 64); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(282, 120); this.groupBox2.TabIndex = 6; this.groupBox2.TabStop = false; this.groupBox2.Text = "測試文本"; // // textBox_Pagraph // this.textBox_Pagraph.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox_Pagraph.Location = new System.Drawing.Point(3, 17); this.textBox_Pagraph.Multiline = true; this.textBox_Pagraph.Name = "textBox_Pagraph"; this.textBox_Pagraph.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.textBox_Pagraph.Size = new System.Drawing.Size(276, 100); this.textBox_Pagraph.TabIndex = 0; this.textBox_Pagraph.Text = ""; // // groupBox3 // this.groupBox3.Controls.Add(this.textBox_PagraphRsult); this.groupBox3.Location = new System.Drawing.Point(0, 216); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(282, 176); this.groupBox3.TabIndex = 7; this.groupBox3.TabStop = false; this.groupBox3.Text = "分句結果"; // // textBox_PagraphRsult // this.textBox_PagraphRsult.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox_PagraphRsult.Location = new System.Drawing.Point(3, 17); this.textBox_PagraphRsult.Multiline = true; this.textBox_PagraphRsult.Name = "textBox_PagraphRsult"; this.textBox_PagraphRsult.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.textBox_PagraphRsult.Size = new System.Drawing.Size(276, 156); this.textBox_PagraphRsult.TabIndex = 0; this.textBox_PagraphRsult.Text = ""; // // groupBox4 // this.groupBox4.Controls.Add(this.textBox_Words); this.groupBox4.Dock = System.Windows.Forms.DockStyle.Bottom; this.groupBox4.Location = new System.Drawing.Point(3, 395); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(282, 144); this.groupBox4.TabIndex = 9; this.groupBox4.TabStop = false; this.groupBox4.Text = "分詞結果"; // // textBox_Words // this.textBox_Words.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox_Words.Location = new System.Drawing.Point(3, 17); this.textBox_Words.Multiline = true; this.textBox_Words.Name = "textBox_Words"; this.textBox_Words.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.textBox_Words.Size = new System.Drawing.Size(276, 124); this.textBox_Words.TabIndex = 0; this.textBox_Words.Text = ""; // // groupBox6 // this.groupBox6.Controls.Add(this.textBox_Sample); this.groupBox6.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox6.Location = new System.Drawing.Point(3, 17); this.groupBox6.Name = "groupBox6"; this.groupBox6.Size = new System.Drawing.Size(434, 455); this.groupBox6.TabIndex = 11; this.groupBox6.TabStop = false; this.groupBox6.Text = "~顯示~"; // // textBox_Sample // this.textBox_Sample.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox_Sample.Location = new System.Drawing.Point(3, 17); this.textBox_Sample.Multiline = true; this.textBox_Sample.Name = "textBox_Sample"; this.textBox_Sample.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.textBox_Sample.Size = new System.Drawing.Size(428, 435); this.textBox_Sample.TabIndex = 0; this.textBox_Sample.Text = ""; // // progressBar1 // this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom; this.progressBar1.Location = new System.Drawing.Point(3, 518); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(434, 23); this.progressBar1.TabIndex = 12; // // groupBox_Process // this.groupBox_Process.Controls.Add(this.label1); this.groupBox_Process.Controls.Add(this.button1); this.groupBox_Process.Controls.Add(this.groupBox1); this.groupBox_Process.Controls.Add(this.groupBox2); this.groupBox_Process.Controls.Add(this.groupBox4); this.groupBox_Process.Controls.Add(this.groupBox3); this.groupBox_Process.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox_Process.Location = new System.Drawing.Point(440, 0); this.groupBox_Process.Name = "groupBox_Process"; this.groupBox_Process.Size = new System.Drawing.Size(288, 542); this.groupBox_Process.TabIndex = 13; this.groupBox_Process.TabStop = false; this.groupBox_Process.Text = "過程測試"; // // label1 // this.label1.Location = new System.Drawing.Point(8, 192); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(216, 23); this.label1.TabIndex = 12; this.label1.Text = "測試文本可以直接粘貼或選擇文字檔案"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // button1 // this.button1.Location = new System.Drawing.Point(240, 192); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(40, 23); this.button1.TabIndex = 11; this.button1.Text = "..."; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox_ResultShow // this.groupBox_ResultShow.Controls.Add(this.groupBox5); this.groupBox_ResultShow.Controls.Add(this.progressBar1); this.groupBox_ResultShow.Controls.Add(this.groupBox6); this.groupBox_ResultShow.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox_ResultShow.Location = new System.Drawing.Point(0, 0); this.groupBox_ResultShow.Name = "groupBox_ResultShow"; this.groupBox_ResultShow.Size = new System.Drawing.Size(440, 544); this.groupBox_ResultShow.TabIndex = 14; this.groupBox_ResultShow.TabStop = false; this.groupBox_ResultShow.Text = "顯示"; // // groupBox5 // this.groupBox5.Controls.Add(this.label2); this.groupBox5.Controls.Add(this.checkBox_Datail); this.groupBox5.Location = new System.Drawing.Point(0, 472); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(440, 40); this.groupBox5.TabIndex = 13; this.groupBox5.TabStop = false; // // checkBox_Datail // this.checkBox_Datail.Location = new System.Drawing.Point(328, 8); this.checkBox_Datail.Name = "checkBox_Datail"; this.checkBox_Datail.Size = new System.Drawing.Size(88, 24); this.checkBox_Datail.TabIndex = 0; this.checkBox_Datail.Text = "顯示細節"; // // label2 // this.label2.Location = new System.Drawing.Point(24, 8); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(232, 23); this.label2.TabIndex = 1; this.label2.Text = "本程式耗較多的記憶體和CPU,刷新會很慢"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // MainFrm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 710); this.Controls.Add(this.groupBox_ResultShow); this.Controls.Add(this.groupBox_Process); this.Controls.Add(this.groupBox_LeftMainFrm); this.MaximizeBox = false; this.Name = "MainFrm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "中文文本分類"; this.Load += new System.EventHandler(this.MainFrm_Load); this.groupBox_LeftMainFrm.ResumeLayout(false); this.groupBox12.ResumeLayout(false); this.groupBox11.ResumeLayout(false); this.groupBox10.ResumeLayout(false); this.groupBox9.ResumeLayout(false); this.groupBox8.ResumeLayout(false); this.groupBox7.ResumeLayout(false); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox4.ResumeLayout(false); this.groupBox6.ResumeLayout(false); this.groupBox_Process.ResumeLayout(false); this.groupBox_ResultShow.ResumeLayout(false); this.groupBox5.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// 應用程式的主入口點。 /// [STAThread] static void Main() { Application.Run(new MainFrm()); } private void button_Exit_Click(object sender, System.EventArgs e) { System.Environment.Exit(0); } //載入詞典 private void button_Dictionary_Click(object sender, System.EventArgs e) { string line; try { this.openFileDialog_AddDictionary.InitialDirectory=System.Windows.Forms.Application.StartupPath; if(this.openFileDialog_AddDictionary.ShowDialog()==DialogResult.OK) { System.IO.StreamReader SR=new StreamReader(this.openFileDialog_AddDictionary.FileName.ToString(),System.Text.Encoding.Default); while((line=SR.ReadLine())!=null) { this.DictionaryHT.Add(line,0); } //添加一項表示詞出現的總數的項 DictionaryHT.Add("SumWords",0); EnableButton(true); MessageBox.Show("載入詞典成功"); } } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } //this.openFileDialog_AddDictionary.; } //清除詞典 private void button_DelDictionary_Click(object sender, System.EventArgs e) { try { this.DictionaryHT.Clear(); EnableButton(false); MessageBox.Show("清除詞典成功"); } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void button_Search_Click(object sender, System.EventArgs e) { if(this.DictionaryHT.ContainsKey(this.textBox_TestWord.Text.Trim().ToString())) { MessageBox.Show("'"+this.textBox_TestWord.Text.Trim().ToString()+"'在詞典中存在"); } else { MessageBox.Show("'"+this.textBox_TestWord.Text.Trim().ToString()+"'在詞典中不存在"); } } //對一段中文進行分句,輸入一段中文,返回一段分好句的中文 private string SplitParagraph(string paragraph) { try { //去掉對於特徵無功能的漢字或詞 //去掉前後空白字元 paragraph=paragraph.Trim(); //分隔符號進行分句 string [] split = paragraph.Split(new Char [] {' ',',', '。', ':','!','[',']','(',')','、','》','《','?','“','”'}); paragraph=null; foreach (string s in split) { if (s.Trim()!="") { //對每句話進行分詞 SplitWords(s.Trim()); paragraph+=s.Trim()+"★"; } } } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } return paragraph; } //對一字串進行分詞 private string SplitWords(string sentence) { string returnwords=null; string word=null; //逆向分詞 int N=sentence.Length; //詞的最大長度 int M=7; int i=0;//指向陣列頭 int j=N;//指向陣列尾 int k=0; int hashtablevalue; bool BFind=false; try { while(j-i>=M) { k=0; BFind=false; while(k2) { k=0; BFind=false; while(k"+"<特徵值>"+""+""); doc.Save(System.Windows.Forms.Application.StartupPath.ToString()+" //XmlFolder//"+filename+".xml"); } MessageBox.Show("添加類別成功"); } catch(Exception ex) { MessageBox.Show("NewXmlFile:"+ex.Message.ToString()); } } //添加新類別 private void button_AddNewCategorzation_Click(object sender, System.EventArgs e) { if(this.textBox_NewCategorzationName.Text.Trim()=="") { MessageBox.Show("類別名不能為空"); return; } if(!this.comboBox_Categorzations.Items.Contains(this.textBox_NewCategorzationName.Text.Trim().ToString())) { NewXmlFile(this.textBox_NewCategorzationName.Text.Trim().ToString()); AddNewCategorzation(this.textBox_NewCategorzationName.Text.Trim().ToString()); } else { MessageBox.Show("已經存在這個類別!"); } } //在指定類別中添加詞及次數 private void AddWord(string categorzation) { //添加詞及出現次數 try { XmlDocument doc=new XmlDocument(); doc.Load(System.Windows.Forms.Application.StartupPath.ToString()+" //XmlFolder//"+categorzation+".xml"); XmlNode root=doc.SelectSingleNode("特徵值"); XmlElement xmlelment=null; //從HASH表中取特徵詞進行存儲和更新 IDictionaryEnumerator myEnumerator = DictionaryHT.GetEnumerator(); while(myEnumerator.MoveNext()) { if(Int32.Parse(myEnumerator.Value.ToString())!=0) { xmlelment=(XmlElement)doc.SelectSingleNode("特徵值//"+myEnumerator.Key.ToString()); //foreach(XmlNode xn in ) if(xmlelment==null) { //不存在該節點 xmlelment=doc.CreateElement(myEnumerator.Key.ToString()); xmlelment.SetAttribute("SumTimes",myEnumerator.Value.ToString()); } else { int sumtime=Int32.Parse(xmlelment.GetAttribute("SumTimes").ToString()); sumtime+=Int32.Parse(myEnumerator.Value.ToString()); xmlelment.SetAttribute("SumTimes",sumtime.ToString()); } root.AppendChild(xmlelment); } } doc.Save(System.Windows.Forms.Application.StartupPath.ToString()+" //XmlFolder//"+categorzation+".xml"); } catch(Exception ex) { MessageBox.Show("AddWord"+ex.Message.ToString()); } } private void UpdateHT(System.Collections.Hashtable myhashtable,Hashtable sourcehashtable) { try { //把源表中的詞添加到目標表中 IDictionaryEnumerator myEnumerator = sourcehashtable.GetEnumerator(); while(myEnumerator.MoveNext()) { //在目標表中不存在該詞,則添加 if(!myhashtable.ContainsKey(myEnumerator.Key)) { myhashtable.Add(myEnumerator.Key,myEnumerator.Value); } else { myhashtable[myEnumerator.Key]=Int32.Parse(myhashtable[myEnumerator.Key].ToString())+Int32.Parse(myEnumerator.Value.ToString()); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } } //在類別檔中添加新的類別 private void AddNewCategorzation(string Name) { this.comboBox_Categorzations.Items.Add(Name); try { XmlDocument doc=new XmlDocument(); doc.Load(System.Windows.Forms.Application.StartupPath.ToString()+"//類別"+".xml"); XmlNode root=doc.SelectSingleNode("文本類別"); XmlElement xmlelment=doc.CreateElement(Name); root.AppendChild(xmlelment); doc.Save(System.Windows.Forms.Application.StartupPath.ToString()+"//類別"+".xml"); } catch(Exception ex) { MessageBox.Show("AddNewCategorzation"+ex.Message.ToString()); } } //程式載入後,將類別顯示在下列框中 private void MainFrm_Load(object sender, System.EventArgs e) { try { XmlDocument doc=new XmlDocument(); doc.Load(System.IO.Directory.GetCurrentDirectory().ToString()+"//類別"+".xml"); XmlNode root=doc.SelectSingleNode("文本類別"); XmlNodeList nodelist=root.ChildNodes; foreach(XmlNode xn in nodelist) { this.comboBox_Categorzations.Items.Add(xn.Name); } } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } } //讀取指定檔夾下的所有檔 private void button_AddSampleFolder_Click(object sender, System.EventArgs e) { //this.openFileDialog_AddDictionary.InitialDirectory=System.Windows.Forms.Application.StartupPath; this.folderBrowserDialogSamplePick.SelectedPath=(System.Windows.Forms.Application.StartupPath); if((this.folderBrowserDialogSamplePick.ShowDialog())==DialogResult.OK) { this.textBox_Sample.Clear(); this.progressBar1.Value=0; MyFile=System.IO.Directory.GetFiles(folderBrowserDialogSamplePick.SelectedPath); this.progressBar1.Maximum=MyFile.Length; for(int i=0;i=0 即當文本相對該類別的相似度達到閾值A時 Step 8   判定該文本屬於類j Step 9  endfor Step 10 若文本仍未歸類,則把文本歸入P[j]值最大的類中 Step 11 endfor */ private void button_SplitCategorzation_Click(object sender, System.EventArgs e) { try { //存儲屬於某類的概率 double CategorzationProbility=1.0; IDictionaryEnumerator myEnumerator=null; //存儲一篇文章中,特徵詞以及次數 System.Collections.Hashtable TempHT=new Hashtable(); //存儲文本中詞總數的臨時變數 int FileSumWords=0; //屬於哪個類別 string CateGorzation=null; //臨時存儲屬於每個類的概率 double TempProbility=0.0; double tempsumwords=0; //記載分到每個類別的篇數,以及檔的總數 int[] count = new int[this.comboBox_Categorzations.Items.Count]; for(int cc=0;cc=TempProbility) { TempProbility=CategorzationProbility; CateGorzation=this.comboBox_Categorzations.Items[j].ToString(); CHT=j; } this.progressBar1.Value=(i+1)*(j+1); } if(this.checkBox_Datail.Checked) { NormalMessageArrived(CutFileName(this.MyFile[i].ToString())+"/t"+CateGorzation.ToString()+"/r/n"); } count[CHT]++; if(this.checkBox_Studying.Checked) { //應該是把它載入到被分到的類別中,暫時固定做測試 UpdateHT(myhashtable[CHT],TempHT); } } NormalMessageArrived("EndTime"+DateTime.Now.ToLongTimeString()+"/r/n"); NormalMessageArrived("-------------------------------------------------------------+/r/n"); NormalMessageArrived("總分類文章數:"+MyFile.Length.ToString()+"/r/n"); for(int i=0;i"+"<特徵值>"+""+""); doc.Save(System.Windows.Forms.Application.StartupPath.ToString()+" //XmlFolder//"+this.comboBox_Categorzations.Text.Trim().ToString()+".xml"); } MessageBox.Show(this.comboBox_Categorzations.Text.Trim().ToString()+"類別,清除詞庫成功"); } } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void button_SystemHelp_Click(object sender, System.EventArgs e) { //MessageBox.Show("copyright(c)2006-2007 SCUT 雷志明"); Help myhelp=new Help(); myhelp.ShowDialog(); } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值