2001-08-08 00:47:43 +08:00
|
|
|
/*
|
|
|
|
* fuzzystrmatch.h
|
|
|
|
*
|
|
|
|
* Functions for "fuzzy" comparison of strings
|
|
|
|
*
|
Jim C. Nasby wrote:
> Second argument to metaphone is suposed to set the limit on the
> number of characters to return, but it breaks on some phrases:
>
> usps=# select metaphone(a,3),metaphone(a,4),metaphone(a,20) from
> (select 'Hello world'::varchar AS a) a;
> HLW | HLWR | HLWRLT
>
> usps=# select metaphone(a,3),metaphone(a,4),metaphone(a,20) from
> (select 'A A COMEAUX MEMORIAL'::varchar AS a) a;
> AKM | AKMKS | AKMKSMMRL
>
> In every case I've found that does this, the 4th and 5th letters are
> always 'KS'.
Nice catch.
There was a bug in the original metaphone algorithm from CPAN. Patch
attached (while I was at it I updated my email address, changed the
copyright to PGDG, and removed an unnecessary palloc). Here's how it
looks now:
regression=# select metaphone(a,4) from (select 'A A COMEAUX
MEMORIAL'::varchar AS a) a;
metaphone
-----------
AKMK
(1 row)
regression=# select metaphone(a,5) from (select 'A A COMEAUX
MEMORIAL'::varchar AS a) a;
metaphone
-----------
AKMKS
(1 row)
Joe Conway
2003-06-25 06:59:46 +08:00
|
|
|
* Joe Conway <mail@joeconway.com>
|
|
|
|
*
|
2004-08-29 12:13:13 +08:00
|
|
|
* Copyright (c) 2001-2004, PostgreSQL Global Development Group
|
Jim C. Nasby wrote:
> Second argument to metaphone is suposed to set the limit on the
> number of characters to return, but it breaks on some phrases:
>
> usps=# select metaphone(a,3),metaphone(a,4),metaphone(a,20) from
> (select 'Hello world'::varchar AS a) a;
> HLW | HLWR | HLWRLT
>
> usps=# select metaphone(a,3),metaphone(a,4),metaphone(a,20) from
> (select 'A A COMEAUX MEMORIAL'::varchar AS a) a;
> AKM | AKMKS | AKMKSMMRL
>
> In every case I've found that does this, the 4th and 5th letters are
> always 'KS'.
Nice catch.
There was a bug in the original metaphone algorithm from CPAN. Patch
attached (while I was at it I updated my email address, changed the
copyright to PGDG, and removed an unnecessary palloc). Here's how it
looks now:
regression=# select metaphone(a,4) from (select 'A A COMEAUX
MEMORIAL'::varchar AS a) a;
metaphone
-----------
AKMK
(1 row)
regression=# select metaphone(a,5) from (select 'A A COMEAUX
MEMORIAL'::varchar AS a) a;
metaphone
-----------
AKMKS
(1 row)
Joe Conway
2003-06-25 06:59:46 +08:00
|
|
|
* ALL RIGHTS RESERVED;
|
2001-08-08 00:47:43 +08:00
|
|
|
*
|
|
|
|
* levenshtein()
|
|
|
|
* -------------
|
|
|
|
* Written based on a description of the algorithm by Michael Gilleland
|
|
|
|
* found at http://www.merriampark.com/ld.htm
|
|
|
|
* Also looked at levenshtein.c in the PHP 4.0.6 distribution for
|
|
|
|
* inspiration.
|
|
|
|
*
|
|
|
|
* metaphone()
|
|
|
|
* -----------
|
|
|
|
* Modified for PostgreSQL by Joe Conway.
|
|
|
|
* Based on CPAN's "Text-Metaphone-1.96" by Michael G Schwern <schwern@pobox.com>
|
|
|
|
* Code slightly modified for use as PostgreSQL function (palloc, elog, etc).
|
|
|
|
* Metaphone was originally created by Lawrence Philips and presented in article
|
|
|
|
* in "Computer Language" December 1990 issue.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and its
|
|
|
|
* documentation for any purpose, without fee, and without a written agreement
|
|
|
|
* is hereby granted, provided that the above copyright notice and this
|
|
|
|
* paragraph and the following two paragraphs appear in all copies.
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-08-08 00:47:43 +08:00
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|
|
|
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|
|
|
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-08-08 00:47:43 +08:00
|
|
|
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FUZZYSTRMATCH_H
|
|
|
|
#define FUZZYSTRMATCH_H
|
|
|
|
|
2002-09-05 08:43:07 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2001-08-08 00:47:43 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* External declarations
|
|
|
|
*/
|
|
|
|
extern Datum levenshtein(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum metaphone(PG_FUNCTION_ARGS);
|
2001-08-08 02:16:01 +08:00
|
|
|
extern Datum soundex(PG_FUNCTION_ARGS);
|
2001-08-08 00:47:43 +08:00
|
|
|
|
|
|
|
/*
|
2001-08-08 02:16:01 +08:00
|
|
|
* Soundex
|
2001-08-08 00:47:43 +08:00
|
|
|
*/
|
2001-08-08 02:16:01 +08:00
|
|
|
static void _soundex(const char *instr, char *outstr);
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-08-08 02:16:01 +08:00
|
|
|
#define SOUNDEX_LEN 4
|
|
|
|
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
|
|
|
|
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-08-08 02:16:01 +08:00
|
|
|
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
|
|
|
static const char *soundex_table = "01230120022455012623010202";
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-08-08 02:16:01 +08:00
|
|
|
#define soundex_code(letter) soundex_table[toupper((unsigned char) (letter)) - 'A']
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Levenshtein
|
|
|
|
*/
|
2001-08-08 00:47:43 +08:00
|
|
|
#define STRLEN(p) strlen(p)
|
|
|
|
#define CHAREQ(p1, p2) (*(p1) == *(p2))
|
|
|
|
#define NextChar(p) ((p)++)
|
2001-08-08 02:16:01 +08:00
|
|
|
#define MAX_LEVENSHTEIN_STRLEN 255
|
|
|
|
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-08-08 02:16:01 +08:00
|
|
|
/*
|
|
|
|
* Metaphone
|
|
|
|
*/
|
|
|
|
#define MAX_METAPHONE_STRLEN 255
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/*
|
2001-08-08 00:47:43 +08:00
|
|
|
* Original code by Michael G Schwern starts here.
|
|
|
|
* Code slightly modified for use as PostgreSQL
|
|
|
|
* function (combined *.h into here).
|
|
|
|
*------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
metaphone -- Breaks english phrases down into their phonemes.
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-08-08 00:47:43 +08:00
|
|
|
Input
|
2001-10-25 13:50:21 +08:00
|
|
|
word -- An english word to be phonized
|
|
|
|
max_phonemes -- How many phonemes to calculate. If 0, then it
|
2001-08-08 00:47:43 +08:00
|
|
|
will phonize the entire phrase.
|
2001-10-25 13:50:21 +08:00
|
|
|
phoned_word -- The final phonized word. (We'll allocate the
|
2001-08-08 00:47:43 +08:00
|
|
|
memory.)
|
|
|
|
Output
|
|
|
|
error -- A simple error flag, returns TRUE or FALSE
|
2001-10-25 13:50:21 +08:00
|
|
|
|
|
|
|
NOTES: ALL non-alpha characters are ignored, this includes whitespace,
|
2001-08-08 00:47:43 +08:00
|
|
|
although non-alpha characters will break up phonemes.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
my constants -- constants I like
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-08-08 00:47:43 +08:00
|
|
|
Probably redundant.
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-08-08 00:47:43 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define META_ERROR FALSE
|
|
|
|
#define META_SUCCESS TRUE
|
|
|
|
#define META_FAILURE FALSE
|
|
|
|
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/* I add modifications to the traditional metaphone algorithm that you
|
2001-08-08 00:47:43 +08:00
|
|
|
might find in books. Define this if you want metaphone to behave
|
|
|
|
traditionally */
|
|
|
|
#undef USE_TRADITIONAL_METAPHONE
|
|
|
|
|
|
|
|
/* Special encodings */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define SH 'X'
|
2001-08-08 00:47:43 +08:00
|
|
|
#define TH '0'
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
char Lookahead(char *word, int how_far);
|
|
|
|
int
|
|
|
|
_metaphone(
|
|
|
|
/* IN */
|
|
|
|
char *word,
|
|
|
|
int max_phonemes,
|
|
|
|
/* OUT */
|
|
|
|
char **phoned_word
|
2001-08-08 00:47:43 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Metachar.h ... little bits about characters for metaphone */
|
|
|
|
|
|
|
|
|
|
|
|
/*-- Character encoding array & accessing macros --*/
|
|
|
|
/* Stolen directly out of the book... */
|
2001-10-25 13:50:21 +08:00
|
|
|
char _codes[26] = {
|
|
|
|
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
|
|
|
|
/* 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 */
|
2001-08-08 00:47:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-12-31 07:09:42 +08:00
|
|
|
#define ENCODE(c) (isalpha((unsigned char) (c)) ? _codes[((toupper((unsigned char) (c))) - 'A')] : 0)
|
2001-08-08 00:47:43 +08:00
|
|
|
|
|
|
|
#define isvowel(c) (ENCODE(c) & 1) /* AEIOU */
|
|
|
|
|
|
|
|
/* These letters are passed through unchanged */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define NOCHANGE(c) (ENCODE(c) & 2) /* FJMNR */
|
2001-08-08 00:47:43 +08:00
|
|
|
|
|
|
|
/* These form dipthongs when preceding H */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define AFFECTH(c) (ENCODE(c) & 4) /* CGPST */
|
2001-08-08 00:47:43 +08:00
|
|
|
|
|
|
|
/* These make C and G soft */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define MAKESOFT(c) (ENCODE(c) & 8) /* EIY */
|
2001-08-08 00:47:43 +08:00
|
|
|
|
|
|
|
/* These prevent GH from becoming F */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define NOGHTOF(c) (ENCODE(c) & 16) /* BDH */
|
2001-08-08 00:47:43 +08:00
|
|
|
|
2001-11-06 01:46:40 +08:00
|
|
|
#endif /* FUZZYSTRMATCH_H */
|