字符串相似度算法,编辑距离算法,Levenshtein Distance

参考:https://www.cnblogs.com/shihuajie/p/5772173.html
参考:http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html
参考:https://www.cnblogs.com/shikyoh/p/4995078.html
参考:https://blog.csdn.net/kuangjian0284/article/details/78226580

///
/// 字符串相似度算法,编辑距离算法
///
public class LevenshteinDistance
{
private static LevenshteinDistance _instance = null;
public static LevenshteinDistance Instance
{
get
{
if (_instance == null)
{
return new LevenshteinDistance();
}
return _instance;
}
}

    /// 
    /// 取最小的一位数
    /// 
    /// 
    /// 
    /// 
    /// 
    private int LowerOfThree(int first, int second, int third)
    {
        int min = first;
        if (second < min)
            min = second;
        if (third < min)
            min = third;
        return min;
    }

    private int Levenshtein_Distance(string str1, string str2)
    {
        int[,] Matrix;
        int n = str1.Length;
        int m = str2.Length;

        int temp = 0;
        char ch1;
        char ch2;
        int i = 0;
        int j = 0;
        if (n == 0)
        {
            return m;
        }
        if (m == 0)
        {
            return n;
        }
        Matrix = new int[n + 1, m + 1];

        for (i = 0; i <= n; i++)
        {
            //初始化第一列
            Matrix[i, 0] = i;
        }

        for (j = 0; j <= m; j++)
        {
            //初始化第一行
            Matrix[0, j] = j;
        }

        for (i = 1; i <= n; i++)
        {
            ch1 = str1[i - 1];
            for (j = 1; j <= m; j++)
            {
                ch2 = str2[j - 1];
                if (ch1.Equals(ch2))
                {
                    temp = 0;
                }
                else
                {
                    temp = 1;
                }
                Matrix[i, j] = LowerOfThree(Matrix[i - 1, j] + 1, Matrix[i, j - 1] + 1, Matrix[i - 1, j - 1] + temp);
            }
        }
        return Matrix[n, m];

    }

    /// 
    /// 计算字符串相似度
    /// 
    /// 
    /// 
    /// 
    public decimal LevenshteinDistancePercent(string str1, string str2)
    {
        int val = Levenshtein_Distance(str1, str2);
        return 1 - (decimal)val / Math.Max(str1.Length, str2.Length);
    }
}

你可能感兴趣的:(C#,编辑距离,字符串相似度算法,字符串近似搜索,Levenshtein,Distance算法)