File: distance.cpp

package info (click to toggle)
sdcv 0.3.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,644 kB
  • ctags: 1,207
  • sloc: sh: 9,235; ansic: 7,803; cpp: 2,613; makefile: 485; yacc: 316; sed: 16
file content (202 lines) | stat: -rw-r--r-- 5,389 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
    writer : Opera Wang
    E-Mail : wangvisual AT sohu DOT com
    License: GPL
*/

/* filename: distance.cc */
/*
http://www.merriampark.com/ld.htm
What is Levenshtein Distance?

Levenshtein distance (LD) is a measure of the similarity between two strings, 
which we will refer to as the source string (s) and the target string (t). 
The distance is the number of deletions, insertions, or substitutions required
 to transform s into t. For example,

    * If s is "test" and t is "test", then LD(s,t) = 0, because no transformations are needed. 
    The strings are already identical.
    * If s is "test" and t is "tent", then LD(s,t) = 1, because one substitution
     (change "s" to "n") is sufficient to transform s into t.

The greater the Levenshtein distance, the more different the strings are.

Levenshtein distance is named after the Russian scientist Vladimir Levenshtein,
 who devised the algorithm in 1965. If you can't spell or pronounce Levenshtein,
 the metric is also sometimes called edit distance.

The Levenshtein distance algorithm has been used in:

    * Spell checking
    * Speech recognition
    * DNA analysis
    * Plagiarism detection 
*/

#include <stdlib.h>
#include <string.h>
//#include <stdio.h>

#include "distance.h"

#define OPTIMIZE_ED
/*
Cover transposition, in addition to deletion,
insertion and substitution. This step is taken from:
Berghel, Hal ; Roach, David : "An Extension of Ukkonen's 
Enhanced Dynamic Programming ASM Algorithm"
(http://www.acm.org/~hlb/publications/asm/asm.html)
*/
#define COVER_TRANSPOSITION

/****************************************/
/*Implementation of Levenshtein distance*/
/****************************************/

EditDistance::EditDistance()
{
    currentelements = 2500; // It's enough for most conditions :-)
    d = (int*)malloc(sizeof(int)*currentelements);
}

EditDistance::~EditDistance()
{
//    printf("size:%d\n",currentelements);
    if (d) free(d);
}

#ifdef OPTIMIZE_ED
int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int limit)
/*Compute levenshtein distance between s and t, this is using QUICK algorithm*/
{
    int n=0,m=0,iLenDif,k,i,j,cost;
    // Remove leftmost matching portion of strings
    while ( *s && (*s==*t) )
    {
        s++;
		t++;
    }

	while (s[n])
	{
		n++;
	}
	while (t[m])
	{
		m++;
	}
	
    // Remove rightmost matching portion of strings by decrement n and m.
    while ( n && m && (*(s+n-1)==*(t+m-1)) )
    {
        n--;m--;
    }
    if ( m==0 || n==0 || d==(int*)0 )
        return (m+n);
    if ( m < n )
    {
        const gunichar * temp = s;
        int itemp = n;
        s = t;
        t = temp;
        n = m;
        m = itemp;
    }
    iLenDif = m - n;
    if ( iLenDif >= limit )
        return iLenDif;
    // step 1
    n++;m++;
//    d=(int*)malloc(sizeof(int)*m*n);
    if ( m*n > currentelements )
    {
        currentelements = m*n*2;    // double the request
        d = (int*)realloc(d,sizeof(int)*currentelements);
        if ( (int*)0 == d )
            return (m+n);
    }
    // step 2, init matrix
    for (k=0;k<n;k++)
        d[k] = k;
    for (k=1;k<m;k++)
        d[k*n] = k;
    // step 3
    for (i=1;i<n;i++)
    {
        // first calculate column, d(i,j)
        for ( j=1;j<iLenDif+i;j++ )
        {
            cost = s[i-1]==t[j-1]?0:1;
            d[j*n+i] = minimum(d[(j-1)*n+i]+1,d[j*n+i-1]+1,d[(j-1)*n+i-1]+cost);
#ifdef COVER_TRANSPOSITION
            if ( i>=2 && j>=2 && (d[j*n+i]-d[(j-2)*n+i-2]==2)
                 && (s[i-2]==t[j-1]) && (s[i-1]==t[j-2]) )
                d[j*n+i]--;
#endif
        }
        // second calculate row, d(k,j)
        // now j==iLenDif+i;
        for ( k=1;k<=i;k++ )
        {
            cost = s[k-1]==t[j-1]?0:1;
            d[j*n+k] = minimum(d[(j-1)*n+k]+1,d[j*n+k-1]+1,d[(j-1)*n+k-1]+cost);
#ifdef COVER_TRANSPOSITION
            if ( k>=2 && j>=2 && (d[j*n+k]-d[(j-2)*n+k-2]==2)
                 && (s[k-2]==t[j-1]) && (s[k-1]==t[j-2]) )
                d[j*n+k]--;
#endif
        }
        // test if d(i,j) limit gets equal or exceed
        if ( d[j*n+i] >= limit )
        {
            return d[j*n+i];
        }
    }
    // d(n-1,m-1)
    return d[n*m-1];
}
#else
int EditDistance::CalEditDistance(const char *s,const char *t,const int limit)
{
    //Step 1
    int k,i,j,n,m,cost;
    n=strlen(s); 
    m=strlen(t);
    if( n!=0 && m!=0 && d!=(int*)0 )
    {
        m++;n++;
        if ( m*n > currentelements )
        {
            currentelements = m*n*2;
            d = (int*)realloc(d,sizeof(int)*currentelements);
            if ( (int*)0 == d )
                return (m+n);
        }
        //Step 2	
        for(k=0;k<n;k++)
            d[k]=k;
        for(k=0;k<m;k++)
            d[k*n]=k;
        //Step 3 and 4	
        for(i=1;i<n;i++)
            for(j=1;j<m;j++)
            {
                //Step 5
                if(s[i-1]==t[j-1])
                    cost=0;
                else
                    cost=1;
                //Step 6			 
                d[j*n+i]=minimum(d[(j-1)*n+i]+1,d[j*n+i-1]+1,d[(j-1)*n+i-1]+cost);
#ifdef COVER_TRANSPOSITION
                if ( i>=2 && j>=2 && (d[j*n+i]-d[(j-2)*n+i-2]==2)
                     && (s[i-2]==t[j-1]) && (s[i-1]==t[j-2]) )
                    d[j*n+i]--;
#endif        
            }
        return d[n*m-1];
    }
    else 
        return (n+m);
}
#endif