18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include "levenshtein.h"
38c2ecf20Sopenharmony_ci#include <errno.h>
48c2ecf20Sopenharmony_ci#include <stdlib.h>
58c2ecf20Sopenharmony_ci#include <string.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * This function implements the Damerau-Levenshtein algorithm to
98c2ecf20Sopenharmony_ci * calculate a distance between strings.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Basically, it says how many letters need to be swapped, substituted,
128c2ecf20Sopenharmony_ci * deleted from, or added to string1, at least, to get string2.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The idea is to build a distance matrix for the substrings of both
158c2ecf20Sopenharmony_ci * strings.  To avoid a large space complexity, only the last three rows
168c2ecf20Sopenharmony_ci * are kept in memory (if swaps had the same or higher cost as one deletion
178c2ecf20Sopenharmony_ci * plus one insertion, only two rows would be needed).
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * At any stage, "i + 1" denotes the length of the current substring of
208c2ecf20Sopenharmony_ci * string1 that the distance is calculated for.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * row2 holds the current row, row1 the previous row (i.e. for the substring
238c2ecf20Sopenharmony_ci * of string1 of length "i"), and row0 the row before that.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * In other words, at the start of the big loop, row2[j + 1] contains the
268c2ecf20Sopenharmony_ci * Damerau-Levenshtein distance between the substring of string1 of length
278c2ecf20Sopenharmony_ci * "i" and the substring of string2 of length "j + 1".
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * All the big loop does is determine the partial minimum-cost paths.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * It does so by calculating the costs of the path ending in characters
328c2ecf20Sopenharmony_ci * i (in string1) and j (in string2), respectively, given that the last
338c2ecf20Sopenharmony_ci * operation is a substition, a swap, a deletion, or an insertion.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * This implementation allows the costs to be weighted:
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * - w (as in "sWap")
388c2ecf20Sopenharmony_ci * - s (as in "Substitution")
398c2ecf20Sopenharmony_ci * - a (for insertion, AKA "Add")
408c2ecf20Sopenharmony_ci * - d (as in "Deletion")
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Note that this algorithm calculates a distance _iff_ d == a.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_ciint levenshtein(const char *string1, const char *string2,
458c2ecf20Sopenharmony_ci		int w, int s, int a, int d)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	int len1 = strlen(string1), len2 = strlen(string2);
488c2ecf20Sopenharmony_ci	int *row0 = malloc(sizeof(int) * (len2 + 1));
498c2ecf20Sopenharmony_ci	int *row1 = malloc(sizeof(int) * (len2 + 1));
508c2ecf20Sopenharmony_ci	int *row2 = malloc(sizeof(int) * (len2 + 1));
518c2ecf20Sopenharmony_ci	int i, j;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	for (j = 0; j <= len2; j++)
548c2ecf20Sopenharmony_ci		row1[j] = j * a;
558c2ecf20Sopenharmony_ci	for (i = 0; i < len1; i++) {
568c2ecf20Sopenharmony_ci		int *dummy;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		row2[0] = (i + 1) * d;
598c2ecf20Sopenharmony_ci		for (j = 0; j < len2; j++) {
608c2ecf20Sopenharmony_ci			/* substitution */
618c2ecf20Sopenharmony_ci			row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
628c2ecf20Sopenharmony_ci			/* swap */
638c2ecf20Sopenharmony_ci			if (i > 0 && j > 0 && string1[i - 1] == string2[j] &&
648c2ecf20Sopenharmony_ci					string1[i] == string2[j - 1] &&
658c2ecf20Sopenharmony_ci					row2[j + 1] > row0[j - 1] + w)
668c2ecf20Sopenharmony_ci				row2[j + 1] = row0[j - 1] + w;
678c2ecf20Sopenharmony_ci			/* deletion */
688c2ecf20Sopenharmony_ci			if (row2[j + 1] > row1[j + 1] + d)
698c2ecf20Sopenharmony_ci				row2[j + 1] = row1[j + 1] + d;
708c2ecf20Sopenharmony_ci			/* insertion */
718c2ecf20Sopenharmony_ci			if (row2[j + 1] > row2[j] + a)
728c2ecf20Sopenharmony_ci				row2[j + 1] = row2[j] + a;
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		dummy = row0;
768c2ecf20Sopenharmony_ci		row0 = row1;
778c2ecf20Sopenharmony_ci		row1 = row2;
788c2ecf20Sopenharmony_ci		row2 = dummy;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	i = row1[len2];
828c2ecf20Sopenharmony_ci	free(row0);
838c2ecf20Sopenharmony_ci	free(row1);
848c2ecf20Sopenharmony_ci	free(row2);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return i;
878c2ecf20Sopenharmony_ci}
88