Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / C#

Neural Dot Net Pt 3 The Adaline Network

Rate me:
Please Sign up or sign in to vote.
3.71/5 (16 votes)
23 Oct 200316 min read 73.4K   379   41  
A neural network library in C#.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace Neural_Net_Test
{
	/// <summary>
	/// Summary description for AddWordToAdalineWordFile.
	/// </summary>
	public class AddWordToAdalineWordFile : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public AddWordToAdalineWordFile()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			try
			{
				FileInfo file = new FileInfo( "AdalineWordFile.dat" );
				if( file.Exists == false )
				{
					MessageBox.Show( "Error Unable to find the AdalineWordFile" );
					this.Close();
				}

				FileStream stream = new FileStream( file.FullName, FileMode.Open, FileAccess.ReadWrite );
				StreamReader reader = new StreamReader( stream );
				string strString;
				double dValue = 0.0;
				
				while( reader.Peek() > -1 )
				{
					strString = reader.ReadLine();
					ListViewItem item = this.listView1.Items.Add( strString );
					
					for( int i=0; i<strString.Length; i++ )
					{
						dValue = strString[ i ] *i+1;
					}

					item.SubItems.Add( dValue.ToString() ); 
				}

				reader.Close();
			}
			catch( IOException ioExp )
			{
				MessageBox.Show( "Error io exception thrown " + ioExp.Message );
			}

			listView1.Sort();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AddWordToAdalineWordFile));
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2});
			this.listView1.FullRowSelect = true;
			this.listView1.Location = new System.Drawing.Point(8, 80);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(448, 97);
			this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listView1.TabIndex = 0;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Words";
			this.columnHeader1.Width = 186;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Value";
			this.columnHeader2.Width = 231;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(24, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(256, 23);
			this.label1.TabIndex = 2;
			this.label1.Text = "Enter the word to add to the file";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(32, 208);
			this.button1.Name = "button1";
			this.button1.TabIndex = 3;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.OnOK);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(328, 208);
			this.button2.Name = "button2";
			this.button2.TabIndex = 4;
			this.button2.Text = "Cancel";
			this.button2.Click += new System.EventHandler(this.OnCancel);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(192, 40);
			this.button3.Name = "button3";
			this.button3.TabIndex = 5;
			this.button3.Text = "Add";
			this.button3.Click += new System.EventHandler(this.OnAdd);
			// 
			// AddWordToAdalineWordFile
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(472, 246);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.listView1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "AddWordToAdalineWordFile";
			this.Text = "Add Word To Adaline Word File";
			this.ResumeLayout(false);

		}
		#endregion

		private void OnOK(object sender, System.EventArgs e)
		{
			FileInfo file = new FileInfo( "AdalineWordFile.dat" );
			if( file.Exists == false )
			{
				MessageBox.Show( "Error Unable to find the AdalineWordFile" );
				this.Close();
			}
			else
				file.Delete();

			FileStream stream = new FileStream( file.FullName, FileMode.Create, FileAccess.ReadWrite );
			StreamWriter writer = new StreamWriter( stream );

			ListView.ListViewItemCollection col = listView1.Items;

			foreach( ListViewItem item in col )
			{
				writer.WriteLine( item.Text );
			}

			writer.Close();

			this.Close();
		}

		private void OnCancel(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void OnAdd(object sender, System.EventArgs e)
		{
			ListViewItem item = new ListViewItem( textBox1.Text );
			double dValue = 0.0;
			string strTemp = textBox1.Text;
			string strCompare;
			bool bFound = false;

			for( int i=0; i<listView1.Items.Count; i++ )
			{
				strCompare = listView1.Items[ i ].Text;
				strCompare = strCompare.TrimEnd( ' ', '\n', '\r' );				
				if( strCompare == strTemp )
				{
					bFound = true;
					i = listView1.Items.Count;
				}
			}

			if( bFound == true )
			{
				MessageBox.Show( "The word " + strTemp + " is already in the list " );
				return;
			}
			
			for( int i=0; i<strTemp.Length; i++ )
			{
				dValue += strTemp[ i ] *i+1;
			}

			item.SubItems.Add( dValue.ToString() );

			listView1.Items.Add( item );
			listView1.Sort();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions