blob: 91fa057e019d428e10cbeb2293867846961af980 (
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
|
#include "CRSMUtil.hpp"
namespace Util {
int damerauLevenshteinDistance(const QString& s1, const QString& s2)
{
int length1 = s1.length();
int length2 = s2.length();
int d[length1+1][length2+1];
int i;
int j;
int cost;
for(i = 0; i <= length1; i++)
{
d[i][0] = i;
}
for(j = 0; j <= length2; j++)
{
d[0][j] = j;
}
for (i = 0; i < length1;i++)
{
for(j = 0; j < length2; j++)
{
if(s1[i] == s2[j])
{
cost = 0;
}
else
{
cost = 1;
}
d[i+1][j+1] = std::min({
d[i][j+1] + 1, // delete
d[i+1][j] + 1, // insert
d[i][j] + cost // substitution
});
if((i > 0) && (j > 0) && (s1[i] == s2[j-1]) && (s1[i-1] == s2[j]))
{
d[i+1][j+1] = std::min(
d[i+1][j+1],
d[i-1][j-1] + cost // transposition
);
}
}
}
return d[length1][length2];
}
}
|