Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (80 sloc) 1.765 kB
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
1 /*
2 * Copyright 2002 by Steven McDougall. This module is free
3 * software; you can redistribute it and/or modify it under the same
4 * terms as Perl itself.
5 *
6 */
7
603d94e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1282 5dc97367-9…
alobbs authored
8 #include "common-internal.h"
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
9 #include "levenshtein_distance.h"
10
11 #include <stdlib.h>
12 #include <string.h>
13
14
15 static int _prefix_distance(char *A, char *B, int lA, int lB);
16 static int _min(int a, int b, int c);
17 static void score(int *pD, char *A, char *B, int lA, int lB);
18
3e477c7 More cleanups.
ADF authored
19 #define D(r,c) pD[(r)*lB1+(c)]
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
20
21 int distance(char *A, char *B)
22 {
3e477c7 More cleanups.
ADF authored
23 int *pD, d;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
24
3e477c7 More cleanups.
ADF authored
25 int lA = strlen(A);
26 int lB = strlen(B);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
27
3e477c7 More cleanups.
ADF authored
28 int lA1 = lA+1;
29 int lB1 = lB+1;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
30
3e477c7 More cleanups.
ADF authored
31 pD = (int *) malloc(lA1 * lB1 * sizeof(int));
32 if (!pD)
33 return -1;
34
35 score(pD, A, B, lA, lB);
36
37 d = D(lA,lB);
38 free(pD);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
39
3e477c7 More cleanups.
ADF authored
40 return d;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
41 }
42
43
44 int prefix_distance(char *A, char *B)
45 {
3e477c7 More cleanups.
ADF authored
46 int lA = strlen(A);
47 int lB = strlen(B);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
48
3e477c7 More cleanups.
ADF authored
49 return lA < lB ?
50 _prefix_distance(A, B, lA, lB) :
51 _prefix_distance(B, A, lB, lA);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
52 }
53
3e477c7 More cleanups.
ADF authored
54
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
55 static int _prefix_distance(char *A, char *B, int lA, int lB)
56 {
3e477c7 More cleanups.
ADF authored
57 int *pD, c, d;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
58
3e477c7 More cleanups.
ADF authored
59 int lA1 = lA+1;
60 int lB1 = lB+1;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
61
3e477c7 More cleanups.
ADF authored
62 pD = (int *) malloc(lA1 * lB1 * sizeof(int));
63 if (!pD)
64 return -1;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
65
3e477c7 More cleanups.
ADF authored
66 score(pD, A, B, lA, lB);
67
68 d = D(lA, lA);
69 for (c = lA+1; c <= lB; c++)
70 if (d > D(lA, c))
71 d = D(lA, c);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
72
3e477c7 More cleanups.
ADF authored
73 free(pD);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
74
3e477c7 More cleanups.
ADF authored
75 return d;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
76 }
77
78
79 static void score(int *pD, char *A, char *B, int lA, int lB)
80 {
3e477c7 More cleanups.
ADF authored
81 int r, c;
82
83 int lB1 = lB+1;
84
85 for (r = 0; r <= lA; r++)
86 D(r,0) = r;
87
88 for (c = 0; c <= lB; c++)
89 D(0,c) = c;
90
91 for (r = 1; r <= lA; r++) {
92 for (c = 1; c <= lB; c++) {
93 int u = D(r-1,c );
94 int l = D(r ,c-1);
95 int ul = D(r-1,c-1);
96 int cost = (A[r-1] == B[c-1] ? 0 : 1);
97 D(r,c) = _min(u+1, l+1, ul+cost);
98 }
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
99 }
100 }
101
3e477c7 More cleanups.
ADF authored
102
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
103 static int _min (int a, int b, int c)
104 {
3e477c7 More cleanups.
ADF authored
105 int min = a;
106
107 if (min > b)
108 min = b;
109 if (min > c)
110 min = c;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
111
3e477c7 More cleanups.
ADF authored
112 return min;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97…
alobbs authored
113 }
3e477c7 More cleanups.
ADF authored
114
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-9…
alobbs authored
115
Something went wrong with that request. Please try again.