4 * Functions for "fuzzy" comparison of strings
6 * Joe Conway <mail@joeconway.com>
8 * contrib/fuzzystrmatch/fuzzystrmatch.c
9 * Copyright (c) 2001-2015, PostgreSQL Global Development Group
10 * ALL RIGHTS RESERVED;
14 * Modified for PostgreSQL by Joe Conway.
15 * Based on CPAN's "Text-Metaphone-1.96" by Michael G Schwern <schwern@pobox.com>
16 * Code slightly modified for use as PostgreSQL function (palloc, elog, etc).
17 * Metaphone was originally created by Lawrence Philips and presented in article
18 * in "Computer Language" December 1990 issue.
20 * Permission to use, copy, modify, and distribute this software and its
21 * documentation for any purpose, without fee, and without a written agreement
22 * is hereby granted, provided that the above copyright notice and this
23 * paragraph and the following two paragraphs appear in all copies.
25 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
26 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
27 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
28 * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
33 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
34 * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
35 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
43 #include "mb/pg_wchar.h"
44 #include "utils/builtins.h"
51 static void _soundex(const char *instr, char *outstr);
55 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
56 static const char *soundex_table = "01230120022455012623010202";
59 soundex_code(char letter)
61 letter = toupper((unsigned char) letter);
62 /* Defend against non-ASCII letters */
63 if (letter >= 'A' && letter <= 'Z')
64 return soundex_table[letter - 'A'];
71 #define MAX_METAPHONE_STRLEN 255
74 * Original code by Michael G Schwern starts here.
75 * Code slightly modified for use as PostgreSQL function.
79 /**************************************************************************
80 metaphone -- Breaks english phrases down into their phonemes.
83 word -- An english word to be phonized
84 max_phonemes -- How many phonemes to calculate. If 0, then it
85 will phonize the entire phrase.
86 phoned_word -- The final phonized word. (We'll allocate the
89 error -- A simple error flag, returns TRUE or FALSE
91 NOTES: ALL non-alpha characters are ignored, this includes whitespace,
92 although non-alpha characters will break up phonemes.
93 ****************************************************************************/
96 /**************************************************************************
97 my constants -- constants I like
101 ***************************************************************************/
103 #define META_ERROR FALSE
104 #define META_SUCCESS TRUE
105 #define META_FAILURE FALSE
108 /* I add modifications to the traditional metaphone algorithm that you
109 might find in books. Define this if you want metaphone to behave
111 #undef USE_TRADITIONAL_METAPHONE
113 /* Special encodings */
117 static char Lookahead(char *word, int how_far);
118 static int _metaphone(char *word, int max_phonemes, char **phoned_word);
120 /* Metachar.h ... little bits about characters for metaphone */
123 /*-- Character encoding array & accessing macros --*/
124 /* Stolen directly out of the book... */
125 static const char _codes[26] = {
126 1, 16, 4, 16, 9, 2, 4, 16, 9, 2, 0, 2, 2, 2, 1, 4, 0, 2, 4, 4, 1, 0, 0, 0, 8, 0
127 /* a b c d e f g h i j k l m n o p q r s t u v w x y z */
133 if (isalpha((unsigned char) c))
135 c = toupper((unsigned char) c);
136 /* Defend against non-ASCII letters */
137 if (c >= 'A' && c <= 'Z')
138 return _codes[c - 'A'];
143 #define isvowel(c) (getcode(c) & 1) /* AEIOU */
145 /* These letters are passed through unchanged */
146 #define NOCHANGE(c) (getcode(c) & 2) /* FJMNR */
148 /* These form diphthongs when preceding H */
149 #define AFFECTH(c) (getcode(c) & 4) /* CGPST */
151 /* These make C and G soft */
152 #define MAKESOFT(c) (getcode(c) & 8) /* EIY */
154 /* These prevent GH from becoming F */
155 #define NOGHTOF(c) (getcode(c) & 16) /* BDH */
157 PG_FUNCTION_INFO_V1(levenshtein_with_costs);
159 levenshtein_with_costs(PG_FUNCTION_ARGS)
161 text *src = PG_GETARG_TEXT_PP(0);
162 text *dst = PG_GETARG_TEXT_PP(1);
163 int ins_c = PG_GETARG_INT32(2);
164 int del_c = PG_GETARG_INT32(3);
165 int sub_c = PG_GETARG_INT32(4);
171 /* Extract a pointer to the actual character data */
172 s_data = VARDATA_ANY(src);
173 t_data = VARDATA_ANY(dst);
174 /* Determine length of each string in bytes and characters */
175 s_bytes = VARSIZE_ANY_EXHDR(src);
176 t_bytes = VARSIZE_ANY_EXHDR(dst);
178 PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes, ins_c,
183 PG_FUNCTION_INFO_V1(levenshtein);
185 levenshtein(PG_FUNCTION_ARGS)
187 text *src = PG_GETARG_TEXT_PP(0);
188 text *dst = PG_GETARG_TEXT_PP(1);
194 /* Extract a pointer to the actual character data */
195 s_data = VARDATA_ANY(src);
196 t_data = VARDATA_ANY(dst);
197 /* Determine length of each string in bytes and characters */
198 s_bytes = VARSIZE_ANY_EXHDR(src);
199 t_bytes = VARSIZE_ANY_EXHDR(dst);
201 PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes, 1, 1,
206 PG_FUNCTION_INFO_V1(levenshtein_less_equal_with_costs);
208 levenshtein_less_equal_with_costs(PG_FUNCTION_ARGS)
210 text *src = PG_GETARG_TEXT_PP(0);
211 text *dst = PG_GETARG_TEXT_PP(1);
212 int ins_c = PG_GETARG_INT32(2);
213 int del_c = PG_GETARG_INT32(3);
214 int sub_c = PG_GETARG_INT32(4);
215 int max_d = PG_GETARG_INT32(5);
221 /* Extract a pointer to the actual character data */
222 s_data = VARDATA_ANY(src);
223 t_data = VARDATA_ANY(dst);
224 /* Determine length of each string in bytes and characters */
225 s_bytes = VARSIZE_ANY_EXHDR(src);
226 t_bytes = VARSIZE_ANY_EXHDR(dst);
228 PG_RETURN_INT32(varstr_levenshtein_less_equal(s_data, s_bytes, t_data,
229 t_bytes, ins_c, del_c,
234 PG_FUNCTION_INFO_V1(levenshtein_less_equal);
236 levenshtein_less_equal(PG_FUNCTION_ARGS)
238 text *src = PG_GETARG_TEXT_PP(0);
239 text *dst = PG_GETARG_TEXT_PP(1);
240 int max_d = PG_GETARG_INT32(2);
246 /* Extract a pointer to the actual character data */
247 s_data = VARDATA_ANY(src);
248 t_data = VARDATA_ANY(dst);
249 /* Determine length of each string in bytes and characters */
250 s_bytes = VARSIZE_ANY_EXHDR(src);
251 t_bytes = VARSIZE_ANY_EXHDR(dst);
253 PG_RETURN_INT32(varstr_levenshtein_less_equal(s_data, s_bytes, t_data,
254 t_bytes, 1, 1, 1, max_d));
259 * Calculates the metaphone of an input string.
260 * Returns number of characters requested
261 * (suggested value is 4)
263 PG_FUNCTION_INFO_V1(metaphone);
265 metaphone(PG_FUNCTION_ARGS)
267 char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0));
268 size_t str_i_len = strlen(str_i);
273 /* return an empty string if we receive one */
274 if (!(str_i_len > 0))
275 PG_RETURN_TEXT_P(cstring_to_text(""));
277 if (str_i_len > MAX_METAPHONE_STRLEN)
279 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
280 errmsg("argument exceeds the maximum length of %d bytes",
281 MAX_METAPHONE_STRLEN)));
283 reqlen = PG_GETARG_INT32(1);
284 if (reqlen > MAX_METAPHONE_STRLEN)
286 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
287 errmsg("output exceeds the maximum length of %d bytes",
288 MAX_METAPHONE_STRLEN)));
292 (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
293 errmsg("output cannot be empty string")));
296 retval = _metaphone(str_i, reqlen, &metaph);
297 if (retval == META_SUCCESS)
298 PG_RETURN_TEXT_P(cstring_to_text(metaph));
302 elog(ERROR, "metaphone: failure");
303 /* keep the compiler quiet */
310 * Original code by Michael G Schwern starts here.
311 * Code slightly modified for use as PostgreSQL
312 * function (palloc, etc).
315 /* I suppose I could have been using a character pointer instead of
316 * accessing the array directly... */
318 /* Look at the next letter in the word */
319 #define Next_Letter (toupper((unsigned char) word[w_idx+1]))
320 /* Look at the current letter in the word */
321 #define Curr_Letter (toupper((unsigned char) word[w_idx]))
322 /* Go N letters back. */
323 #define Look_Back_Letter(n) \
324 (w_idx >= (n) ? toupper((unsigned char) word[w_idx-(n)]) : '\0')
325 /* Previous letter. I dunno, should this return null on failure? */
326 #define Prev_Letter (Look_Back_Letter(1))
327 /* Look two letters down. It makes sure you don't walk off the string. */
328 #define After_Next_Letter \
329 (Next_Letter != '\0' ? toupper((unsigned char) word[w_idx+2]) : '\0')
330 #define Look_Ahead_Letter(n) toupper((unsigned char) Lookahead(word+w_idx, n))
333 /* Allows us to safely look ahead an arbitrary # of letters */
334 /* I probably could have just used strlen... */
336 Lookahead(char *word, int how_far)
338 char letter_ahead = '\0'; /* null by default */
341 for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
342 /* Edge forward in the string... */
344 letter_ahead = word[idx]; /* idx will be either == to how_far or at the
345 * end of the string */
350 /* phonize one letter */
351 #define Phonize(c) do {(*phoned_word)[p_idx++] = c;} while (0)
352 /* Slap a null character on the end of the phoned word */
353 #define End_Phoned_Word do {(*phoned_word)[p_idx] = '\0';} while (0)
354 /* How long is the phoned word? */
355 #define Phone_Len (p_idx)
357 /* Note is a letter is a 'break' in the word */
358 #define Isbreak(c) (!isalpha((unsigned char) (c)))
362 _metaphone(char *word, /* IN */
364 char **phoned_word) /* OUT */
366 int w_idx = 0; /* point in the phonization we're at. */
367 int p_idx = 0; /* end of the phoned phrase */
369 /*-- Parameter checks --*/
372 * Shouldn't be necessary, but left these here anyway jec Aug 3, 2001
375 /* Negative phoneme length is meaningless */
376 if (!(max_phonemes > 0))
378 elog(ERROR, "metaphone: Requested output length must be > 0");
380 /* Empty/null string is meaningless */
381 if ((word == NULL) || !(strlen(word) > 0))
383 elog(ERROR, "metaphone: Input string length must be > 0");
385 /*-- Allocate memory for our phoned_phrase --*/
386 if (max_phonemes == 0)
387 { /* Assume largest possible */
388 *phoned_word = palloc(sizeof(char) * strlen(word) +1);
392 *phoned_word = palloc(sizeof(char) * max_phonemes + 1);
395 /*-- The first phoneme has to be processed specially. --*/
396 /* Find our first letter */
397 for (; !isalpha((unsigned char) (Curr_Letter)); w_idx++)
399 /* On the off chance we were given nothing but crap... */
400 if (Curr_Letter == '\0')
403 return META_SUCCESS; /* For testing */
411 if (Next_Letter == 'E')
416 /* Remember, preserve vowels at the beginning */
423 /* [GKP]N becomes N */
427 if (Next_Letter == 'N')
435 * WH becomes H, WR becomes R W if followed by a vowel
438 if (Next_Letter == 'H' ||
441 Phonize(Next_Letter);
444 else if (isvowel(Next_Letter))
456 /* Vowels are kept */
459 * We did A already case 'A': case 'a':
465 Phonize(Curr_Letter);
475 /* On to the metaphoning */
476 for (; Curr_Letter != '\0' &&
477 (max_phonemes == 0 || Phone_Len < max_phonemes);
481 * How many letters to skip because an earlier encoding handled
484 unsigned short int skip_letter = 0;
488 * THOUGHT: It would be nice if, rather than having things like...
489 * well, SCI. For SCI you encode the S, then have to remember to skip
490 * the C. So the phonome SCI invades both S and C. It would be
491 * better, IMHO, to skip the C from the S part of the encoding. Hell,
495 /* Ignore non-alphas */
496 if (!isalpha((unsigned char) (Curr_Letter)))
499 /* Drop duplicates, except CC */
500 if (Curr_Letter == Prev_Letter &&
506 /* B -> B unless in MB */
508 if (Prev_Letter != 'M')
513 * 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW is
514 * handled in S) S if -CI-, -CE- or -CY- dropped if -SCI-,
515 * SCE-, -SCY- (handed in S) else K
518 if (MAKESOFT(Next_Letter))
520 if (After_Next_Letter == 'A' &&
526 else if (Prev_Letter == 'S')
533 else if (Next_Letter == 'H')
535 #ifndef USE_TRADITIONAL_METAPHONE
536 if (After_Next_Letter == 'R' ||
538 { /* Christ, School */
553 * J if in -DGE-, -DGI- or -DGY- else T
556 if (Next_Letter == 'G' &&
557 MAKESOFT(After_Next_Letter))
567 * F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
568 * dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
569 * -DGY- (handled in D) else J if in -GE-, -GI, -GY and not GG
573 if (Next_Letter == 'H')
575 if (!(NOGHTOF(Look_Back_Letter(3)) ||
576 Look_Back_Letter(4) == 'H'))
586 else if (Next_Letter == 'N')
588 if (Isbreak(After_Next_Letter) ||
589 (After_Next_Letter == 'E' &&
590 Look_Ahead_Letter(3) == 'D'))
597 else if (MAKESOFT(Next_Letter) &&
603 /* H if before a vowel and not after C,G,P,S,T */
605 if (isvowel(Next_Letter) &&
606 !AFFECTH(Prev_Letter))
611 * dropped if after C else K
614 if (Prev_Letter != 'C')
619 * F if before H else P
622 if (Next_Letter == 'H')
636 * 'sh' in -SH-, -SIO- or -SIA- or -SCHW- else S
639 if (Next_Letter == 'I' &&
640 (After_Next_Letter == 'O' ||
641 After_Next_Letter == 'A'))
643 else if (Next_Letter == 'H')
648 #ifndef USE_TRADITIONAL_METAPHONE
649 else if (Next_Letter == 'C' &&
650 Look_Ahead_Letter(2) == 'H' &&
651 Look_Ahead_Letter(3) == 'W')
662 * 'sh' in -TIA- or -TIO- else 'th' before H else T
665 if (Next_Letter == 'I' &&
666 (After_Next_Letter == 'O' ||
667 After_Next_Letter == 'A'))
669 else if (Next_Letter == 'H')
681 /* W before a vowel, else dropped */
683 if (isvowel(Next_Letter))
689 if (max_phonemes == 0 || Phone_Len < max_phonemes)
692 /* Y if followed by a vowel */
694 if (isvowel(Next_Letter))
701 /* No transformation */
708 Phonize(Curr_Letter);
715 w_idx += skip_letter;
720 return (META_SUCCESS);
721 } /* END metaphone */
725 * SQL function: soundex(text) returns text
727 PG_FUNCTION_INFO_V1(soundex);
730 soundex(PG_FUNCTION_ARGS)
732 char outstr[SOUNDEX_LEN + 1];
735 arg = text_to_cstring(PG_GETARG_TEXT_P(0));
737 _soundex(arg, outstr);
739 PG_RETURN_TEXT_P(cstring_to_text(outstr));
743 _soundex(const char *instr, char *outstr)
750 outstr[SOUNDEX_LEN] = '\0';
752 /* Skip leading non-alphabetic characters */
753 while (!isalpha((unsigned char) instr[0]) && instr[0])
759 outstr[0] = (char) 0;
763 /* Take the first letter as is */
764 *outstr++ = (char) toupper((unsigned char) *instr++);
767 while (*instr && count < SOUNDEX_LEN)
769 if (isalpha((unsigned char) *instr) &&
770 soundex_code(*instr) != soundex_code(*(instr - 1)))
772 *outstr = soundex_code(instr[0]);
783 while (count < SOUNDEX_LEN)
791 PG_FUNCTION_INFO_V1(difference);
794 difference(PG_FUNCTION_ARGS)
796 char sndx1[SOUNDEX_LEN + 1],
797 sndx2[SOUNDEX_LEN + 1];
801 _soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1);
802 _soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2);
805 for (i = 0; i < SOUNDEX_LEN; i++)
807 if (sndx1[i] == sndx2[i])
811 PG_RETURN_INT32(result);