コード例 #1
0
        private int GetCommonPrefix(String str1, String str2)
        {
            if (str1 == null || str2 == null)
            {
                return(this.prefixLength);
            }

            int commonPrefix = 0;
            int length       = MathExtension.Min(str1.Length,
                                                 str2.Length,
                                                 this.prefixLength);

            // check for prefix similarity of length n
            for (int i = 0; i < length; i++)
            {
                // check the prefix is the same so far
                if (str1[i] == str2[i])
                {
                    // count up
                    commonPrefix++;
                }
            }

            return(commonPrefix);
        }
コード例 #2
0
        public int BuildDamerauLevenshteinDistance(string str1, string str2)
        {
            if (str1 == null || str2 == null)
            {
                return(0);
            }

            int length1 = str1.Length;
            int length2 = str2.Length;

            if (length1 == 0)
            {
                return(length2);
            }

            if (length2 == 0)
            {
                return(length1);
            }

            int[,] matrix = new int[str1.Length + 1, str2.Length + 1];

            // initialize matrix
            for (int i = 0; i <= length1; i++)
            {
                matrix[i, 0] = i;
            }
            for (int j = 0; j <= length2; j++)
            {
                matrix[0, j] = j;
            }

            // analyze
            for (int i = 1; i <= length1; i++)
            {
                for (int j = 1; j <= length2; j++)
                {
                    int deletionCost  = 1;
                    int insertionCost = 1;

                    // substitution is based on a cost function for different chars. e.g. m->n has lower cost than a->X
                    int substitionCost = (int)this.CostFunction.GetCost(str1, i - 1, str2, j - 1);

                    int del   = matrix[i - 1, j];       // above is deletion
                    int ins   = matrix[i, j - 1];       // left is insertion
                    int subst = matrix[i - 1, j - 1];   // diagonal is substitution

                    // new cell is the minium of all three precessors (left, diagonal, above).
                    // So the minumium moves to this cell plus the cost of the operation
                    int cell = MathExtension.Min(del + deletionCost,
                                                 ins + insertionCost,
                                                 subst + substitionCost);

                    // transposition e.g. "ab" <-> "ba" (Damerau-extension to Levensthein)
                    if (i > 1 && j > 1)
                    {
                        int transposition = matrix[i - 2, j - 2] + 1;
                        if (str1[i - 2] != str2[j - 1])
                        {
                            transposition++;
                        }
                        if (str1[i - 1] != str2[j - 2])
                        {
                            transposition++;
                        }
                        //trans += (int)this.CostFunction.GetCost(str1, i - 2, str2, j - 1);
                        //trans += (int)this.CostFunction.GetCost(str1, i - 1, str2, j - 2);
                        cell = MathExtension.Min(cell,
                                                 transposition);
                    }

                    // put the new cellvalue back in the matrix
                    matrix[i, j] = cell;
                }
            }

            // return the last cell (right, bottom) of the matrix
            return(matrix[length1, length2]);
        }