levenshtein.c 2.6 KB

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