4
* Functions for "fuzzy" comparison of strings
6
* Joe Conway <mail@joeconway.com>
8
* Copyright (c) 2001-2005, PostgreSQL Global Development Group
13
* Written based on a description of the algorithm by Michael Gilleland
14
* found at http://www.merriampark.com/ld.htm
15
* Also looked at levenshtein.c in the PHP 4.0.6 distribution for
20
* Modified for PostgreSQL by Joe Conway.
21
* Based on CPAN's "Text-Metaphone-1.96" by Michael G Schwern <schwern@pobox.com>
22
* Code slightly modified for use as PostgreSQL function (palloc, elog, etc).
23
* Metaphone was originally created by Lawrence Philips and presented in article
24
* in "Computer Language" December 1990 issue.
26
* Permission to use, copy, modify, and distribute this software and its
27
* documentation for any purpose, without fee, and without a written agreement
28
* is hereby granted, provided that the above copyright notice and this
29
* paragraph and the following two paragraphs appear in all copies.
31
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
32
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
33
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
34
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
37
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
38
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
39
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
40
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
41
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45
#include "fuzzystrmatch.h"
48
* Calculates Levenshtein Distance between two strings.
49
* Uses simplest and fastest cost model only, i.e. assumes a cost of 1 for
50
* each deletion, substitution, or insertion.
52
PG_FUNCTION_INFO_V1(levenshtein);
54
levenshtein(PG_FUNCTION_ARGS)
68
* Fetch the arguments. str_s is referred to as the "source" cols =
69
* length of source + 1 to allow for the initialization column str_t
70
* is referred to as the "target", rows = length of target + 1 rows =
71
* length of target + 1 to allow for the initialization row
73
str_s = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
74
str_t = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
76
cols = strlen(str_s) + 1;
77
rows = strlen(str_t) + 1;
80
* Restrict the length of the strings being compared to something
81
* reasonable because we will have to perform rows * cols
82
* calculations. If longer strings need to be compared, increase
83
* MAX_LEVENSHTEIN_STRLEN to suit (but within your tolerance for speed
86
if ((cols > MAX_LEVENSHTEIN_STRLEN + 1) || (rows > MAX_LEVENSHTEIN_STRLEN + 1))
88
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
89
errmsg("argument exceeds max length: %d",
90
MAX_LEVENSHTEIN_STRLEN)));
93
* If either rows or cols is 0, the answer is the other value. This
94
* makes sense since it would take that many insertions the build a
99
PG_RETURN_INT32(rows);
102
PG_RETURN_INT32(cols);
105
* Allocate two vectors of integers. One will be used for the "upper"
106
* row, the other for the "lower" row. Initialize the "upper" row to
109
u_cells = palloc(sizeof(int) * cols);
110
for (i = 0; i < cols; i++)
113
l_cells = palloc(sizeof(int) * cols);
116
* Use str_s0 to "rewind" the pointer to str_s in the nested for loop
122
* Loop throught the rows, starting at row 1. Row 0 is used for the
123
* initial "upper" row.
125
for (j = 1; j < rows; j++)
128
* We'll always start with col 1, and initialize lower row col 0
133
for (i = 1; i < cols; i++)
141
* The "cost" value is 0 if the character at the current col
142
* position in the source string, matches the character at the
143
* current row position in the target string; cost is 1
146
c = ((CHAREQ(str_s, str_t)) ? 0 : 1);
149
* c1 is upper right cell plus 1
154
* c2 is lower left cell plus 1
156
c2 = l_cells[i - 1] + 1;
159
* c3 is cell diagonally above to the left plus "cost"
161
c3 = u_cells[i - 1] + c;
164
* The lower right cell is set to the minimum of c1, c2, c3
166
l_cells[i] = (c1 < c2 ? c1 : c2) < c3 ? (c1 < c2 ? c1 : c2) : c3;
169
* Increment the pointer to str_s
175
* Lower row now becomes the upper row, and the upper row gets
176
* reused as the new lower row.
183
* Increment the pointer to str_t
188
* Rewind the pointer to str_s
194
* Because the final value (at position row, col) was swapped from the
195
* lower row to the upper row, that's where we'll find it.
197
PG_RETURN_INT32(u_cells[cols - 1]);
201
* Calculates the metaphone of an input string.
202
* Returns number of characters requested
203
* (suggested value is 4)
205
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
207
PG_FUNCTION_INFO_V1(metaphone);
209
metaphone(PG_FUNCTION_ARGS)
218
str_i = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
219
str_i_len = strlen(str_i);
221
/* return an empty string if we receive one */
222
if (!(str_i_len > 0))
223
PG_RETURN_TEXT_P(GET_TEXT(""));
225
if (str_i_len > MAX_METAPHONE_STRLEN)
227
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
228
errmsg("argument exceeds max length: %d",
229
MAX_METAPHONE_STRLEN)));
231
if (!(str_i_len > 0))
233
(errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
234
errmsg("argument is empty string")));
236
reqlen = PG_GETARG_INT32(1);
237
if (reqlen > MAX_METAPHONE_STRLEN)
239
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
240
errmsg("output length exceeds max length: %d",
241
MAX_METAPHONE_STRLEN)));
245
(errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
246
errmsg("output cannot be empty string")));
249
retval = _metaphone(str_i, reqlen, &metaph);
250
if (retval == META_SUCCESS)
252
result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(metaph)));
253
PG_RETURN_TEXT_P(result_text);
258
elog(ERROR, "metaphone: failure");
261
* Keep the compiler quiet
269
* Original code by Michael G Schwern starts here.
270
* Code slightly modified for use as PostgreSQL
271
* function (palloc, etc). Original includes
272
* are rolled into fuzzystrmatch.h
273
*------------------------------------------------------------------*/
275
/* I suppose I could have been using a character pointer instead of
276
* accessing the array directly... */
278
/* Look at the next letter in the word */
279
#define Next_Letter (toupper((unsigned char) word[w_idx+1]))
280
/* Look at the current letter in the word */
281
#define Curr_Letter (toupper((unsigned char) word[w_idx]))
282
/* Go N letters back. */
283
#define Look_Back_Letter(n) \
284
(w_idx >= (n) ? toupper((unsigned char) word[w_idx-(n)]) : '\0')
285
/* Previous letter. I dunno, should this return null on failure? */
286
#define Prev_Letter (Look_Back_Letter(1))
287
/* Look two letters down. It makes sure you don't walk off the string. */
288
#define After_Next_Letter \
289
(Next_Letter != '\0' ? toupper((unsigned char) word[w_idx+2]) : '\0')
290
#define Look_Ahead_Letter(n) toupper((unsigned char) Lookahead(word+w_idx, n))
293
/* Allows us to safely look ahead an arbitrary # of letters */
294
/* I probably could have just used strlen... */
296
Lookahead(char *word, int how_far)
298
char letter_ahead = '\0'; /* null by default */
301
for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
302
/* Edge forward in the string... */
304
letter_ahead = word[idx]; /* idx will be either == to how_far or at
305
* the end of the string */
310
/* phonize one letter */
311
#define Phonize(c) do {(*phoned_word)[p_idx++] = c;} while (0)
312
/* Slap a null character on the end of the phoned word */
313
#define End_Phoned_Word do {(*phoned_word)[p_idx] = '\0';} while (0)
314
/* How long is the phoned word? */
315
#define Phone_Len (p_idx)
317
/* Note is a letter is a 'break' in the word */
318
#define Isbreak(c) (!isalpha((unsigned char) (c)))
330
int w_idx = 0; /* point in the phonization we're at. */
331
int p_idx = 0; /* end of the phoned phrase */
333
/*-- Parameter checks --*/
336
* Shouldn't be necessary, but left these here anyway jec Aug 3, 2001
339
/* Negative phoneme length is meaningless */
340
if (!(max_phonemes > 0))
342
elog(ERROR, "metaphone: Requested output length must be > 0");
344
/* Empty/null string is meaningless */
345
if ((word == NULL) || !(strlen(word) > 0))
347
elog(ERROR, "metaphone: Input string length must be > 0");
349
/*-- Allocate memory for our phoned_phrase --*/
350
if (max_phonemes == 0)
351
{ /* Assume largest possible */
352
*phoned_word = palloc(sizeof(char) * strlen(word) +1);
358
*phoned_word = palloc(sizeof(char) * max_phonemes + 1);
363
/*-- The first phoneme has to be processed specially. --*/
364
/* Find our first letter */
365
for (; !isalpha((unsigned char) (Curr_Letter)); w_idx++)
367
/* On the off chance we were given nothing but crap... */
368
if (Curr_Letter == '\0')
371
return META_SUCCESS; /* For testing */
379
if (Next_Letter == 'E')
384
/* Remember, preserve vowels at the beginning */
391
/* [GKP]N becomes N */
395
if (Next_Letter == 'N')
403
* WH becomes H, WR becomes R W if followed by a vowel
406
if (Next_Letter == 'H' ||
409
Phonize(Next_Letter);
412
else if (isvowel(Next_Letter))
424
/* Vowels are kept */
427
* We did A already case 'A': case 'a':
433
Phonize(Curr_Letter);
443
/* On to the metaphoning */
444
for (; Curr_Letter != '\0' &&
445
(max_phonemes == 0 || Phone_Len < max_phonemes);
449
* How many letters to skip because an earlier encoding handled
452
unsigned short int skip_letter = 0;
456
* THOUGHT: It would be nice if, rather than having things
457
* like... well, SCI. For SCI you encode the S, then have to
458
* remember to skip the C. So the phonome SCI invades both S and
459
* C. It would be better, IMHO, to skip the C from the S part of
460
* the encoding. Hell, I'm trying it.
463
/* Ignore non-alphas */
464
if (!isalpha((unsigned char) (Curr_Letter)))
467
/* Drop duplicates, except CC */
468
if (Curr_Letter == Prev_Letter &&
474
/* B -> B unless in MB */
476
if (Prev_Letter != 'M')
481
* 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW
482
* is handled in S) S if -CI-, -CE- or -CY- dropped if
483
* -SCI-, SCE-, -SCY- (handed in S) else K
486
if (MAKESOFT(Next_Letter))
488
if (After_Next_Letter == 'A' &&
494
else if (Prev_Letter == 'S')
501
else if (Next_Letter == 'H')
503
#ifndef USE_TRADITIONAL_METAPHONE
504
if (After_Next_Letter == 'R' ||
506
{ /* Christ, School */
521
* J if in -DGE-, -DGI- or -DGY- else T
524
if (Next_Letter == 'G' &&
525
MAKESOFT(After_Next_Letter))
535
* F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
536
* dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
537
* -DGY- (handled in D) else J if in -GE-, -GI, -GY and
541
if (Next_Letter == 'H')
543
if (!(NOGHTOF(Look_Back_Letter(3)) ||
544
Look_Back_Letter(4) == 'H'))
554
else if (Next_Letter == 'N')
556
if (Isbreak(After_Next_Letter) ||
557
(After_Next_Letter == 'E' &&
558
Look_Ahead_Letter(3) == 'D'))
565
else if (MAKESOFT(Next_Letter) &&
571
/* H if before a vowel and not after C,G,P,S,T */
573
if (isvowel(Next_Letter) &&
574
!AFFECTH(Prev_Letter))
579
* dropped if after C else K
582
if (Prev_Letter != 'C')
587
* F if before H else P
590
if (Next_Letter == 'H')
604
* 'sh' in -SH-, -SIO- or -SIA- or -SCHW- else S
607
if (Next_Letter == 'I' &&
608
(After_Next_Letter == 'O' ||
609
After_Next_Letter == 'A'))
611
else if (Next_Letter == 'H')
616
#ifndef USE_TRADITIONAL_METAPHONE
617
else if (Next_Letter == 'C' &&
618
Look_Ahead_Letter(2) == 'H' &&
619
Look_Ahead_Letter(3) == 'W')
630
* 'sh' in -TIA- or -TIO- else 'th' before H else T
633
if (Next_Letter == 'I' &&
634
(After_Next_Letter == 'O' ||
635
After_Next_Letter == 'A'))
637
else if (Next_Letter == 'H')
649
/* W before a vowel, else dropped */
651
if (isvowel(Next_Letter))
657
if (max_phonemes == 0 || Phone_Len < max_phonemes)
660
/* Y if followed by a vowel */
662
if (isvowel(Next_Letter))
669
/* No transformation */
676
Phonize(Curr_Letter);
683
w_idx += skip_letter;
688
return (META_SUCCESS);
689
} /* END metaphone */
693
* SQL function: soundex(text) returns text
695
PG_FUNCTION_INFO_V1(soundex);
698
soundex(PG_FUNCTION_ARGS)
700
char outstr[SOUNDEX_LEN + 1];
703
arg = _textout(PG_GETARG_TEXT_P(0));
705
_soundex(arg, outstr);
707
PG_RETURN_TEXT_P(_textin(outstr));
711
_soundex(const char *instr, char *outstr)
718
outstr[SOUNDEX_LEN] = '\0';
720
/* Skip leading non-alphabetic characters */
721
while (!isalpha((unsigned char) instr[0]) && instr[0])
727
outstr[0] = (char) 0;
731
/* Take the first letter as is */
732
*outstr++ = (char) toupper((unsigned char) *instr++);
735
while (*instr && count < SOUNDEX_LEN)
737
if (isalpha((unsigned char) *instr) &&
738
soundex_code(*instr) != soundex_code(*(instr - 1)))
740
*outstr = soundex_code(instr[0]);
751
while (count < SOUNDEX_LEN)