2 #include "levenshtein.h"
4 int levenshtein(const char *string1
, const char *string2
,
5 int w
, int s
, int a
, int d
)
7 int len1
= strlen(string1
), len2
= strlen(string2
);
8 int *row0
= xmalloc(sizeof(int) * (len2
+ 1));
9 int *row1
= xmalloc(sizeof(int) * (len2
+ 1));
10 int *row2
= xmalloc(sizeof(int) * (len2
+ 1));
13 for (j
= 0; j
<= len2
; j
++)
15 for (i
= 0; i
< len1
; i
++) {
18 row2
[0] = (i
+ 1) * d
;
19 for (j
= 0; j
< len2
; j
++) {
21 row2
[j
+ 1] = row1
[j
] + s
* (string1
[i
] != string2
[j
]);
23 if (i
> 0 && j
> 0 && string1
[i
- 1] == string2
[j
] &&
24 string1
[i
] == string2
[j
- 1] &&
25 row2
[j
+ 1] > row0
[j
- 1] + w
)
26 row2
[j
+ 1] = row0
[j
- 1] + w
;
28 if (j
+ 1 < len2
&& row2
[j
+ 1] > row1
[j
+ 1] + d
)
29 row2
[j
+ 1] = row1
[j
+ 1] + d
;
31 if (row2
[j
+ 1] > row2
[j
] + a
)
32 row2
[j
+ 1] = row2
[j
] + a
;