[转]字符串相似度算法(编辑距离算法 Levenshtein Distance)

时间:2024-04-04 15:07:28

转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981

    http://www.cnblogs.com/ivanyb/archive/2011/11/25/2263356.html

在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录。
据百度百科介绍:
编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数,如果它们的距离越大,说明它们越是不同。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
  例如将kitten一字转成sitting:
  sitten (k→s)
  sittin (e→i)
  sitting (→g)
  俄罗斯科学家Vladimir Levenshtein在1965年提出这个概念。因此也叫Levenshtein Distance。
例如

  • 如果str1="ivan",str2="ivan",那么经过计算后等于 0。没有经过转换。相似度=1-0/Math.Max(str1.length,str2.length)=1
  • 如果str1="ivan1",str2="ivan2",那么经过计算后等于1。str1的"1"转换"2",转换了一个字符,所以距离是1,相似度=1-1/Math.Max(str1.length,str2.length)=0.8

应用  DNA分析
  拼字检查
  语音辨识
  抄袭侦测
感谢大石头在评论中给出一个很好的关于此方法应用的连接 补充在此:
小规模的字符串近似搜索,需求类似于搜索引擎中输入关键字,出现类似的结果列表,文章连接:【算法】字符串近似搜索
算法过程

  • str1或str2的长度为0返回另一个字符串的长度。 if(str1.length==0) return str2.length; if(str2.length==0) return str1.length;
  • 初始化(n+1)*(m+1)的矩阵d,并让第一行和列的值从0开始增长。
  • 扫描两字符串(n*m级的),如果:str1 == str2[j],用temp记录它,为0。否则temp记为1。然后在矩阵d[i,j]赋于d[i-1,j]+1 、d[i,j-1]+1、d[i-1,j-1]+temp三者的最小值。
  • 扫描完后,返回矩阵的最后一个值d[n][m]即是它们的距离。

计算相似度公式:1-它们的距离/两个字符串长度的最大值。

为了直观表现,我将两个字符串分别写到行和列中,实际计算中不需要。我们用字符串“ivan1”和“ivan2”举例来看看矩阵中值的状况:
1、第一行和第一列的值从0开始增长

    i v a n 1
  0 1 2 3 4 5
i 1          
v 2          
a 3          
n 4          
2 5          

2、i列值的产生 Matrix[i - 1, j] + 1 ; Matrix[i, j - 1] + 1   ;    Matrix[i - 1, j - 1] + t

    i v a n 1
  0+t=0 1+1=2 2 3 4 5
i 1+1=2 取三者最小值=0        
v 2 依次类推:1        
a 3 2        
n 4 3        
2 5 4        

3、V列值的产生

    i v a n 1
  0 1 2      
i 1 0 1      
v 2 1 0      
a 3 2 1      
n 4 3 2      
2 5 4 3      

依次类推直到矩阵全部生成

    i v a n 1
  0 1 2 3 4 5
i 1 0 1 2 3 4
v 2 1 0 1 2 3
a 3 2 1 0 1 2
n 4 3 2 1 0 1
2 5 4 3 2 1 1

最后得到它们的距离=1
相似度:1-1/Math.Max(“ivan1”.length,“ivan2”.length) =0.8

算法用C#实现

普通浏览复制代码
  1. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)showpublic class LevenshteinDistance
  2. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)    {
  3. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <summary>
  4. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// 取最小的一位数
  5. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// </summary>
  6. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <param name="first"></param>
  7. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <param name="second"></param>
  8. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <param name="third"></param>
  9. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <returns></returns>
  10. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        private int LowerOfThree(int first, int second, int third)
  11. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        {
  12. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int min = Math.Min(first, second);
  13. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            return Math.Min(min, third);
  14. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        }
  15. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  16. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        private int Levenshtein_Distance(string str1, string str2)
  17. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        {
  18. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int[,] Matrix;
  19. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int n = str1.Length;
  20. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int m = str2.Length;
  21. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  22. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int temp = 0;
  23. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            char ch1;
  24. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            char ch2;
  25. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int i = 0;
  26. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int j = 0;
  27. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            if (n == 0)
  28. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  29. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                return m;
  30. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  31. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            if (m == 0)
  32. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  33. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  34. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                return n;
  35. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  36. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            Matrix = new int[n + 1, m + 1];
  37. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  38. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            for (i = 0; i <= n; i++)
  39. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  40. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                //初始化第一列
  41. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                Matrix[i, 0] = i;
  42. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  43. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  44. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            for (j = 0; j <= m; j++)
  45. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  46. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                //初始化第一行
  47. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                Matrix[0, j] = j;
  48. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  49. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  50. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            for (i = 1; i <= n; i++)
  51. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  52. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                ch1 = str1[i - 1];
  53. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                for (j = 1; j <= m; j++)
  54. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                {
  55. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    ch2 = str2[j - 1];
  56. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    if (ch1.Equals(ch2))
  57. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    {
  58. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                        temp = 0;
  59. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    }
  60. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    else
  61. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    {
  62. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                        temp = 1;
  63. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    }
  64. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    Matrix[i, j] = LowerOfThree(Matrix[i - 1, j] + 1, Matrix[i, j - 1] + 1, Matrix[i - 1, j - 1] + temp);
  65. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                }
  66. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  67. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)      for (i = 0; i <= n; i++)
  68. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            {
  69. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                for (j = 0; j <= m; j++)
  70. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                {
  71. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                    Console.Write(" {0} ", Matrix[i, j]);
  72. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                }
  73. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)                Console.WriteLine("");
  74. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            }
  75. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  76. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            return Matrix[n, m];
  77. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        }
  78. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  79. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <summary>
  80. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// 计算字符串相似度
  81. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// </summary>
  82. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <param name="str1"></param>
  83. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <param name="str2"></param>
  84. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        /// <returns></returns>
  85. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        public decimal LevenshteinDistancePercent(string str1, string str2)
  86. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        {
  87. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            //int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
  88. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            int val = Levenshtein_Distance(str1, str2);
  89. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            return 1 - (decimal)val / Math.Max(str1.Length, str2.Length);
  90. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        }
  91. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)    }
1
<STRONG>调用</STRONG>

普通浏览复制代码
  1. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)showstatic void Main(string[] args)
  2. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        {
  3. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            string str1 = "ivan1";
  4. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            string str2 = "ivan2";
  5. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            Console.WriteLine("字符串1 {0}", str1);
  6. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  7. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            Console.WriteLine("字符串2 {0}", str2);
  8. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
  9. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            Console.WriteLine("相似度 {0} %", new LevenshteinDistance().LevenshteinDistancePercent(str1, str2) * 100);
  10. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)            Console.ReadLine();
  11. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)        }
1
<STRONG>结果</STRONG>

[转]字符串相似度算法(编辑距离算法 Levenshtein Distance) 
------------------------------
动态时间弯曲距离 dynamic time warping
1.What is 动态时间弯曲距离 <ignore_js_op>[转]字符串相似度算法(编辑距离算法 Levenshtein Distance)

在日常的生活中我们最经常使用的距离毫无疑问应该是欧式距离,但是对于一些特殊情况,欧氏距离存在着其很明显的缺陷,比如说时间序列,举个比较简单的例子,序列A:1,1,1,10,2,3,序列B:1,1,1,2,10,3,如果用欧氏距离,也就是distance[j]=(b[j]-a)*(b[j]-a)来计算的话,总的距离和应该是128,应该说这个距离是非常大的,而实际上这个序列的图像是十分相似的,这种情况下就有人开始考虑寻找新的时间序列距离的计算方法,然后提出了DTW算法,这种方法在语音识别,机器学习方便有着很重要的作用。
这个算法是基于动态规划(DP)的思想,解决了发音长短不一的模板匹配问题,简单来说,就是通过构建一个邻接矩阵,寻找最短路径和。
还以上面的2个序列作为例子,A中的10和B中的2对应以及A中的2和B中的10对应的时候,distance[3]以及distance[4]肯定是非常大的,这就直接导致了最后距离和的膨胀,这种时候,我们需要来调整下时间序列,如果我们让A中的10和B中的10 对应 ,A中的1和B中的2对应,那么最后的距离和就将大大缩短,这种方式可以看做是一种时间扭曲,看到这里的时候,我相信应该会有人提出来,为什么不能使用A中的2与B中的2对应的问题,那样的话距离和肯定是0了啊,距离应该是最小的吧,但这种情况是不允许的,因为A中的10是发生在2的前面,而B中的2则发生在10的前面,如果对应方式交叉的话会导致时间上的混乱,不符合因果关系。
接下来,以output[6][6](所有的记录下标从1开始,开始的时候全部置0)记录A,B之间的DTW距离,简单的介绍一下具体的算法,这个算法其实就是一个简单的DP,状态转移公式是output[j]=Min(Min(output[i-1][j],output[j-1]),output[i-1][j-1])+distance[j];最后得到的output[5][5]就是我们所需要的DTW距离.
2.动态时间弯曲距离程序Matlab  C++
3.在金融工程中的应用根据动态时间距离方法预测市场走势 理论基础:历史重复,使用模式匹配方法,寻找历史中与现在最接近的时段!!
(1)基于日线--动态时间距离方法预测市场走势(每日自动更新)
(2)基于日线列出最相似T0p5--动态时间距离方法预测市场走势(每日自动发送)
(3)基于交易量与价格等等。。。。