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