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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
Datum text_metaphone(PG_FUNCTION_ARGS);
Datum text_metaphone_length(PG_FUNCTION_ARGS);
void phonetic(char *name, char *metaph, int metalen);
#define METAPHONE_LEN 50
#undef METAPHONE_TEST
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
#define NULLCHAR (char *) 0
char *VOWELS="AEIOU",
*FRONTV="EIY", /* special cases for letters in FRONT of these */
*VARSON="CSPTG", /* variable sound--those modified by adding an "h" */
*DOUBLE="."; /* let these double letters through */
char *excpPAIR="AGKPW", /* exceptions "ae-", "gn-", "kn-", "pn-", "wr-" */
*nextLTR ="ENNNR";
char *chrptr, *chrptr1;
void phonetic(name,metaph,metalen)
char *name, *metaph;
int metalen;
{
int ii, jj, silent, hard, Lng, lastChr;
char curLtr, prevLtr, nextLtr, nextLtr2, nextLtr3;
int vowelAfter, vowelBefore, frontvAfter;
char wname[60];
char *ename=wname;
jj = 0;
for (ii=0; name[ii] != '\0'; ii++) {
if ( isalpha(name[ii]) ) {
ename[jj] = toupper(name[ii]);
jj++;
}
}
ename[jj] = '\0';
if (strlen(ename) == 0) return;
/* if ae, gn, kn, pn, wr then drop the first letter */
if ( (chrptr=strchr(excpPAIR,ename[0]) ) != NULLCHAR ) {
chrptr1 = nextLTR + (chrptr-excpPAIR);
if ( *chrptr1 == ename[1] ) strcpy(ename,&ename[1]);
}
/* change x to s */
if (ename[0] == 'X') ename[0] = 'S';
/* get rid of the "h" in "wh" */
if ( strncmp(ename,"WH",2) == 0 ) strcpy(&ename[1], &ename[2]);
Lng = strlen(ename);
lastChr = Lng -1; /* index to last character in string makes code easier*/
/* Remove an S from the end of the string */
if ( ename[lastChr] == 'S' ) {
ename[lastChr] = '\0';
Lng = strlen(ename);
lastChr = Lng -1;
}
for (ii=0; ( (strlen(metaph) < metalen) && (ii < Lng) ); ii++) {
curLtr = ename[ii];
vowelBefore = FALSE; prevLtr = ' ';
if (ii > 0) {
prevLtr = ename[ii-1];
if ( strchr(VOWELS,prevLtr) != NULLCHAR ) vowelBefore = TRUE;
}
/* if first letter is a vowel KEEP it */
if (ii == 0 && (strchr(VOWELS,curLtr) != NULLCHAR) ) {
strncat(metaph,&curLtr,1);
continue;
}
vowelAfter = FALSE; frontvAfter = FALSE; nextLtr = ' ';
if ( ii < lastChr ) {
nextLtr = ename[ii+1];
if ( strchr(VOWELS,nextLtr) != NULLCHAR ) vowelAfter = TRUE;
if ( strchr(FRONTV,nextLtr) != NULLCHAR ) frontvAfter = TRUE;
}
/* skip double letters except ones in list */
if (curLtr == nextLtr && (strchr(DOUBLE,nextLtr) == NULLCHAR) ) continue;
nextLtr2 = ' ';
if (ii < (lastChr-1) ) nextLtr2 = ename[ii+2];
nextLtr3 = ' ';
if (ii < (lastChr-2) ) nextLtr3 = ename[ii+3];
switch (curLtr) {
case 'B': silent = FALSE;
if (ii == lastChr && prevLtr == 'M') silent = TRUE;
if (! silent) strncat(metaph,&curLtr,1);
break;
/*silent -sci-,-sce-,-scy-; sci-, etc OK*/
case 'C': if (! (ii > 1 && prevLtr == 'S' && frontvAfter) ) {
if ( ii > 0 && nextLtr == 'I' && nextLtr2 == 'A' )
strncat(metaph,"X",1);
else
if (frontvAfter)
strncat(metaph,"S",1);
else
if (ii > 1 && prevLtr == 'S' && nextLtr == 'H')
strncat(metaph,"K",1);
else
if (nextLtr == 'H')
if (ii == 0 && (strchr(VOWELS,nextLtr2) == NULLCHAR) )
strncat(metaph,"K",1);
else
strncat(metaph,"X",1);
else
if (prevLtr == 'C')
strncat(metaph,"C",1);
else
strncat(metaph,"K",1);
}
break;
case 'D': if (nextLtr == 'G' && (strchr(FRONTV,nextLtr2) != NULLCHAR))
strncat(metaph,"J",1);
else
strncat(metaph,"T",1);
break;
case 'G': silent=FALSE;
/* SILENT -gh- except for -gh and no vowel after h */
if ( (ii < (lastChr-1) && nextLtr == 'H')
&& (strchr(VOWELS,nextLtr2) == NULLCHAR) )
silent=TRUE;
if ( (ii == (lastChr-3) )
&& nextLtr == 'N' && nextLtr2 == 'E' && nextLtr3 == 'D')
silent=TRUE;
else
if ( (ii == (lastChr-1)) && nextLtr == 'N') silent=TRUE;
if (prevLtr == 'D' && frontvAfter) silent=TRUE;
if (prevLtr == 'G')
hard=TRUE;
else
hard=FALSE;
if (!silent) {
if (frontvAfter && (! hard) )
strncat(metaph,"J",1);
else
strncat(metaph,"K",1);
}
break;
case 'H': silent = FALSE;
if ( strchr(VARSON,prevLtr) != NULLCHAR ) silent = TRUE;
if ( vowelBefore && !vowelAfter) silent = TRUE;
if (!silent) strncat(metaph,&curLtr,1);
break;
case 'F':
case 'J':
case 'L':
case 'M':
case 'N':
case 'R': strncat(metaph,&curLtr,1);
break;
case 'K': if (prevLtr != 'C') strncat(metaph,&curLtr,1);
break;
case 'P': if (nextLtr == 'H')
strncat(metaph,"F",1);
else
strncat(metaph,"P",1);
break;
case 'Q': strncat(metaph,"K",1);
break;
case 'S': if (ii > 1 && nextLtr == 'I'
&& ( nextLtr2 == 'O' || nextLtr2 == 'A') )
strncat(metaph,"X",1);
else
if (nextLtr == 'H')
strncat(metaph,"X",1);
else
strncat(metaph,"S",1);
break;
case 'T': if (ii > 1 && nextLtr == 'I'
&& ( nextLtr2 == 'O' || nextLtr2 == 'A') )
strncat(metaph,"X",1);
else
if (nextLtr == 'H') /* The=0, Tho=T, Withrow=0 */
if (ii > 0 || (strchr(VOWELS,nextLtr2) != NULLCHAR) )
strncat(metaph,"0",1);
else
strncat(metaph,"T",1);
else
if (! (ii < (lastChr-2) && nextLtr == 'C' && nextLtr2 == 'H'))
strncat(metaph,"T",1);
break;
case 'V': strncat(metaph,"F",1);
break;
case 'W':
case 'Y': if (ii < lastChr && vowelAfter) strncat(metaph,&curLtr,1);
break;
case 'X': strncat(metaph,"KS",2);
break;
case 'Z': strncat(metaph,"S",1);
break;
}
}
/* DON'T DO THIS NOW, REMOVING "S" IN BEGINNING HAS the same effect
with plurals, in addition imbedded S's in the Metaphone are included
Lng = strlen(metaph);
lastChr = Lng -1;
if ( metaph[lastChr] == 'S' && Lng >= 3 ) metaph[lastChr] = '\0';
*/
return;
}
#ifdef METAPHONE_TEST
int
main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "usage: %s string\n", argv[0]);
return 1;
}
else
{
char output[51]="";
phonetic(argv[1], output, 50);
printf("metaphone(%s) = %s\n", argv[1], output);
return 0;
}
}
#endif /* METAPHONE_TEST */
#ifndef METAPHONE_TEST
/*
* SQL function: text_metaphone(text) returns text
*/
PG_FUNCTION_INFO_V1(text_metaphone);
Datum
text_metaphone(PG_FUNCTION_ARGS)
{
char outstr[51]="";
char *arg;
arg = _textout(PG_GETARG_TEXT_P(0));
phonetic(arg, outstr, 50);
PG_RETURN_TEXT_P(_textin(outstr));
}
/*
char outstr[51]="";
char *arg;
int32 metalen;
arg = _textout(PG_GETARG_TEXT_P(0));
metalen = PG_GETARG_INT32(1);
phonetic(arg, outstr, metalen);
*/
PG_FUNCTION_INFO_V1(text_metaphone_length);
Datum
text_metaphone_length(PG_FUNCTION_ARGS)
{
char outstr[51]="";
char *arg;
int32 metalen;
arg = _textout(PG_GETARG_TEXT_P(0));
metalen = PG_GETARG_INT32(1);
phonetic(arg, outstr, metalen);
PG_RETURN_TEXT_P(_textin(outstr));
}
#endif /* not METAPHONE_TEST */
|