THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5KB

  1. #include "cache.h"
  2. #include "levenshtein.h"
  3. /*
  4. * This function implements the Damerau-Levenshtein algorithm to
  5. * calculate a distance between strings.
  6. *
  7. * Basically, it says how many letters need to be swapped, substituted,
  8. * deleted from, or added to string1, at least, to get string2.
  9. *
  10. * The idea is to build a distance matrix for the substrings of both
  11. * strings. To avoid a large space complexity, only the last three rows
  12. * are kept in memory (if swaps had the same or higher cost as one deletion
  13. * plus one insertion, only two rows would be needed).
  14. *
  15. * At any stage, "i + 1" denotes the length of the current substring of
  16. * string1 that the distance is calculated for.
  17. *
  18. * row2 holds the current row, row1 the previous row (i.e. for the substring
  19. * of string1 of length "i"), and row0 the row before that.
  20. *
  21. * In other words, at the start of the big loop, row2[j + 1] contains the
  22. * Damerau-Levenshtein distance between the substring of string1 of length
  23. * "i" and the substring of string2 of length "j + 1".
  24. *
  25. * All the big loop does is determine the partial minimum-cost paths.
  26. *
  27. * It does so by calculating the costs of the path ending in characters
  28. * i (in string1) and j (in string2), respectively, given that the last
  29. * operation is a substitution, a swap, a deletion, or an insertion.
  30. *
  31. * This implementation allows the costs to be weighted:
  32. *
  33. * - w (as in "sWap")
  34. * - s (as in "Substitution")
  35. * - a (for insertion, AKA "Add")
  36. * - d (as in "Deletion")
  37. *
  38. * Note that this algorithm calculates a distance _iff_ d == a.
  39. */
  40. int levenshtein(const char *string1, const char *string2,
  41. int w, int s, int a, int d)
  42. {
  43. int len1 = strlen(string1), len2 = strlen(string2);
  44. int *row0, *row1, *row2;
  45. int i, j;
  46. ALLOC_ARRAY(row0, len2 + 1);
  47. ALLOC_ARRAY(row1, len2 + 1);
  48. ALLOC_ARRAY(row2, len2 + 1);
  49. for (j = 0; j <= len2; j++)
  50. row1[j] = j * a;
  51. for (i = 0; i < len1; i++) {
  52. int *dummy;
  53. row2[0] = (i + 1) * d;
  54. for (j = 0; j < len2; j++) {
  55. /* substitution */
  56. row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
  57. /* swap */
  58. if (i > 0 && j > 0 && string1[i - 1] == string2[j] &&
  59. string1[i] == string2[j - 1] &&
  60. row2[j + 1] > row0[j - 1] + w)
  61. row2[j + 1] = row0[j - 1] + w;
  62. /* deletion */
  63. if (row2[j + 1] > row1[j + 1] + d)
  64. row2[j + 1] = row1[j + 1] + d;
  65. /* insertion */
  66. if (row2[j + 1] > row2[j] + a)
  67. row2[j + 1] = row2[j] + a;
  68. }
  69. dummy = row0;
  70. row0 = row1;
  71. row1 = row2;
  72. row2 = dummy;
  73. }
  74. i = row1[len2];
  75. free(row0);
  76. free(row1);
  77. free(row2);
  78. return i;
  79. }