distance.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. writer : Opera Wang
  3. E-Mail : wangvisual AT sohu DOT com
  4. License: GPL
  5. */
  6. /* filename: distance.cc */
  7. /*
  8. http://www.merriampark.com/ld.htm
  9. What is Levenshtein Distance?
  10. Levenshtein distance (LD) is a measure of the similarity between two strings,
  11. which we will refer to as the source string (s) and the target string (t).
  12. The distance is the number of deletions, insertions, or substitutions required
  13. to transform s into t. For example,
  14. * If s is "test" and t is "test", then LD(s,t) = 0, because no transformations are needed.
  15. The strings are already identical.
  16. * If s is "test" and t is "tent", then LD(s,t) = 1, because one substitution
  17. (change "s" to "n") is sufficient to transform s into t.
  18. The greater the Levenshtein distance, the more different the strings are.
  19. Levenshtein distance is named after the Russian scientist Vladimir Levenshtein,
  20. who devised the algorithm in 1965. If you can't spell or pronounce Levenshtein,
  21. the metric is also sometimes called edit distance.
  22. The Levenshtein distance algorithm has been used in:
  23. * Spell checking
  24. * Speech recognition
  25. * DNA analysis
  26. * Plagiarism detection
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. //#include <stdio.h>
  31. #include "distance.h"
  32. #define OPTIMIZE_ED
  33. /*
  34. Cover transposition, in addition to deletion,
  35. insertion and substitution. This step is taken from:
  36. Berghel, Hal ; Roach, David : "An Extension of Ukkonen's
  37. Enhanced Dynamic Programming ASM Algorithm"
  38. (http://www.acm.org/~hlb/publications/asm/asm.html)
  39. */
  40. #define COVER_TRANSPOSITION
  41. /****************************************/
  42. /*Implementation of Levenshtein distance*/
  43. /****************************************/
  44. EditDistance::EditDistance()
  45. {
  46. currentelements = 2500; // It's enough for most conditions :-)
  47. d = (int*)malloc(sizeof(int) * currentelements);
  48. }
  49. EditDistance::~EditDistance()
  50. {
  51. // printf("size:%d\n",currentelements);
  52. if (d)
  53. free(d);
  54. }
  55. #ifdef OPTIMIZE_ED
  56. int EditDistance::CalEditDistance(const gunichar *s, const gunichar *t, const int limit)
  57. /*Compute levenshtein distance between s and t, this is using QUICK algorithm*/
  58. {
  59. int n = 0, m = 0, iLenDif, k, i, j, cost;
  60. // Remove leftmost matching portion of strings
  61. while ( *s && (*s == *t) )
  62. {
  63. s++;
  64. t++;
  65. }
  66. while (s[n])
  67. {
  68. n++;
  69. }
  70. while (t[m])
  71. {
  72. m++;
  73. }
  74. // Remove rightmost matching portion of strings by decrement n and m.
  75. while ( n && m && (*(s + n - 1) == *(t + m - 1)) )
  76. {
  77. n--;
  78. m--;
  79. }
  80. if ( m == 0 || n == 0 || d == (int*)0 )
  81. return (m + n);
  82. if ( m < n )
  83. {
  84. const gunichar * temp = s;
  85. int itemp = n;
  86. s = t;
  87. t = temp;
  88. n = m;
  89. m = itemp;
  90. }
  91. iLenDif = m - n;
  92. if ( iLenDif >= limit )
  93. return iLenDif;
  94. // step 1
  95. n++;
  96. m++;
  97. // d=(int*)malloc(sizeof(int)*m*n);
  98. if ( m*n > currentelements )
  99. {
  100. currentelements = m * n * 2; // double the request
  101. d = (int*)realloc(d, sizeof(int) * currentelements);
  102. if ( (int*)0 == d )
  103. return (m + n);
  104. }
  105. // step 2, init matrix
  106. for (k = 0;k < n;k++)
  107. d[k] = k;
  108. for (k = 1;k < m;k++)
  109. d[k*n] = k;
  110. // step 3
  111. for (i = 1;i < n;i++)
  112. {
  113. // first calculate column, d(i,j)
  114. for ( j = 1;j < iLenDif + i;j++ )
  115. {
  116. cost = s[i - 1] == t[j - 1] ? 0 : 1;
  117. d[j*n + i] = minimum(d[(j - 1) * n + i] + 1, d[j * n + i - 1] + 1, d[(j - 1) * n + i - 1] + cost);
  118. #ifdef COVER_TRANSPOSITION
  119. if ( i >= 2 && j >= 2 && (d[j*n + i] - d[(j - 2)*n + i - 2] == 2)
  120. && (s[i - 2] == t[j - 1]) && (s[i - 1] == t[j - 2]) )
  121. d[j*n + i]--;
  122. #endif
  123. }
  124. // second calculate row, d(k,j)
  125. // now j==iLenDif+i;
  126. for ( k = 1;k <= i;k++ )
  127. {
  128. cost = s[k - 1] == t[j - 1] ? 0 : 1;
  129. d[j*n + k] = minimum(d[(j - 1) * n + k] + 1, d[j * n + k - 1] + 1, d[(j - 1) * n + k - 1] + cost);
  130. #ifdef COVER_TRANSPOSITION
  131. if ( k >= 2 && j >= 2 && (d[j*n + k] - d[(j - 2)*n + k - 2] == 2)
  132. && (s[k - 2] == t[j - 1]) && (s[k - 1] == t[j - 2]) )
  133. d[j*n + k]--;
  134. #endif
  135. }
  136. // test if d(i,j) limit gets equal or exceed
  137. if ( d[j*n + i] >= limit )
  138. {
  139. return d[j*n + i];
  140. }
  141. }
  142. // d(n-1,m-1)
  143. return d[n*m - 1];
  144. }
  145. #else
  146. int EditDistance::CalEditDistance(const char *s, const char *t, const int limit)
  147. {
  148. //Step 1
  149. int k, i, j, n, m, cost;
  150. n = strlen(s);
  151. m = strlen(t);
  152. if ( n != 0 && m != 0 && d != (int*)0 )
  153. {
  154. m++;
  155. n++;
  156. if ( m*n > currentelements )
  157. {
  158. currentelements = m * n * 2;
  159. d = (int*)realloc(d, sizeof(int) * currentelements);
  160. if ( (int*)0 == d )
  161. return (m + n);
  162. }
  163. //Step 2
  164. for (k = 0;k < n;k++)
  165. d[k] = k;
  166. for (k = 0;k < m;k++)
  167. d[k*n] = k;
  168. //Step 3 and 4
  169. for (i = 1;i < n;i++)
  170. for (j = 1;j < m;j++)
  171. {
  172. //Step 5
  173. if (s[i - 1] == t[j - 1])
  174. cost = 0;
  175. else
  176. cost = 1;
  177. //Step 6
  178. d[j*n + i] = minimum(d[(j - 1) * n + i] + 1, d[j * n + i - 1] + 1, d[(j - 1) * n + i - 1] + cost);
  179. #ifdef COVER_TRANSPOSITION
  180. if ( i >= 2 && j >= 2 && (d[j*n + i] - d[(j - 2)*n + i - 2] == 2)
  181. && (s[i - 2] == t[j - 1]) && (s[i - 1] == t[j - 2]) )
  182. d[j*n + i]--;
  183. #endif
  184. }
  185. return d[n*m - 1];
  186. }
  187. else
  188. return (n + m);
  189. }
  190. #endif