`
mtnt2008
  • 浏览: 363623 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

levenshtein distance

阅读更多

字符串编辑距离(levenshtein distace莱文史特距离)是一种字符串之间相似度算法。对于中文来说,很多时候都是将词作为一个基本单位,而不是字符。

 

算 法描述:(算法是由俄国科学家Levenshtein提出)

 

Step Description
1 Set n to be the length of s.
Set m to be the length of t.
If n = 0, return m and exit.
If m = 0, return n and exit.
Construct a matrix containing 0..m rows and 0..n columns.
2 Initialize the first row to 0..n.
Initialize the first column to 0..m.
3 Examine each character of s (i from 1 to n).
4 Examine each character of t (j from 1 to m).
5 If s[i] equals t[j], the cost is 0.
If s[i] doesn't equal t[j], the cost is 1.
6 Set cell d[i,j] of the matrix equal to the minimum of:
a. The cell immediately above plus 1: d[i-1,j] + 1.
b. The cell immediately to the left plus 1: d[i,j-1] + 1.
c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.
7 After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].

 

 

java实现:

 

  1. Java   
  2.   
  3. public   class  Distance {   
  4.   
  5.    //****************************   
  6.    // Get minimum of three values   
  7.    //****************************   
  8.   
  9.    private   int  Minimum ( int  a,  int  b,  int  c) {   
  10.    int  mi;   
  11.   
  12.     mi = a;   
  13.      if  (b < mi) {   
  14.       mi = b;   
  15.     }   
  16.      if  (c < mi) {   
  17.       mi = c;   
  18.     }   
  19.      return  mi;   
  20.   
  21.   }   
  22.   
  23.    //*****************************   
  24.    // Compute Levenshtein distance   
  25.    //*****************************   
  26.   
  27.    public   int  LD (String s, String t) {   
  28.    int  d[][];  // matrix   
  29.    int  n;  // length of s   
  30.    int  m;  // length of t   
  31.    int  i;  // iterates through s   
  32.    int  j;  // iterates through t   
  33.    char  s_i;  // ith character of s   
  34.    char  t_j;  // jth character of t   
  35.    int  cost;  // cost   
  36.   
  37.      // Step 1   
  38.   
  39.     n = s.length ();   
  40.     m = t.length ();   
  41.      if  (n ==  0 ) {   
  42.        return  m;   
  43.     }   
  44.      if  (m ==  0 ) {   
  45.        return  n;   
  46.     }   
  47.     d =  new   int [n+ 1 ][m+ 1 ];   
  48.   
  49.      // Step 2   
  50.   
  51.      for  (i =  0 ; i <= n; i++) {   
  52.       d[i][ 0 ] = i;   
  53.     }   
  54.   
  55.      for  (j =  0 ; j <= m; j++) {   
  56.       d[ 0 ][j] = j;   
  57.     }   
  58.   
  59.      // Step 3   
  60.   
  61.      for  (i =  1 ; i <= n; i++) {   
  62.   
  63.       s_i = s.charAt (i -  1 );   
  64.   
  65.        // Step 4   
  66.   
  67.        for  (j =  1 ; j <= m; j++) {   
  68.   
  69.         t_j = t.charAt (j -  1 );   
  70.   
  71.          // Step 5   
  72.   
  73.          if  (s_i == t_j) {   
  74.           cost =  0 ;   
  75.         }   
  76.          else  {   
  77.           cost =  1 ;   
  78.         }   
  79.   
  80.          // Step 6   
  81.   
  82.         d[i][j] = Minimum (d[i- 1 ][j]+ 1 , d[i][j- 1 ]+ 1 , d[i- 1 ][j- 1 ] + cost);   
  83.   
  84.       }   
  85.   
  86.     }   
  87.   
  88.      // Step 7   
  89.   
  90.      return  d[n][m];   
  91.   
  92.   }   
  93.   
  94. }  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics