| 1 |
|
| /* strcmp95.c Version 2.1 */ |
| 2 |
|
| |
| 3 |
|
| /* The strcmp95 function returns a double precision value from 0.0 (total |
| 4 |
|
| disagreement) to 1.0 (character-by-character agreement). The returned |
| 5 |
|
| value is a measure of the similarity of the two strings. */ |
| 6 |
|
| |
| 7 |
|
| /* Date of Release: Jan. 10, 2009 */ |
| 8 |
|
| /* Modified: eliminate limitation of MAX_VAR_SIZE |
| 9 |
|
| Authors: modification made by Shu-Chun Weng (scw@csie.org) */ |
| 10 |
|
| /* Date of Release (2.0): Jan. 26, 1994 */ |
| 11 |
|
| /* Modified: April 24, 1994 Corrected the processing of the single length |
| 12 |
|
| character strings. |
| 13 |
|
| Authors: This function was written using the logic from code written by |
| 14 |
|
| Bill Winkler, George McLaughlin and Matt Jaro with modifications |
| 15 |
|
| by Maureen Lynch. |
| 16 |
|
| Comment: This is the official string comparator to be used for matching |
| 17 |
|
| during the 1995 Test Census. */ |
| 18 |
|
| |
| 19 |
|
| #include <ctype.h> |
| 20 |
|
| #include <string.h> |
| 21 |
|
| #include <stdlib.h> |
| 22 |
|
| |
| 23 |
|
| #define NOTNUM(c) ((c>57) || (c<48)) |
| 24 |
|
| #define INRANGE(c) ((c>0) && (c<91)) |
| 25 |
|
| /* #define MAX_VAR_SIZE 250 */ |
| 26 |
|
| |
| 27 |
15 |
| double _strcmp95(char *ying, char *yang, long y_length, int *ind_c[]) |
| 28 |
|
| |
| 29 |
|
| { |
| 30 |
|
| /* Arguments: |
| 31 |
|
| |
| 32 |
|
| ying and yang are pointers to the 2 strings to be compared. The strings |
| 33 |
|
| need not be NUL-terminated strings because the length is passed. |
| 34 |
|
| |
| 35 |
|
| y_length is the length of the strings. |
| 36 |
|
| |
| 37 |
|
| ind_c is an array that is used to define whether certain options should be |
| 38 |
|
| activated. A nonzero value indicates the option is deactivated. |
| 39 |
|
| The options are: |
| 40 |
|
| ind_c[0] Increase the probability of a match when the number of matched |
| 41 |
|
| characters is large. This option allows for a little more |
| 42 |
|
| tolerance when the strings are large. It is not an appropriate |
| 43 |
|
| test when comparing fixed length fields such as phone and |
| 44 |
|
| social security numbers. |
| 45 |
|
| ind_c[1] All lower case characters are converted to upper case prior |
| 46 |
|
| to the comparison. Disabling this feature means that the lower |
| 47 |
|
| case string "code" will not be recognized as the same as the |
| 48 |
|
| upper case string "CODE". Also, the adjustment for similar |
| 49 |
|
| characters section only applies to uppercase characters. |
| 50 |
|
| |
| 51 |
|
| The suggested values are all zeros for character strings such as names. */ |
| 52 |
|
| |
| 53 |
|
| static int pass=0, adjwt[91][91]; |
| 54 |
|
| static char sp[39][2] = |
| 55 |
|
| {'A','E', 'A','I', 'A','O', 'A','U', 'B','V', 'E','I', 'E','O', 'E','U', |
| 56 |
|
| 'I','O', 'I','U', 'O','U', 'I','Y', 'E','Y', 'C','G', 'E','F', |
| 57 |
|
| 'W','U', 'W','V', 'X','K', 'S','Z', 'X','S', 'Q','C', 'U','V', |
| 58 |
|
| 'M','N', 'L','I', 'Q','O', 'P','R', 'I','J', '2','Z', '5','S', |
| 59 |
|
| '8','B', '1','I', '1','L', '0','O', '0','Q', 'C','K', 'G','J', |
| 60 |
|
| 'E',' ', 'Y',' ', 'S',' '}; |
| 61 |
|
| |
| 62 |
|
| char* ying_hold, |
| 63 |
|
| * yang_hold, |
| 64 |
|
| * ying_flag, |
| 65 |
|
| * yang_flag; |
| 66 |
|
| |
| 67 |
|
| double weight, Num_sim; |
| 68 |
|
| |
| 69 |
|
| long minv, search_range, lowlim, ying_length, |
| 70 |
|
| hilim, N_trans, Num_com, yang_length; |
| 71 |
|
| |
| 72 |
|
| int yl1, yi_st, N_simi; |
| 73 |
|
| |
| 74 |
|
| register int i, j, k; |
| 75 |
|
| |
| 76 |
|
| /* Initialize the adjwt array on the first call to the function only. |
| 77 |
|
| The adjwt array is used to give partial credit for characters that |
| 78 |
|
| may be errors due to known phonetic or character recognition errors. |
| 79 |
|
| A typical example is to match the letter "O" with the number "0" */ |
| 80 |
15 |
▶ | if (!pass) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pass) { |
1 |
14 |
15 |
|
| 81 |
1 |
| pass++; |
| 82 |
8373 |
▶ | for (i=0; i<91; i++) for (j=0; j<91; j++) adjwt[i][j] = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i=0; i<91; i++) for (j=0; j<91; j++) adjwt[i][j] = 0; |
8281 |
91 |
8372 |
| for (i=0; i<91; i++) for (j=0; j<91; j++) adjwt[i][j] = 0; |
91 |
1 |
92 |
|
| 83 |
37 |
▶ | for (i=0; i<36; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<36; i++) { |
36 |
1 |
37 |
|
| 84 |
36 |
| adjwt[sp[i][0]][sp[i][1]] = 3; |
| 85 |
36 |
| adjwt[sp[i][1]][sp[i][0]] = 3; |
| 86 |
|
| } } |
| 87 |
|
| |
| 88 |
|
| /* Identify the strings to be compared by stripping off all leading and |
| 89 |
|
| trailing spaces. */ |
| 90 |
15 |
| k = y_length - 1; |
| 91 |
16 |
▶ | for(j = 0;((ying[j]==' ') && (j < k));j++); |
Branch: 2 branches
| Branch | True | False | Total |
| for(j = 0;((ying[j]==' ') && (j < k));j++); |
1 |
15 |
16 |
| for(j = 0;((ying[j]==' ') && (j < k));j++); |
1 |
0 |
1 |
|
| 92 |
222 |
▶ | for(i = k;((ying[i]==' ') && (i > 0));i--); |
Branch: 2 branches
| Branch | True | False | Total |
| for(i = k;((ying[i]==' ') && (i > 0));i--); |
207 |
15 |
222 |
| for(i = k;((ying[i]==' ') && (i > 0));i--); |
207 |
0 |
207 |
|
| 93 |
15 |
| ying_length = i + 1 - j; |
| 94 |
15 |
| yi_st = j; |
| 95 |
|
| |
| 96 |
16 |
▶ | for(j = 0;((yang[j]==' ') && (j < k));j++); |
Branch: 2 branches
| Branch | True | False | Total |
| for(j = 0;((yang[j]==' ') && (j < k));j++); |
1 |
15 |
16 |
| for(j = 0;((yang[j]==' ') && (j < k));j++); |
1 |
0 |
1 |
|
| 97 |
70 |
▶ | for(i = k;((yang[i]==' ') && (i > 0));i--); |
Branch: 2 branches
| Branch | True | False | Total |
| for(i = k;((yang[i]==' ') && (i > 0));i--); |
55 |
15 |
70 |
| for(i = k;((yang[i]==' ') && (i > 0));i--); |
55 |
0 |
55 |
|
| 98 |
15 |
| yang_length = i + 1 - j; |
| 99 |
|
| |
| 100 |
|
| /* If either string is blank - return - added in Version 2 */ |
| 101 |
15 |
▶ | if(ying_length <= 0 || yang_length <= 0) return(0.0); |
Branch: 2 branches
| Branch | True | False | Total |
| if(ying_length <= 0 || yang_length <= 0) return(0.0); |
15 |
0 |
15 |
| if(ying_length <= 0 || yang_length <= 0) return(0.0); |
0 |
15 |
15 |
|
| 102 |
|
| |
| 103 |
15 |
| ying_hold = (char*) malloc(sizeof(char) * (ying_length + 1)); |
| 104 |
15 |
| yang_hold = (char*) malloc(sizeof(char) * (yang_length + 1)); |
| 105 |
|
| |
| 106 |
15 |
| strncpy(ying_hold,&ying[yi_st],ying_length); |
| 107 |
15 |
| strncpy(yang_hold,&yang[j],yang_length); |
| 108 |
|
| |
| 109 |
15 |
▶ | if (ying_length > yang_length) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ying_length > yang_length) { |
6 |
9 |
15 |
|
| 110 |
6 |
| search_range = ying_length; |
| 111 |
6 |
| minv = yang_length; |
| 112 |
|
| } |
| 113 |
|
| else { |
| 114 |
9 |
| search_range = yang_length; |
| 115 |
9 |
| minv = ying_length; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* If either string is blank - return */ |
| 119 |
|
| /* if (!minv) return(0.0); removed in version 2 */ |
| 120 |
|
| |
| 121 |
|
| /* Blank out the flags */ |
| 122 |
15 |
| ying_flag = (char*) malloc(sizeof(char) * search_range); |
| 123 |
15 |
| yang_flag = (char*) malloc(sizeof(char) * search_range); |
| 124 |
|
| |
| 125 |
15 |
| memset(ying_flag, ' ', search_range); |
| 126 |
15 |
| memset(yang_flag, ' ', search_range); |
| 127 |
15 |
| search_range = (search_range/2) - 1; |
| 128 |
15 |
▶ | if (search_range < 0) search_range = 0; /* added in version 2 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (search_range < 0) search_range = 0; /* added in version 2 */ |
0 |
15 |
15 |
|
| 129 |
|
| |
| 130 |
|
| /* Convert all lower case characters to upper case. */ |
| 131 |
15 |
▶ | if (!ind_c[1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ind_c[1]) { |
11 |
4 |
15 |
|
| 132 |
1312 |
▶ | for (i = 0;i < ying_length;i++) if (islower(ying_hold[i])) ying_hold[i] -= 32; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0;i < ying_length;i++) if (islower(ying_hold[i])) ying_hold[i] -= 32; |
1054 |
247 |
1301 |
| for (i = 0;i < ying_length;i++) if (islower(ying_hold[i])) ying_hold[i] -= 32; |
1301 |
11 |
1312 |
|
| 133 |
1414 |
▶ | for (j = 0;j < yang_length;j++) if (islower(yang_hold[j])) yang_hold[j] -= 32; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0;j < yang_length;j++) if (islower(yang_hold[j])) yang_hold[j] -= 32; |
1143 |
260 |
1403 |
| for (j = 0;j < yang_length;j++) if (islower(yang_hold[j])) yang_hold[j] -= 32; |
1403 |
11 |
1414 |
|
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| /* Looking only within the search range, count and flag the matched pairs. */ |
| 137 |
15 |
| Num_com = 0; |
| 138 |
15 |
| yl1 = yang_length - 1; |
| 139 |
1798 |
▶ | for (i = 0;i < ying_length;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < ying_length;i++) { |
1783 |
15 |
1798 |
|
| 140 |
1783 |
▶ | lowlim = (i >= search_range) ? i - search_range : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| lowlim = (i >= search_range) ? i - search_range : 0; |
805 |
978 |
1783 |
|
| 141 |
1783 |
| hilim = ((i + search_range) <= yl1) ? (i + search_range) : yl1; |
| 142 |
103376 |
▶ | for (j = lowlim;j <= hilim;j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = lowlim;j <= hilim;j++) { |
103091 |
285 |
103376 |
|
| 143 |
103091 |
▶ | if ((yang_flag[j] != '1') && (yang_hold[j] == ying_hold[i])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((yang_flag[j] != '1') && (yang_hold[j] == ying_hold[i])) { |
39986 |
63105 |
103091 |
| if ((yang_flag[j] != '1') && (yang_hold[j] == ying_hold[i])) { |
1498 |
38488 |
39986 |
|
| 144 |
1498 |
| yang_flag[j] = '1'; |
| 145 |
1498 |
| ying_flag[i] = '1'; |
| 146 |
1498 |
| Num_com++; |
| 147 |
1498 |
| break; |
| 148 |
|
| } } } |
| 149 |
|
| |
| 150 |
|
| /* If no characters in common - return */ |
| 151 |
15 |
▶ | if (!Num_com){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!Num_com){ |
0 |
15 |
15 |
|
| 152 |
0 |
| free(ying_hold); |
| 153 |
0 |
| free(yang_hold); |
| 154 |
0 |
| free(ying_flag); |
| 155 |
0 |
| free(yang_flag); |
| 156 |
0 |
| return(0.0); |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| /* Count the number of transpositions */ |
| 160 |
15 |
| k = N_trans = 0; |
| 161 |
1798 |
▶ | for (i = 0;i < ying_length;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < ying_length;i++) { |
1783 |
15 |
1798 |
|
| 162 |
1783 |
▶ | if (ying_flag[i] == '1') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ying_flag[i] == '1') { |
1498 |
285 |
1783 |
|
| 163 |
1934 |
▶ | for (j = k;j < yang_length;j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = k;j < yang_length;j++) { |
1934 |
0 |
1934 |
|
| 164 |
1934 |
▶ | if (yang_flag[j] == '1') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (yang_flag[j] == '1') { |
1498 |
436 |
1934 |
|
| 165 |
1498 |
| k = j + 1; |
| 166 |
1498 |
| break; |
| 167 |
|
| } } |
| 168 |
1498 |
▶ | if (ying_hold[i] != yang_hold[j]) N_trans++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ying_hold[i] != yang_hold[j]) N_trans++; |
1332 |
166 |
1498 |
|
| 169 |
|
| } } |
| 170 |
15 |
| N_trans = N_trans / 2; |
| 171 |
|
| |
| 172 |
|
| /* adjust for similarities in nonmatched characters */ |
| 173 |
15 |
| N_simi = 0; |
| 174 |
15 |
▶ | if (minv > Num_com) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (minv > Num_com) { |
15 |
0 |
15 |
|
| 175 |
1798 |
▶ | for (i = 0;i < ying_length;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < ying_length;i++) { |
1783 |
15 |
1798 |
|
| 176 |
1783 |
▶ | if (ying_flag[i] == ' ' && INRANGE(ying_hold[i])) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (ying_flag[i] == ' ' && INRANGE(ying_hold[i])) { |
285 |
1498 |
1783 |
| if (ying_flag[i] == ' ' && INRANGE(ying_hold[i])) { |
285 |
0 |
285 |
| if (ying_flag[i] == ' ' && INRANGE(ying_hold[i])) { |
219 |
66 |
285 |
|
| 177 |
26422 |
▶ | for (j = 0;j < yang_length;j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0;j < yang_length;j++) { |
26262 |
160 |
26422 |
|
| 178 |
26262 |
▶ | if (yang_flag[j] == ' ' && INRANGE(yang_hold[j])) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (yang_flag[j] == ' ' && INRANGE(yang_hold[j])) { |
4115 |
22147 |
26262 |
| if (yang_flag[j] == ' ' && INRANGE(yang_hold[j])) { |
4115 |
0 |
4115 |
| if (yang_flag[j] == ' ' && INRANGE(yang_hold[j])) { |
3797 |
318 |
4115 |
|
| 179 |
3797 |
▶ | if (adjwt[ying_hold[i]][yang_hold[j]] > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (adjwt[ying_hold[i]][yang_hold[j]] > 0) { |
59 |
3738 |
3797 |
|
| 180 |
59 |
| N_simi += adjwt[ying_hold[i]][yang_hold[j]]; |
| 181 |
59 |
| yang_flag[j] = '2'; |
| 182 |
59 |
| break; |
| 183 |
|
| } } } } } } |
| 184 |
15 |
| Num_sim = ((double) N_simi)/10.0 + Num_com; |
| 185 |
|
| |
| 186 |
|
| /* Main weight computation. */ |
| 187 |
15 |
| weight= Num_sim / ((double) ying_length) + Num_sim / ((double) yang_length) |
| 188 |
15 |
| + ((double) (Num_com - N_trans)) / ((double) Num_com); |
| 189 |
15 |
| weight = weight / 3.0; |
| 190 |
|
| |
| 191 |
|
| /* Continue to boost the weight if the strings are similar */ |
| 192 |
15 |
▶ | if (weight > 0.7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (weight > 0.7) { |
15 |
0 |
15 |
|
| 193 |
|
| |
| 194 |
|
| /* Adjust for having up to the first 4 characters in common */ |
| 195 |
15 |
| j = (minv >= 4) ? 4 : minv; |
| 196 |
16 |
▶ | for (i=0;((i<j)&&(ying_hold[i]==yang_hold[i])&&(NOTNUM(ying_hold[i])));i++); |
Branch: 4 branches
| Branch | True | False | Total |
| for (i=0;((i<j)&&(ying_hold[i]==yang_hold[i])&&(NOTNUM(ying_hold[i])));i++); |
16 |
0 |
16 |
| for (i=0;((i<j)&&(ying_hold[i]==yang_hold[i])&&(NOTNUM(ying_hold[i])));i++); |
1 |
15 |
16 |
| for (i=0;((i<j)&&(ying_hold[i]==yang_hold[i])&&(NOTNUM(ying_hold[i])));i++); |
1 |
0 |
1 |
| for (i=0;((i<j)&&(ying_hold[i]==yang_hold[i])&&(NOTNUM(ying_hold[i])));i++); |
0 |
0 |
0 |
|
| 197 |
15 |
▶ | if (i) weight += i * 0.1 * (1.0 - weight); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) weight += i * 0.1 * (1.0 - weight); |
1 |
14 |
15 |
|
| 198 |
|
| |
| 199 |
|
| /* Optionally adjust for long strings. */ |
| 200 |
|
| /* After agreeing beginning chars, at least two more must agree and |
| 201 |
|
| the agreeing characters must be > .5 of remaining characters. */ |
| 202 |
15 |
▶ | if ((!ind_c[0]) && (minv>4) && (Num_com>i+1) && (2*Num_com>=minv+i)) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((!ind_c[0]) && (minv>4) && (Num_com>i+1) && (2*Num_com>=minv+i)) |
11 |
4 |
15 |
| if ((!ind_c[0]) && (minv>4) && (Num_com>i+1) && (2*Num_com>=minv+i)) |
11 |
0 |
11 |
| if ((!ind_c[0]) && (minv>4) && (Num_com>i+1) && (2*Num_com>=minv+i)) |
11 |
0 |
11 |
| if ((!ind_c[0]) && (minv>4) && (Num_com>i+1) && (2*Num_com>=minv+i)) |
11 |
0 |
11 |
|
| 203 |
11 |
▶ | if (NOTNUM(ying_hold[0])) |
Branch: 2 branches
| Branch | True | False | Total |
| if (NOTNUM(ying_hold[0])) |
0 |
11 |
11 |
| if (NOTNUM(ying_hold[0])) |
0 |
0 |
0 |
|
| 204 |
11 |
| weight += (double) (1.0-weight) * |
| 205 |
11 |
| ((double) (Num_com-i-1) / ((double) (ying_length+yang_length-i*2+2))); |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
15 |
| free(ying_hold); |
| 209 |
15 |
| free(yang_hold); |
| 210 |
15 |
| free(ying_flag); |
| 211 |
15 |
| free(yang_flag); |
| 212 |
15 |
| return(weight); |
| 213 |
|
| |
| 214 |
|
| } /* strcmp95 */ |
| 215 |
|
| |