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 if (!(str_i_len > 0))
285 (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
286 errmsg("argument is empty string")));
288 reqlen = PG_GETARG_INT32(1);
289 if (reqlen > MAX_METAPHONE_STRLEN)
291 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292 errmsg("output exceeds the maximum length of %d bytes",
293 MAX_METAPHONE_STRLEN)));
297 (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
298 errmsg("output cannot be empty string")));
301 retval = _metaphone(str_i, reqlen, &metaph);
302 if (retval == META_SUCCESS)
303 PG_RETURN_TEXT_P(cstring_to_text(metaph));
307 elog(ERROR, "metaphone: failure");
308 /* keep the compiler quiet */
315 * Original code by Michael G Schwern starts here.
316 * Code slightly modified for use as PostgreSQL
317 * function (palloc, etc).
320 /* I suppose I could have been using a character pointer instead of
321 * accessing the array directly... */
323 /* Look at the next letter in the word */
324 #define Next_Letter (toupper((unsigned char) word[w_idx+1]))
325 /* Look at the current letter in the word */
326 #define Curr_Letter (toupper((unsigned char) word[w_idx]))
327 /* Go N letters back. */
328 #define Look_Back_Letter(n) \
329 (w_idx >= (n) ? toupper((unsigned char) word[w_idx-(n)]) : '\0')
330 /* Previous letter. I dunno, should this return null on failure? */
331 #define Prev_Letter (Look_Back_Letter(1))
332 /* Look two letters down. It makes sure you don't walk off the string. */
333 #define After_Next_Letter \
334 (Next_Letter != '\0' ? toupper((unsigned char) word[w_idx+2]) : '\0')
335 #define Look_Ahead_Letter(n) toupper((unsigned char) Lookahead(word+w_idx, n))
338 /* Allows us to safely look ahead an arbitrary # of letters */
339 /* I probably could have just used strlen... */
341 Lookahead(char *word, int how_far)
343 char letter_ahead = '\0'; /* null by default */
346 for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
347 /* Edge forward in the string... */
349 letter_ahead = word[idx]; /* idx will be either == to how_far or at the
350 * end of the string */
355 /* phonize one letter */
356 #define Phonize(c) do {(*phoned_word)[p_idx++] = c;} while (0)
357 /* Slap a null character on the end of the phoned word */
358 #define End_Phoned_Word do {(*phoned_word)[p_idx] = '\0';} while (0)
359 /* How long is the phoned word? */
360 #define Phone_Len (p_idx)
362 /* Note is a letter is a 'break' in the word */
363 #define Isbreak(c) (!isalpha((unsigned char) (c)))
367 _metaphone(char *word, /* IN */
369 char **phoned_word) /* OUT */
371 int w_idx = 0; /* point in the phonization we're at. */
372 int p_idx = 0; /* end of the phoned phrase */
374 /*-- Parameter checks --*/
377 * Shouldn't be necessary, but left these here anyway jec Aug 3, 2001
380 /* Negative phoneme length is meaningless */
381 if (!(max_phonemes > 0))
383 elog(ERROR, "metaphone: Requested output length must be > 0");
385 /* Empty/null string is meaningless */
386 if ((word == NULL) || !(strlen(word) > 0))
388 elog(ERROR, "metaphone: Input string length must be > 0");
390 /*-- Allocate memory for our phoned_phrase --*/
391 if (max_phonemes == 0)
392 { /* Assume largest possible */
393 *phoned_word = palloc(sizeof(char) * strlen(word) +1);
397 *phoned_word = palloc(sizeof(char) * max_phonemes + 1);
400 /*-- The first phoneme has to be processed specially. --*/
401 /* Find our first letter */
402 for (; !isalpha((unsigned char) (Curr_Letter)); w_idx++)
404 /* On the off chance we were given nothing but crap... */
405 if (Curr_Letter == '\0')
408 return META_SUCCESS; /* For testing */
416 if (Next_Letter == 'E')
421 /* Remember, preserve vowels at the beginning */
428 /* [GKP]N becomes N */
432 if (Next_Letter == 'N')
440 * WH becomes H, WR becomes R W if followed by a vowel
443 if (Next_Letter == 'H' ||
446 Phonize(Next_Letter);
449 else if (isvowel(Next_Letter))
461 /* Vowels are kept */
464 * We did A already case 'A': case 'a':
470 Phonize(Curr_Letter);
480 /* On to the metaphoning */
481 for (; Curr_Letter != '\0' &&
482 (max_phonemes == 0 || Phone_Len < max_phonemes);
486 * How many letters to skip because an earlier encoding handled
489 unsigned short int skip_letter = 0;
493 * THOUGHT: It would be nice if, rather than having things like...
494 * well, SCI. For SCI you encode the S, then have to remember to skip
495 * the C. So the phonome SCI invades both S and C. It would be
496 * better, IMHO, to skip the C from the S part of the encoding. Hell,
500 /* Ignore non-alphas */
501 if (!isalpha((unsigned char) (Curr_Letter)))
504 /* Drop duplicates, except CC */
505 if (Curr_Letter == Prev_Letter &&
511 /* B -> B unless in MB */
513 if (Prev_Letter != 'M')
518 * 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW is
519 * handled in S) S if -CI-, -CE- or -CY- dropped if -SCI-,
520 * SCE-, -SCY- (handed in S) else K
523 if (MAKESOFT(Next_Letter))
525 if (After_Next_Letter == 'A' &&
531 else if (Prev_Letter == 'S')
538 else if (Next_Letter == 'H')
540 #ifndef USE_TRADITIONAL_METAPHONE
541 if (After_Next_Letter == 'R' ||
543 { /* Christ, School */
558 * J if in -DGE-, -DGI- or -DGY- else T
561 if (Next_Letter == 'G' &&
562 MAKESOFT(After_Next_Letter))
572 * F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
573 * dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
574 * -DGY- (handled in D) else J if in -GE-, -GI, -GY and not GG
578 if (Next_Letter == 'H')
580 if (!(NOGHTOF(Look_Back_Letter(3)) ||
581 Look_Back_Letter(4) == 'H'))
591 else if (Next_Letter == 'N')
593 if (Isbreak(After_Next_Letter) ||
594 (After_Next_Letter == 'E' &&
595 Look_Ahead_Letter(3) == 'D'))
602 else if (MAKESOFT(Next_Letter) &&
608 /* H if before a vowel and not after C,G,P,S,T */
610 if (isvowel(Next_Letter) &&
611 !AFFECTH(Prev_Letter))
616 * dropped if after C else K
619 if (Prev_Letter != 'C')
624 * F if before H else P
627 if (Next_Letter == 'H')
641 * 'sh' in -SH-, -SIO- or -SIA- or -SCHW- else S
644 if (Next_Letter == 'I' &&
645 (After_Next_Letter == 'O' ||
646 After_Next_Letter == 'A'))
648 else if (Next_Letter == 'H')
653 #ifndef USE_TRADITIONAL_METAPHONE
654 else if (Next_Letter == 'C' &&
655 Look_Ahead_Letter(2) == 'H' &&
656 Look_Ahead_Letter(3) == 'W')
667 * 'sh' in -TIA- or -TIO- else 'th' before H else T
670 if (Next_Letter == 'I' &&
671 (After_Next_Letter == 'O' ||
672 After_Next_Letter == 'A'))
674 else if (Next_Letter == 'H')
686 /* W before a vowel, else dropped */
688 if (isvowel(Next_Letter))
694 if (max_phonemes == 0 || Phone_Len < max_phonemes)
697 /* Y if followed by a vowel */
699 if (isvowel(Next_Letter))
706 /* No transformation */
713 Phonize(Curr_Letter);
720 w_idx += skip_letter;
725 return (META_SUCCESS);
726 } /* END metaphone */
730 * SQL function: soundex(text) returns text
732 PG_FUNCTION_INFO_V1(soundex);
735 soundex(PG_FUNCTION_ARGS)
737 char outstr[SOUNDEX_LEN + 1];
740 arg = text_to_cstring(PG_GETARG_TEXT_P(0));
742 _soundex(arg, outstr);
744 PG_RETURN_TEXT_P(cstring_to_text(outstr));
748 _soundex(const char *instr, char *outstr)
755 outstr[SOUNDEX_LEN] = '\0';
757 /* Skip leading non-alphabetic characters */
758 while (!isalpha((unsigned char) instr[0]) && instr[0])
764 outstr[0] = (char) 0;
768 /* Take the first letter as is */
769 *outstr++ = (char) toupper((unsigned char) *instr++);
772 while (*instr && count < SOUNDEX_LEN)
774 if (isalpha((unsigned char) *instr) &&
775 soundex_code(*instr) != soundex_code(*(instr - 1)))
777 *outstr = soundex_code(instr[0]);
788 while (count < SOUNDEX_LEN)
796 PG_FUNCTION_INFO_V1(difference);
799 difference(PG_FUNCTION_ARGS)
801 char sndx1[SOUNDEX_LEN + 1],
802 sndx2[SOUNDEX_LEN + 1];
806 _soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1);
807 _soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2);
810 for (i = 0; i < SOUNDEX_LEN; i++)
812 if (sndx1[i] == sndx2[i])
816 PG_RETURN_INT32(result);