www.pudn.com > classifier.rar > calMI.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Data; 
 
namespace classifier 
{ 
	///  
	/// calMI 的摘要说明。 
	///  
	public class calMI : System.Windows.Forms.Form 
	{ 
		private System.Windows.Forms.Label label1; 
		private System.Windows.Forms.ComboBox comboBox1; 
		private System.Windows.Forms.Button calBtn; 
		private System.Windows.Forms.DataGrid resultGrid; 
		///  
		/// 必需的设计器变量。 
		///  
		private System.ComponentModel.Container components = null; 
 
		public calMI() 
		{ 
			// 
			// Windows 窗体设计器支持所必需的 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
			// 
		} 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if(components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows 窗体设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
		/// 此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			this.label1 = new System.Windows.Forms.Label(); 
			this.comboBox1 = new System.Windows.Forms.ComboBox(); 
			this.calBtn = new System.Windows.Forms.Button(); 
			this.resultGrid = new System.Windows.Forms.DataGrid(); 
			((System.ComponentModel.ISupportInitialize)(this.resultGrid)).BeginInit(); 
			this.SuspendLayout(); 
			//  
			// label1 
			//  
			this.label1.Location = new System.Drawing.Point(16, 32); 
			this.label1.Name = "label1"; 
			this.label1.Size = new System.Drawing.Size(152, 23); 
			this.label1.TabIndex = 0; 
			this.label1.Text = "选择要维护的类别:"; 
			//  
			// comboBox1 
			//  
			this.comboBox1.Location = new System.Drawing.Point(208, 32); 
			this.comboBox1.Name = "comboBox1"; 
			this.comboBox1.Size = new System.Drawing.Size(121, 20); 
			this.comboBox1.TabIndex = 1; 
			this.comboBox1.Text = "comboBox1"; 
			//  
			// calBtn 
			//  
			this.calBtn.Location = new System.Drawing.Point(424, 32); 
			this.calBtn.Name = "calBtn"; 
			this.calBtn.TabIndex = 2; 
			this.calBtn.Text = "计算"; 
			this.calBtn.Click += new System.EventHandler(this.calBtn_Click); 
			//  
			// resultGrid 
			//  
			this.resultGrid.DataMember = ""; 
			this.resultGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; 
			this.resultGrid.Location = new System.Drawing.Point(16, 72); 
			this.resultGrid.Name = "resultGrid"; 
			this.resultGrid.Size = new System.Drawing.Size(552, 344); 
			this.resultGrid.TabIndex = 3; 
			//  
			// calMI 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(592, 461); 
			this.Controls.Add(this.resultGrid); 
			this.Controls.Add(this.calBtn); 
			this.Controls.Add(this.comboBox1); 
			this.Controls.Add(this.label1); 
			this.Name = "calMI"; 
			this.Text = "calMI"; 
			this.Load += new System.EventHandler(this.calMI_Load); 
			((System.ComponentModel.ISupportInitialize)(this.resultGrid)).EndInit(); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		//窗口生成 
		private void calMI_Load(object sender, System.EventArgs e) 
		{ 
			OleDbConnection sqlCon=db.dbOpertation.getConnection(); 
 
			if(sqlCon.State==ConnectionState.Closed) 
			{ 
				sqlCon.Open(); 
			} 
			string cmd="select * from typeTable"; 
			comboBox1.Items.Clear(); 
			OleDbCommand sqlCom=new OleDbCommand(cmd,sqlCon); 
			OleDbDataReader sqlRead=sqlCom.ExecuteReader(); 
			while(sqlRead.Read()) 
			{ 
				comboBox1.Items.Add(sqlRead["typeName"]); 
			} 
			comboBox1.SelectedItem=comboBox1.Items[0]; 
			sqlRead.Close(); 
 
			sqlCon.Close(); 
		} 
 
		//计算类别的MI值 
		private void calBtn_Click(object sender, System.EventArgs e) 
		{ 
			string str; 
			//互信息, 
			double MI=0; 
			//词在类C出现的次数 
			double wordClass; 
			//训练文档总数 
			double docSum=0; 
			//包含 词t 的文档总数 
			double wordSum=0; 
			//c类的文档总数 
			double classSum=0; 
			DataSet ds; 
			OleDbConnection sqlCon=db.dbOpertation.getConnection(); 
			DataRow [] drs; 
			DataTable sumTable,classTable; 
			DataRow dr; 
			if(sqlCon.State==ConnectionState.Closed) 
			{ 
				sqlCon.Open(); 
			} 
			string cmd; 
			cmd="select sum(times) as bb from scanFile where typeName=\'"+comboBox1.Text+"\'"; 
 
			OleDbCommand sqlCom; 
			sqlCom=new OleDbCommand(cmd,sqlCon); 
			OleDbDataReader sqlRead; 
			sqlRead=sqlCom.ExecuteReader(); 
			 
			if(sqlRead.Read()) 
			{ 
				str=sqlRead["bb"].ToString().Trim(); 
				if(str==""||str=="0") 
				{ 
					sqlRead.Close(); 
					sqlCon.Close(); 
					MessageBox.Show("文章数目统计有误"); 
				} 
				else 
				{ 
					classSum=Convert.ToDouble(str); 
					sqlRead.Close(); 
					cmd="select sum(times) as bb from scanFile"; 
					sqlCom.CommandText=cmd; 
					sqlRead=sqlCom.ExecuteReader(); 
					if(sqlRead.Read()) 
					{ 
						str=sqlRead["bb"].ToString().Trim(); 
						if(str==""||str=="0") 
						{ 
							sqlRead.Close(); 
							sqlCon.Close(); 
							MessageBox.Show("文章数目统计有误"); 
						} 
						else 
						{ 
							docSum=Convert.ToDouble(str); 
 
							sqlRead.Close(); 
 
							ds=new DataSet(); 
							cmd="select * from "+comboBox1.Text+"2"; 
							OleDbDataAdapter sqlAd=new OleDbDataAdapter(cmd,sqlCon); 
							OleDbCommandBuilder sqlBuilder=new OleDbCommandBuilder(sqlAd); 
							sqlAd.Fill(ds,"classTable"); 
							 
							cmd="select * from wordSum2"; 
							OleDbDataAdapter sqlAd1=new OleDbDataAdapter(cmd,sqlCon); 
							sqlAd1.Fill(ds,"sumTable"); 
 
							classTable=ds.Tables["classTable"]; 
							sumTable=ds.Tables["sumTable"]; 
 
 
							for(int i=0;i