17 const float ci,
const float cd,
const float cs,
const float ct)
21 auto min3 = [] (
float a,
float b,
float c) {
return std::min (a,
std::min (b, c)); };
26 const char *a = source.
data(), *b = target.
data();
28 while (n && m && *a == *b)
31 while (n && m && a[n-1] == b[m-1])
40 float *row2 = rowmem.data(), *row1 = row2 + m+1, *row0 = row1 + m+1;
42 for (
ssize_t j = 0; j <= m; j++)
45 for (
ssize_t i = 1; i <= n; i++) {
47 for (
ssize_t j = 1; j <= m; j++) {
48 const bool ue = a[i-1] != b[j-1];
49 row0[j] = min3 (row1[j] + cd,
52 if (ue && i > 1 && j > 1 && a[i-1] == b[j-2] && a[i-2] == b[j-1])
53 row0[j] =
std::min (row0[j], row2[j-2] + ct);
56 float *
const next = row2;
68 L2DMatrix (
size_t a,
size_t b, T init = {}) :
71 v.resize (sa * sb, init);
74 operator() (
size_t a,
size_t b)
86 const float ci,
const float cd,
const float cs,
const float ct)
88 auto u = [] (
char c) {
return uint8_t (c); };
89 const size_t lp = source.
size(), ls = target.
size();
90 const char *p = source.
data()-1, *s = target.
data()-1;
95 for (
int i = 0; i <= lp; i++)
97 for (
int j = 0; j <= ls; j++)
100 for (
int i = 1; i <= lp; i++) {
102 for (
int j = 1; j <= ls; j++) {
103 const float d = p[i] == s[j] ? 0 : cs;
108 const int i_ = CP[u(s[j])];
110 if (i_ > 0 and j_ > 0)
111 C(i,j) =
std::min (C(i,j), C(i_-1,j_-1) + (i-i_)-1 + (j-j_)-1 + ct);
126 const float ci,
const float cd,
const float cs,
const float ct)
128 size_t n = source.
size(), m = target.
size();
129 const char *a = source.
data(), *b = target.
data();
131 while (n && m && *a == *b)
134 while (n && m && a[n-1] == b[m-1])
143 return damerau_levenshtein_unrestricted ({a, n}, {b, m}, ci, cd, cs, ct);
145 return damerau_levenshtein_unrestricted ({b, m}, {a, n}, cd, ci, cs, ct);
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
The Anklang C++ API namespace.
float damerau_levenshtein_distance(const std::string &source, const std::string &target, const float ci, const float cd, const float cs, const float ct)
float damerau_levenshtein_restricted(const std::string &source, const std::string &target, const float ci, const float cd, const float cs, const float ct)
#define TCMP(a, cmp, b)
Compare a and b according to operator cmp, verbose on failiure.