Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Fuzzy Logic Vs Adaline Neural Network

, 29 Oct 2003
An experiment to see if it possible to duplicate the behavior of the Adaline Network using Fuzzy Logic.
fuzzylogicvadaline.zip
Fuzzy Logic Vs Adaline Neural Network
Fuzzy Logic Library
Fuzzy Logic Library.csproj.user
Fuzzy Logic Vs Adaline Neural Network
App.ico
bin
Debug
Fuzzy Logic Library.dll
Fuzzy Logic Vs Adaline Neural Network.exe
Lin2Var.trn
Fuzzy Logic Vs Adaline Neural Network.csproj.user
using System;
using System.Text;
using System.Collections;

namespace Fuzzy_Logic_Library
{
	/// <summary>
	/// Summary description for FuzzyWord.
	/// </summary>
	public class FuzzyWord : FuzzyBasic
	{

		private string strWord;
		private double dMinMaxDifference;
		private double dComparedMembership;
		private char[] punctuationArray; 
		private int nMinimumLetters;
		private int nValueDifference;
		private bool bAcceptPlurals;
		private int nMaximumIncorrectLetters;
		private bool bLowerCase;

		public string Word
		{
			get
			{
				return strWord;
			}
			set
			{
				strWord = value;

				/// get the value for the word
				double dValue = 0;
				for( int i=0; i<strWord.Length; i++ )
				{
					dValue += strWord[ i ];
				}

				/// default to 10 if no minmax difference supplied
				if( dMinMaxDifference == 0.0 )
				{
					dMinMaxDifference = 10.0;
				}

				this.Maximum = dValue + dMinMaxDifference;
				this.Minimum = dValue - dMinMaxDifference;

				this.Number = dValue;
			}
		}

		public double ComparedMembership
		{
			get
			{
				return dComparedMembership;
			}
			set
			{
				dComparedMembership = value;
			}
		}

		public int MinimumMatchingLetters
		{
			get
			{
				return nMinimumLetters;
			}
			set
			{
				nMinimumLetters = value;
			}
		}

		public int ValueDifference
		{
			get
			{
				return nValueDifference;
			}
			set
			{
				nValueDifference = value;
			}
		}

		public bool AcceptPlurals 
		{
			get
			{
				return bAcceptPlurals;
			}
			set
			{
				bAcceptPlurals = value;
			}
		}

		public int MaximumIncorrectLetters
		{
			get
			{
				return nMaximumIncorrectLetters;
			}
			set
			{
				nMaximumIncorrectLetters = value;
			}
		}

		public bool LowerCase
		{
			get
			{
				return bLowerCase;
			}
			set
			{
				bLowerCase = value;
			}
		}
		

		public FuzzyWord() : base()
		{
			//
			// TODO: Add constructor logic here
			//
			dMinMaxDifference = 0.0;
			dComparedMembership = 0.0;
			strWord = null;
			nMinimumLetters = 3;
			nValueDifference = 10;
			bAcceptPlurals = true;
			nMaximumIncorrectLetters = 2;
			bLowerCase = false;

			punctuationArray = new char[ 5 ];
			punctuationArray[ 0 ] = ';';
			punctuationArray[ 1 ] = ',';
			punctuationArray[ 2 ] = '.';
			punctuationArray[ 3 ] = ':';
			punctuationArray[ 4 ] = '?';
		}

		public FuzzyWord( string word ) : base()
		{
			dMinMaxDifference = 0.0;
			dComparedMembership = 0.0;
			Word = word;
			nMinimumLetters = 3;
			nValueDifference = 10;
			bAcceptPlurals = true;
			nMaximumIncorrectLetters = 2;
			bLowerCase = false;

			punctuationArray = new char[ 5 ];
			punctuationArray[ 0 ] = ';';
			punctuationArray[ 1 ] = ',';
			punctuationArray[ 2 ] = '.';
			punctuationArray[ 3 ] = ':';
			punctuationArray[ 4 ] = '?';
		}

		public FuzzyWord( string word, double minMaxDifference )
		{
			dMinMaxDifference = minMaxDifference;
			Word = word;
			dComparedMembership = 0.0;
			nMinimumLetters = 3;
			nValueDifference = 10;
			bAcceptPlurals = true;
			nMaximumIncorrectLetters = 2;
			bLowerCase = false;

			punctuationArray = new char[ 5 ];
			punctuationArray[ 0 ] = ';';
			punctuationArray[ 1 ] = ',';
			punctuationArray[ 2 ] = '.';
			punctuationArray[ 3 ] = ':';
			punctuationArray[ 4 ] = '?';
		}


		/// <summary>
		/// compare two fuzzy words
		/// </summary>
		/// <param name="comparison"></param>
		/// <returns></returns>
		public FuzzyWord Compare( string comparison )
		{

			if( comparison == "" || comparison == null )
			{
				return null;
			}

			/// check the passed in string for any punctuation marks if they exist 
			/// remove them

			if( comparison.IndexOfAny( punctuationArray ) != -1 )
			{
				/// special case Origin of the species has the punctuation " ,- " which gives a bad value
				if( comparison.LastIndexOf( '-' ) != -1 )
					comparison = comparison.Remove( comparison.Length -1, 1 );

				comparison = comparison.Remove( comparison.Length -1, 1 );
				if( comparison == "" || comparison == null )
					return null;
			}

			/// check if not case sensitive
			if( LowerCase == true )
			{
				comparison = comparison.ToLower();
			}

			/// check if plurals are accepted.
			if( AcceptPlurals == true )
			{
				/// treat plurals as acceptable 
				if( comparison[ comparison.Length -1 ] == 's' || comparison[ comparison.Length - 1 ] == 'S' )
				{
					string strTest = comparison.Remove( comparison.Length -1, 1 );
					if( strTest == "" || strTest == null )
						return null;

					/// check for apostrophy s 
					if( strTest[ strTest.Length -1 ] == '\'' )
					{
						strTest = strTest.Remove( strTest.Length -1, 1 );
					}
															 
					if( strTest == Word )
					{
						FuzzyWord temp = new FuzzyWord( comparison );
						temp.ComparedMembership = 1.0;
						return temp;				
					}
				}
			}


			if( comparison == Word )
			{
				FuzzyWord temp = new FuzzyWord( comparison );
				temp.ComparedMembership = 1.0;
				return temp;
			}

			/// get the value for the comparison
			double dValue = 0;
			for( int i=0; i<comparison.Length; i++ )
			{
				dValue += comparison[ i ];
			}


			/// get the comparison between the two ( the number of letters the same )
			int nCount = 0;

			for( int i=0; i<Word.Length; i++ )
			{
				if( i < Word.Length && i < comparison.Length )
				{
					if( Word[ i ] == comparison[ i ] )
					{
						nCount++;
					}
				}
			}


			/// allow a minimum number of correct letters
			if( nCount >= this.MinimumMatchingLetters )
			{
				if( comparison.Length > Word.Length && nCount + this.MaximumIncorrectLetters >= comparison.Length )
				{

					FuzzyWord temp = new FuzzyWord( comparison );

					/// set the membership value
					
					if( dValue >= this.Number - nValueDifference && dValue <= this.Number + nValueDifference )
						temp.ComparedMembership = 1.12;
					else if( dValue >= this.Number - ( nValueDifference * 2 ) && dValue <= this.Number + ( nValueDifference * 2 ) )
						temp.ComparedMembership = 1.25;
					else if( dValue >= this.Number - ( nValueDifference * 3 ) && dValue <= this.Number + ( nValueDifference * 3 ) )
						temp.ComparedMembership = 1.37;
					else if( dValue >= this.Number - ( nValueDifference * 4 ) && dValue <= this.Number + ( nValueDifference * 4 ) )
						temp.ComparedMembership = 1.50;
					else if( dValue >= this.Number - ( nValueDifference * 5 ) && dValue <= this.Number + ( nValueDifference * 5 ) )
						temp.ComparedMembership = 1.63;
					else if( dValue >= this.Number - ( nValueDifference * 6 ) && dValue <= this.Number + ( nValueDifference * 6 ) )
						temp.ComparedMembership = 1.75;
					else 
						temp.ComparedMembership = 1.87;

					return temp;
				}

				if( comparison.Length <= Word.Length && Word.Length - this.MaximumIncorrectLetters > 0 )
				{

					FuzzyWord temp = new FuzzyWord( comparison );

					/// set the membership value
					
					if( dValue >= this.Number - nValueDifference && dValue <= this.Number + nValueDifference )
						temp.ComparedMembership = 0.87;
					else if( dValue >= this.Number - ( nValueDifference * 2 ) && dValue <= this.Number + ( nValueDifference * 2 ) )
						temp.ComparedMembership = 0.75;
					else if( dValue >= this.Number - ( nValueDifference * 3 ) && dValue <= this.Number + ( nValueDifference * 3 ) )
						temp.ComparedMembership = 0.63;
					else if( dValue >= this.Number - ( nValueDifference * 4 ) && dValue <= this.Number + ( nValueDifference * 4 ) )
						temp.ComparedMembership = 0.50;
					else if( dValue >= this.Number - ( nValueDifference * 5 ) && dValue <= this.Number + ( nValueDifference * 5 ) )
						temp.ComparedMembership = 0.37;
					else if( dValue >= this.Number - ( nValueDifference * 6 ) && dValue <= this.Number + ( nValueDifference * 6 ) )
						temp.ComparedMembership = 0.25;
					else 
						temp.ComparedMembership = 0.12;

					return temp;
				}
			}

			return null;
		}
	}

	/// <summary>
	///  Class for holding complete sentences
	/// </summary>
	public class FuzzySentence : FuzzyBasic
	{
		private ArrayList arraySentence;
		private char finalPunctuation;


		public string Sentence
		{
			get
			{
				StringBuilder strTemp = new StringBuilder();
	
				for( int i=0; i<arraySentence.Count; i++ )
				{
					strTemp.Append( ( ( FuzzyWord )arraySentence[ i ] ).Word );
					if( i != arraySentence.Count )
						strTemp.Append( " " );
					else
						strTemp.Append( finalPunctuation ); 
				}

				return strTemp.ToString();
			}
		}


		/// <summary>
		///  Basic Constructor
		/// </summary>
		public FuzzySentence() : base()
		{
			arraySentence = new ArrayList();
		}

		/// <summary>
		/// Constructor that takes the string for the sentence
		/// </summary>
		/// <param name="strSentence"></param>
		public FuzzySentence( string strSentence ) : base()
		{
			arraySentence = new ArrayList();

			AddSentence( strSentence );
		}


		/// <summary>
		/// add a single word to the sentence
		/// </summary>
		/// <param name="fuzzyWord"></param>
		public void AddWord( FuzzyWord fuzzyWord )
		{
			arraySentence.Add( fuzzyWord );
		}

		/// <summary>
		/// add a single word to the sentence
		/// </summary>
		/// <param name="strWord"></param>
		public void AddWord( string strWord )
		{
			arraySentence.Add( strWord );
		}

		/// <summary>
		/// Add the sentence
		/// </summary>
		/// <param name="strSentence"></param>
		public void AddSentence( string strSentence )
		{
			/// clear out any current sentence
			arraySentence.Clear();

			int nCount = 0;
			/// extract the words
			for( int i=0; i<strSentence.Length; i++ )
			{
				/// if space found a word
				if( strSentence[ i ] == ' ' )
				{
					arraySentence.Add( new FuzzyWord( strSentence.Substring( nCount, i-nCount ) ) );
					nCount = i;
				}

				/// check if found the end of the sentence
				if( strSentence[ i ] == '.' || strSentence[ i ] == '?' )
				{
					arraySentence.Add( new FuzzyWord( strSentence.Substring( nCount, i-1-nCount ) ) );
					finalPunctuation = strSentence[ i ];
				}
			}
		}
	}


	/// <summary>
	/// A paragraph is a collection of sentences
	/// </summary>
	public class FuzzyParagraph : FuzzyBasic
	{
		ArrayList arrayParagraph;
		
		public string Paragraph
		{
			get
			{
				StringBuilder strTemp = new StringBuilder();

				for( int i=0; i<arrayParagraph.Count; i++ )
				{
					strTemp.Append( ( ( FuzzySentence )arrayParagraph[ i ] ).Sentence );
					if( i != arrayParagraph.Count )
						strTemp.Append( " " );
				}

				return strTemp.ToString();
			}
		}

		/// <summary>
		///  basic constructor
		/// </summary>
		public FuzzyParagraph() : base()
		{
			arrayParagraph = new ArrayList();
		}


		/// <summary>
		/// constructor taking the paragraph as a string
		/// </summary>
		/// <param name="strParagraph"></param>
		public FuzzyParagraph( string strParagraph ) : base()
		{
			arrayParagraph = new ArrayList();
			AddParagraph( strParagraph );
		}

		/// <summary>
		/// build the paragraph string by string
		/// </summary>
		/// <param name="fuzzySentence"></param>
		public void AddSentence( FuzzySentence fuzzySentence )
		{
			arrayParagraph.Add( fuzzySentence );
		}

		/// <summary>
		/// add a whole paragraph
		/// </summary>
		/// <param name="strParagragh"></param>
		public void AddParagraph( string strParagraph )
		{
			arrayParagraph.Clear();

			StringBuilder strString = new StringBuilder();

			for( int i=0; i<strParagraph.Length; i++ )
			{
				/// add each sentence to the paragraph
				if( strParagraph[ i ] == '.' || strParagraph[ i ] == '?' )
				{
					strString.Append( strParagraph[ i ] );

					arrayParagraph.Add( new FuzzySentence( strString.ToString() ) );
				}

				strString.Append( strParagraph[ i ] );
			}
		}
	}

	/// <summary>
	/// A set to hold a collection of fuzzy paragraphs
	/// Not sure if this should inherit from fuzzy set at the moment see how things work out
	/// </summary>
	public class FuzzyParagraphSet : FuzzySet
	{
		public FuzzyParagraphSet() : base()
		{
		}

		public FuzzyParagraphSet( string strName ) : base()
		{
			this.Name = strName;
		}
	}
}

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

Share

About the Author

pseudonym67

United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 30 Oct 2003
Article Copyright 2003 by pseudonym67
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid