Similitude/String/WeightedLevenshteinResolver.cs
/// <summary>
/// Measures the difference between two strings by counting the minimal number of single-character edits
/// required to transform one string to another. Valid edit types are 'insertion', 'deletion' and
/// 'substitution'.
/// The resultant is zero if and only if the provided strings are identical.
/// The resultant will never exceed the length of the longest provided string.
/// </summary>
/// <param name="source">The source string.</param>
/// <param name="target">The target string.</param>
/// <returns>A number in the range [0, max(first.Length, second.Length)].</returns>
Measures the difference between two strings by counting the minimal number of single-character edits
required to transform one string to another.
Measures the difference between two strings by counting the minimal number of single - character edits required to transform one string to another .
public virtual double GetDistance([NotNull] string source, [NotNull] string target)
        {
            var n = source.Length;
            var m = target.Length;
            if (n == 0)
            {
                return m;
            }
            if (m == 0)
            {
                return n;
            }
            var matrix = new int[n + 1, m + 1];
            for (var i = 0; i <= n; i++)
            {
                matrix[i, 0] = i;
            }
            for (var i = 0; i <= m; i++)
            {
                matrix[0, i] = i;
            }
            for (var x = 1; x <= n; x++)
            {
                for (var y = 1; y <= m; y++)
                {
                    var xChar = source[x - 1];
                    var yChar = target[y - 1];
                    if (!CaseSensitive)
                    {
                        xChar = char.ToLowerInvariant(xChar);
                        yChar = char.ToLowerInvariant(yChar);
                    }
                    var removalCost = matrix[x - 1, y] + RemovalWeight;
                    var insertionCost = matrix[x, y - 1] + InsertionWeight;
                    var substitutaionCost =
                        matrix[x - 1, y - 1] + Convert.ToInt32(xChar != yChar) * SubstitutaionWeight;
                    matrix[x, y] = Math.Min(removalCost, Math.Min(insertionCost, substitutaionCost));
                }
            }
            return matrix[n, m];
        }
public virtual double GetDistance ( [ NotNull ] string source , [ NotNull ] string target ) { var n = source . Length ; var m = target . Length ; if ( n == 0 ) { return m ; } if ( m == 0 ) { return n ; } var matrix = new int [ n + 1 , m + 1 ] ; for ( var i = 0 ; i <= n ; i ++ ) { matrix [ i , 0 ] = i ; } for ( var i = 0 ; i <= m ; i ++ ) { matrix [ 0 , i ] = i ; } for ( var x = 1 ; x <= n ; x ++ ) { for ( var y = 1 ; y <= m ; y ++ ) { var xChar = source [ x - 1 ] ; var yChar = target [ y - 1 ] ; if ( ! CaseSensitive ) { xChar = char . ToLowerInvariant ( xChar ) ; yChar = char . ToLowerInvariant ( yChar ) ; } var removalCost = matrix [ x - 1 , y ] + RemovalWeight ; var insertionCost = matrix [ x , y - 1 ] + InsertionWeight ; var substitutaionCost = matrix [ x - 1 , y - 1 ] + Convert . ToInt32 ( xChar != yChar ) * SubstitutaionWeight ; matrix [ x , y ] = Math . Min ( removalCost , Math . Min ( insertionCost , substitutaionCost ) ) ; } } return matrix [ n , m ] ; }
Measures the difference between two strings by counting the minimal number of single-character edits
required to transform one string to another.
Measures the difference between two strings by counting the minimal number of single - character edits required to transform one string to another .
A number in the range [0, max(first.Length, second.Length)].
A number in the range [ 0 max ( first . Length second . Length ) ] .
string
[NotNull]
source
The source string.
The source string .
target
The target string.
The target string .