aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dlm.c
blob: 19c14e3441e84cdd62f7290a4b4aaccd3c4a57cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* dlm.c - 2004/07/04 - Santtu Lakkala */

#include <glib.h>
#include <string.h>

int dlm_cost    (const gchar, const gchar);
int dlm_minimum (int a, int b, int c, int d);

/*
 * Compute the Damerau-Levenshtein Distance between utf-8 strings ds and dt.
 */
int dlm (const gchar *ds, const gchar *dt)
{
    int i, j, n, m, metric;
    int *d;

    /* Casefold for better matching of the strings. */
    gchar *s = g_utf8_casefold(ds, -1);
    gchar *t = g_utf8_casefold(dt, -1);

    n = strlen(s);
    m = strlen(t);

    if (n && m)
    {
        n++;
        m++;

        d = (int *)g_malloc0(sizeof(int) * m * n);

        for (i = 0; i < m; i++)
            d[i * n] = i;

        for (i = 1; i < n; i++)
        {
            d[i] = i;
            for (j = 1; j < m; j++)
            {
                d[j * n + i] = dlm_minimum(
                    d[(j - 1) * n + i] + 1,
                    d[j * n + i - 1] + 1,
                    d[(j - 1) * n + i - 1] + dlm_cost(s[i - 1], t[j - 1]),
                    (j>1 && i>1 ?
                         d[(j - 2) * n + i - 2] + dlm_cost(s[i - 1], t[j])
                        + dlm_cost(s[i], t[j - 1])
                    : 0x7fff)
                );
            }
        }
        metric = d[n * m - 1];
        g_free(d);

        /* Count a "similarity value" */
        metric = 1000 - (1000 * (metric * 2)) / (m + n);

        g_free(t);
        g_free(s);
        return metric;
    }
    g_free(t);
    g_free(s);

    /* Return value of -1 indicates an error */
    return -1;
}

/* "Cost" of changing from a to b. */
int dlm_cost (const char a, const char b)
{
    return a == b ? 0 : 1;
}

/* Return the smallest of four integers. */
int dlm_minimum (int a, int b, int c, int d)
{
    int min = a;
    if (b < min)
    {
        min = b;
    }
    if (c < min)
    {
        min = c;
    }
    if (d < min)
    {
        min = d;
    }
    return min;
}