예제 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            char[] EscapeChars = "~!@#$%^&*()_+`-=[]\\{}|;':\",./<>? ".ToCharArray();
            for (int i = 0; i < EscapeChars.Length; i++) comparableString.EscapeWords.Add(EscapeChars[i].ToString());
            //comparableString.EscapeWords.AddRange("~!@#$%^&*()_+`-=[]\\{}|;':\",./<>?".ToCharArray()); // new string[] { "-", ".", "/", "\\", "'", "\"", " ", ",", "(", ")", "+", "`", "~", "!", "@", "#", "$", "%", "^",  }

            //comparableString str1 = new comparableString("anshita private limited");
            //comparableString str2 = new comparableString("anshita pvt ltd");
            comparableString str1 = new comparableString("A.");
            comparableString str2 = new comparableString("A..");

            //MessageBox.Show(compare_string(str1, str2).ToString());

            //this.Close();
        }
예제 #2
0
        private int compare_string(comparableString str1, comparableString str2)
        {
            // non comparable // match full string are same
            if (str1.length == 0) return 0;

            List<string> match_string = new List<string>();

            for (int i = 0; i < str1.length; i++)
            {
                int ix = -1;
                int startIndex = 0;

                while (startIndex < str2.length && (ix = str2.IndexOf(str1[i, 1], startIndex)) != -1)
                {
                    int j = 1;
                    string s = str1[i, 1];
                    while (i + j < str1.length && ix + j < str2.length && str1[i + j] == str2[ix + j])
                    {
                        s += str1[i + j];
                        j++;
                    }

                    startIndex += ix + j;

                    match_string.Add(s);
                }
            }

            // sorting by length
            for (int i = 0; i < match_string.Count - 1; i++)
            {
                for (int j = i + 1; j < match_string.Count; j++)
                {
                    if (match_string[i].Length < match_string[j].Length)
                    {
                        string tempString = match_string[i];
                        match_string[i] = match_string[j];
                        match_string[j] = tempString;
                    }
                }
            }

            int[] str1_cleaned = new int[str1.length];
            int[] str2_cleaned = new int[str2.length];

            int match_count = 0;

            for (int i = 0; i < match_string.Count; i++)
            {
                int ix1 = 0;
                int ix2 = 0;

                while (ix1 < str1.length && (ix1 = str1.IndexOf(match_string[i], ix1)) != -1)
                {
                    if (str1_cleaned[ix1] == 0) break;
                    ix1++;
                }

                while (ix2 < str2.length && (ix2 = str2.IndexOf(match_string[i], ix2)) != -1)
                {
                    if (str2_cleaned[ix2] == 0) break;
                    ix2++;
                }

                if (ix1 == -1 || ix2 == -1 || ix1 >= str1.length || ix2 >= str2.length) continue;

                match_count += (int)Math.Pow(match_string[i].Length, 2); //match_string[i].Length * 2 - 1; //
                for (int j = 0; j < match_string[i].Length; j++)
                {
                    str1_cleaned[ix1 + j] = 1;
                    str2_cleaned[ix2 + j] = 1;
                }
            }

            //bool isContinues = false;
            //int continues_match_count = 0;
            //for (int i = 0; i < str1_cleaned.Length; i++)
            //{
            //    if (str1_cleaned[i] == 1)
            //    {
            //        match_count++;
            //        continues_match_count++;
            //        if (isContinues)
            //        {
            //            //match_count++;
            //            match_count += (continues_match_count - 1) * 2;
            //        }
            //        isContinues = true;
            //    }
            //    else
            //    {
            //        isContinues = false;
            //        continues_match_count = 0;
            //    }
            //}

            int result = (match_count * 100) / (int)Math.Pow(Math.Max(str1.length, str2.length), 2); // (Math.Max(str1.length, str2.length)*2-1); //
            return result;
        }