PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fuzzystrmatch.c
Go to the documentation of this file.
1/*
2 * fuzzystrmatch.c
3 *
4 * Functions for "fuzzy" comparison of strings
5 *
6 * Joe Conway <mail@joeconway.com>
7 *
8 * contrib/fuzzystrmatch/fuzzystrmatch.c
9 * Copyright (c) 2001-2026, PostgreSQL Global Development Group
10 * ALL RIGHTS RESERVED;
11 *
12 * metaphone()
13 * -----------
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.
19 *
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.
24 *
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.
30 *
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.
36 *
37 */
38
39#include "postgres.h"
40
41#include <ctype.h>
42
43#include "utils/builtins.h"
44#include "utils/varlena.h"
45#include "varatt.h"
46
48 .name = "fuzzystrmatch",
49 .version = PG_VERSION
50);
51
52/*
53 * Soundex
54 */
55static void _soundex(const char *instr, char *outstr);
56
57#define SOUNDEX_LEN 4
58
59/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
60static const char *const soundex_table = "01230120022455012623010202";
61
62static char
64{
65 letter = pg_ascii_toupper((unsigned char) letter);
66 /* Defend against non-ASCII letters */
67 if (letter >= 'A' && letter <= 'Z')
68 return soundex_table[letter - 'A'];
69 return letter;
70}
71
72/*
73 * Metaphone
74 */
75#define MAX_METAPHONE_STRLEN 255
76
77/*
78 * Original code by Michael G Schwern starts here.
79 * Code slightly modified for use as PostgreSQL function.
80 */
81
82
83/**************************************************************************
84 metaphone -- Breaks english phrases down into their phonemes.
85
86 Input
87 word -- An english word to be phonized
88 max_phonemes -- How many phonemes to calculate. If 0, then it
89 will phonize the entire phrase.
90 phoned_word -- The final phonized word. (We'll allocate the
91 memory.)
92 Output
93 error -- A simple error flag, returns true or false
94
95 NOTES: ALL non-alpha characters are ignored, this includes whitespace,
96 although non-alpha characters will break up phonemes.
97****************************************************************************/
98
99
100/* I add modifications to the traditional metaphone algorithm that you
101 might find in books. Define this if you want metaphone to behave
102 traditionally */
103#undef USE_TRADITIONAL_METAPHONE
104
105/* Special encodings */
106#define SH 'X'
107#define TH '0'
108
109static char Lookahead(char *word, int how_far);
110static void _metaphone(char *word, int max_phonemes, char **phoned_word);
111
112/* Metachar.h ... little bits about characters for metaphone */
113
114
115/*-- Character encoding array & accessing macros --*/
116/* Stolen directly out of the book... */
117static const char _codes[26] = {
118 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
119/* 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 */
120};
121
122static int
124{
125 c = pg_ascii_toupper((unsigned char) c);
126 /* Defend against non-ASCII letters */
127 if (c >= 'A' && c <= 'Z')
128 return _codes[c - 'A'];
129
130 return 0;
131}
132
133static bool
135{
136 return (c >= 'A' && c <= 'Z') ||
137 (c >= 'a' && c <= 'z');
138}
139
140#define isvowel(c) (getcode(c) & 1) /* AEIOU */
141
142/* These letters are passed through unchanged */
143#define NOCHANGE(c) (getcode(c) & 2) /* FJMNR */
144
145/* These form diphthongs when preceding H */
146#define AFFECTH(c) (getcode(c) & 4) /* CGPST */
147
148/* These make C and G soft */
149#define MAKESOFT(c) (getcode(c) & 8) /* EIY */
150
151/* These prevent GH from becoming F */
152#define NOGHTOF(c) (getcode(c) & 16) /* BDH */
153
155Datum
157{
158 text *src = PG_GETARG_TEXT_PP(0);
160 int ins_c = PG_GETARG_INT32(2);
161 int del_c = PG_GETARG_INT32(3);
162 int sub_c = PG_GETARG_INT32(4);
163 const char *s_data;
164 const char *t_data;
165 int s_bytes,
166 t_bytes;
167
168 /* Extract a pointer to the actual character data */
169 s_data = VARDATA_ANY(src);
170 t_data = VARDATA_ANY(dst);
171 /* Determine length of each string in bytes */
174
176 ins_c, del_c, sub_c, false));
177}
178
179
181Datum
183{
184 text *src = PG_GETARG_TEXT_PP(0);
186 const char *s_data;
187 const char *t_data;
188 int s_bytes,
189 t_bytes;
190
191 /* Extract a pointer to the actual character data */
192 s_data = VARDATA_ANY(src);
193 t_data = VARDATA_ANY(dst);
194 /* Determine length of each string in bytes */
197
199 1, 1, 1, false));
200}
201
202
204Datum
206{
207 text *src = PG_GETARG_TEXT_PP(0);
209 int ins_c = PG_GETARG_INT32(2);
210 int del_c = PG_GETARG_INT32(3);
211 int sub_c = PG_GETARG_INT32(4);
212 int max_d = PG_GETARG_INT32(5);
213 const char *s_data;
214 const char *t_data;
215 int s_bytes,
216 t_bytes;
217
218 /* Extract a pointer to the actual character data */
219 s_data = VARDATA_ANY(src);
220 t_data = VARDATA_ANY(dst);
221 /* Determine length of each string in bytes */
224
226 t_data, t_bytes,
227 ins_c, del_c, sub_c,
228 max_d, false));
229}
230
231
233Datum
235{
236 text *src = PG_GETARG_TEXT_PP(0);
238 int max_d = PG_GETARG_INT32(2);
239 const char *s_data;
240 const char *t_data;
241 int s_bytes,
242 t_bytes;
243
244 /* Extract a pointer to the actual character data */
245 s_data = VARDATA_ANY(src);
246 t_data = VARDATA_ANY(dst);
247 /* Determine length of each string in bytes */
250
252 t_data, t_bytes,
253 1, 1, 1,
254 max_d, false));
255}
256
257
258/*
259 * Calculates the metaphone of an input string.
260 * Returns number of characters requested
261 * (suggested value is 4)
262 */
264Datum
266{
268 size_t str_i_len = strlen(str_i);
269 int reqlen;
270 char *metaph;
271
272 /* return an empty string if we receive one */
273 if (!(str_i_len > 0))
275
279 errmsg("argument exceeds the maximum length of %d bytes",
281
286 errmsg("output exceeds the maximum length of %d bytes",
288
289 if (!(reqlen > 0))
292 errmsg("output cannot be empty string")));
293
296}
297
298
299/*
300 * Original code by Michael G Schwern starts here.
301 * Code slightly modified for use as PostgreSQL
302 * function (palloc, etc).
303 */
304
305/* I suppose I could have been using a character pointer instead of
306 * accessing the array directly... */
307
308/* Look at the next letter in the word */
309#define Next_Letter (pg_ascii_toupper((unsigned char) word[w_idx+1]))
310/* Look at the current letter in the word */
311#define Curr_Letter (pg_ascii_toupper((unsigned char) word[w_idx]))
312/* Go N letters back. */
313#define Look_Back_Letter(n) \
314 (w_idx >= (n) ? pg_ascii_toupper((unsigned char) word[w_idx-(n)]) : '\0')
315/* Previous letter. I dunno, should this return null on failure? */
316#define Prev_Letter (Look_Back_Letter(1))
317/* Look two letters down. It makes sure you don't walk off the string. */
318#define After_Next_Letter \
319 (Next_Letter != '\0' ? pg_ascii_toupper((unsigned char) word[w_idx+2]) : '\0')
320#define Look_Ahead_Letter(n) pg_ascii_toupper((unsigned char) Lookahead(word+w_idx, n))
321
322
323/* Allows us to safely look ahead an arbitrary # of letters */
324/* I probably could have just used strlen... */
325static char
327{
328 char letter_ahead = '\0'; /* null by default */
329 int idx;
330
331 for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
332 /* Edge forward in the string... */
333
334 letter_ahead = word[idx]; /* idx will be either == to how_far or at the
335 * end of the string */
336 return letter_ahead;
337}
338
339
340/* phonize one letter */
341#define Phonize(c) do {(*phoned_word)[p_idx++] = c;} while (0)
342/* Slap a null character on the end of the phoned word */
343#define End_Phoned_Word do {(*phoned_word)[p_idx] = '\0';} while (0)
344/* How long is the phoned word? */
345#define Phone_Len (p_idx)
346
347/* Note is a letter is a 'break' in the word */
348#define Isbreak(c) (!ascii_isalpha((unsigned char) (c)))
349
350
351static void
352_metaphone(char *word, /* IN */
353 int max_phonemes,
354 char **phoned_word) /* OUT */
355{
356 int w_idx = 0; /* point in the phonization we're at. */
357 int p_idx = 0; /* end of the phoned phrase */
358
359 /*-- Parameter checks --*/
360
361 /*
362 * Shouldn't be necessary, but left these here anyway jec Aug 3, 2001
363 */
364
365 /* Negative phoneme length is meaningless */
366 if (!(max_phonemes > 0))
367 /* internal error */
368 elog(ERROR, "metaphone: Requested output length must be > 0");
369
370 /* Empty/null string is meaningless */
371 if ((word == NULL) || !(strlen(word) > 0))
372 /* internal error */
373 elog(ERROR, "metaphone: Input string length must be > 0");
374
375 /*-- Allocate memory for our phoned_phrase --*/
376 if (max_phonemes == 0)
377 { /* Assume largest possible */
378 *phoned_word = palloc(sizeof(char) * strlen(word) + 1);
379 }
380 else
381 {
382 *phoned_word = palloc(sizeof(char) * max_phonemes + 1);
383 }
384
385 /*-- The first phoneme has to be processed specially. --*/
386 /* Find our first letter */
387 for (; !ascii_isalpha((unsigned char) (Curr_Letter)); w_idx++)
388 {
389 /* On the off chance we were given nothing but crap... */
390 if (Curr_Letter == '\0')
391 {
393 return;
394 }
395 }
396
397 switch (Curr_Letter)
398 {
399 /* AE becomes E */
400 case 'A':
401 if (Next_Letter == 'E')
402 {
403 Phonize('E');
404 w_idx += 2;
405 }
406 /* Remember, preserve vowels at the beginning */
407 else
408 {
409 Phonize('A');
410 w_idx++;
411 }
412 break;
413 /* [GKP]N becomes N */
414 case 'G':
415 case 'K':
416 case 'P':
417 if (Next_Letter == 'N')
418 {
419 Phonize('N');
420 w_idx += 2;
421 }
422 break;
423
424 /*
425 * WH becomes H, WR becomes R W if followed by a vowel
426 */
427 case 'W':
428 if (Next_Letter == 'H' ||
429 Next_Letter == 'R')
430 {
432 w_idx += 2;
433 }
434 else if (isvowel(Next_Letter))
435 {
436 Phonize('W');
437 w_idx += 2;
438 }
439 /* else ignore */
440 break;
441 /* X becomes S */
442 case 'X':
443 Phonize('S');
444 w_idx++;
445 break;
446 /* Vowels are kept */
447
448 /*
449 * We did A already case 'A': case 'a':
450 */
451 case 'E':
452 case 'I':
453 case 'O':
454 case 'U':
456 w_idx++;
457 break;
458 default:
459 /* do nothing */
460 break;
461 }
462
463
464
465 /* On to the metaphoning */
466 for (; Curr_Letter != '\0' &&
468 w_idx++)
469 {
470 /*
471 * How many letters to skip because an earlier encoding handled
472 * multiple letters
473 */
474 unsigned short int skip_letter = 0;
475
476
477 /*
478 * THOUGHT: It would be nice if, rather than having things like...
479 * well, SCI. For SCI you encode the S, then have to remember to skip
480 * the C. So the phonome SCI invades both S and C. It would be
481 * better, IMHO, to skip the C from the S part of the encoding. Hell,
482 * I'm trying it.
483 */
484
485 /* Ignore non-alphas */
486 if (!ascii_isalpha((unsigned char) (Curr_Letter)))
487 continue;
488
489 /* Drop duplicates, except CC */
490 if (Curr_Letter == Prev_Letter &&
491 Curr_Letter != 'C')
492 continue;
493
494 switch (Curr_Letter)
495 {
496 /* B -> B unless in MB */
497 case 'B':
498 if (Prev_Letter != 'M')
499 Phonize('B');
500 break;
501
502 /*
503 * 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW is
504 * handled in S) S if -CI-, -CE- or -CY- dropped if -SCI-,
505 * SCE-, -SCY- (handed in S) else K
506 */
507 case 'C':
509 { /* C[IEY] */
510 if (After_Next_Letter == 'A' &&
511 Next_Letter == 'I')
512 { /* CIA */
513 Phonize(SH);
514 }
515 /* SC[IEY] */
516 else if (Prev_Letter == 'S')
517 {
518 /* Dropped */
519 }
520 else
521 Phonize('S');
522 }
523 else if (Next_Letter == 'H')
524 {
525#ifndef USE_TRADITIONAL_METAPHONE
526 if (After_Next_Letter == 'R' ||
527 Prev_Letter == 'S')
528 { /* Christ, School */
529 Phonize('K');
530 }
531 else
532 Phonize(SH);
533#else
534 Phonize(SH);
535#endif
536 skip_letter++;
537 }
538 else
539 Phonize('K');
540 break;
541
542 /*
543 * J if in -DGE-, -DGI- or -DGY- else T
544 */
545 case 'D':
546 if (Next_Letter == 'G' &&
548 {
549 Phonize('J');
550 skip_letter++;
551 }
552 else
553 Phonize('T');
554 break;
555
556 /*
557 * F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
558 * dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
559 * -DGY- (handled in D) else J if in -GE-, -GI, -GY and not GG
560 * else K
561 */
562 case 'G':
563 if (Next_Letter == 'H')
564 {
565 if (!(NOGHTOF(Look_Back_Letter(3)) ||
566 Look_Back_Letter(4) == 'H'))
567 {
568 Phonize('F');
569 skip_letter++;
570 }
571 else
572 {
573 /* silent */
574 }
575 }
576 else if (Next_Letter == 'N')
577 {
579 (After_Next_Letter == 'E' &&
580 Look_Ahead_Letter(3) == 'D'))
581 {
582 /* dropped */
583 }
584 else
585 Phonize('K');
586 }
587 else if (MAKESOFT(Next_Letter) &&
588 Prev_Letter != 'G')
589 Phonize('J');
590 else
591 Phonize('K');
592 break;
593 /* H if before a vowel and not after C,G,P,S,T */
594 case 'H':
595 if (isvowel(Next_Letter) &&
597 Phonize('H');
598 break;
599
600 /*
601 * dropped if after C else K
602 */
603 case 'K':
604 if (Prev_Letter != 'C')
605 Phonize('K');
606 break;
607
608 /*
609 * F if before H else P
610 */
611 case 'P':
612 if (Next_Letter == 'H')
613 Phonize('F');
614 else
615 Phonize('P');
616 break;
617
618 /*
619 * K
620 */
621 case 'Q':
622 Phonize('K');
623 break;
624
625 /*
626 * 'sh' in -SH-, -SIO- or -SIA- or -SCHW- else S
627 */
628 case 'S':
629 if (Next_Letter == 'I' &&
630 (After_Next_Letter == 'O' ||
631 After_Next_Letter == 'A'))
632 Phonize(SH);
633 else if (Next_Letter == 'H')
634 {
635 Phonize(SH);
636 skip_letter++;
637 }
638#ifndef USE_TRADITIONAL_METAPHONE
639 else if (Next_Letter == 'C' &&
640 Look_Ahead_Letter(2) == 'H' &&
641 Look_Ahead_Letter(3) == 'W')
642 {
643 Phonize(SH);
644 skip_letter += 2;
645 }
646#endif
647 else
648 Phonize('S');
649 break;
650
651 /*
652 * 'sh' in -TIA- or -TIO- else 'th' before H else T
653 */
654 case 'T':
655 if (Next_Letter == 'I' &&
656 (After_Next_Letter == 'O' ||
657 After_Next_Letter == 'A'))
658 Phonize(SH);
659 else if (Next_Letter == 'H')
660 {
661 Phonize(TH);
662 skip_letter++;
663 }
664 else
665 Phonize('T');
666 break;
667 /* F */
668 case 'V':
669 Phonize('F');
670 break;
671 /* W before a vowel, else dropped */
672 case 'W':
673 if (isvowel(Next_Letter))
674 Phonize('W');
675 break;
676 /* KS */
677 case 'X':
678 Phonize('K');
679 if (max_phonemes == 0 || Phone_Len < max_phonemes)
680 Phonize('S');
681 break;
682 /* Y if followed by a vowel */
683 case 'Y':
684 if (isvowel(Next_Letter))
685 Phonize('Y');
686 break;
687 /* S */
688 case 'Z':
689 Phonize('S');
690 break;
691 /* No transformation */
692 case 'F':
693 case 'J':
694 case 'L':
695 case 'M':
696 case 'N':
697 case 'R':
699 break;
700 default:
701 /* nothing */
702 break;
703 } /* END SWITCH */
704
706 } /* END FOR */
707
709} /* END metaphone */
710
711
712/*
713 * SQL function: soundex(text) returns text
714 */
716
717Datum
729
730static void
731_soundex(const char *instr, char *outstr)
732{
733 int count;
734
735 Assert(instr);
736 Assert(outstr);
737
738 /* Skip leading non-alphabetic characters */
739 while (*instr && !ascii_isalpha((unsigned char) *instr))
740 ++instr;
741
742 /* If no string left, return all-zeroes buffer */
743 if (!*instr)
744 {
745 memset(outstr, '\0', SOUNDEX_LEN + 1);
746 return;
747 }
748
749 /* Take the first letter as is */
750 *outstr++ = (char) pg_ascii_toupper((unsigned char) *instr++);
751
752 count = 1;
753 while (*instr && count < SOUNDEX_LEN)
754 {
755 if (ascii_isalpha((unsigned char) *instr) &&
756 soundex_code(*instr) != soundex_code(*(instr - 1)))
757 {
758 *outstr = soundex_code(*instr);
759 if (*outstr != '0')
760 {
761 ++outstr;
762 ++count;
763 }
764 }
765 ++instr;
766 }
767
768 /* Fill with 0's */
769 while (count < SOUNDEX_LEN)
770 {
771 *outstr = '0';
772 ++outstr;
773 ++count;
774 }
775
776 /* And null-terminate */
777 *outstr = '\0';
778}
779
781
782Datum
784{
785 char sndx1[SOUNDEX_LEN + 1],
786 sndx2[SOUNDEX_LEN + 1];
787 int i,
788 result;
789
792
793 result = 0;
794 for (i = 0; i < SOUNDEX_LEN; i++)
795 {
796 if (sndx1[i] == sndx2[i])
797 result++;
798 }
799
800 PG_RETURN_INT32(result);
801}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
#define TextDatumGetCString(d)
Definition builtins.h:98
#define Assert(condition)
Definition c.h:873
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
Datum metaphone(PG_FUNCTION_ARGS)
static void _metaphone(char *word, int max_phonemes, char **phoned_word)
#define SOUNDEX_LEN
#define After_Next_Letter
Datum levenshtein_less_equal_with_costs(PG_FUNCTION_ARGS)
#define Look_Back_Letter(n)
#define Curr_Letter
static const char *const soundex_table
#define SH
#define MAKESOFT(c)
Datum soundex(PG_FUNCTION_ARGS)
static char soundex_code(char letter)
static const char _codes[26]
Datum levenshtein_with_costs(PG_FUNCTION_ARGS)
#define Isbreak(c)
#define End_Phoned_Word
#define isvowel(c)
#define Prev_Letter
static bool ascii_isalpha(char c)
#define TH
#define NOGHTOF(c)
#define Next_Letter
static void _soundex(const char *instr, char *outstr)
#define Phonize(c)
static int getcode(char c)
Datum difference(PG_FUNCTION_ARGS)
#define MAX_METAPHONE_STRLEN
static char Lookahead(char *word, int how_far)
#define AFFECTH(c)
#define Phone_Len
Datum levenshtein_less_equal(PG_FUNCTION_ARGS)
#define Look_Ahead_Letter(n)
Datum levenshtein(PG_FUNCTION_ARGS)
int i
Definition isn.c:77
int varstr_levenshtein(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, bool trusted)
Definition levenshtein.c:73
void * palloc(Size size)
Definition mcxt.c:1387
void * arg
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition port.h:177
uint64_t Datum
Definition postgres.h:70
char * c
static int fb(int x)
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition regcomp.c:1476
Definition c.h:706
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
text * cstring_to_text(const char *s)
Definition varlena.c:181
char * text_to_cstring(const text *t)
Definition varlena.c:214
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)
const char * name