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
* dmetaphone() and dmetaphone_alt()
27
* ---------------------------------
28
* A port of the DoubleMetaphone perl module by Andrew Dunstan. See dmetaphone.c
33
* Folded existing soundex contrib into this one. Renamed text_soundex() (C function)
34
* to soundex() for consistency.
36
* Permission to use, copy, modify, and distribute this software and its
37
* documentation for any purpose, without fee, and without a written agreement
38
* is hereby granted, provided that the above copyright notice and this
39
* paragraph and the following two paragraphs appear in all copies.
41
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
42
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
43
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
44
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
45
* POSSIBILITY OF SUCH DAMAGE.
47
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
48
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
49
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
50
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
51
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
56
Version 0.3 (30 June, 2004):
60
- added double metaphone code from Andrew Dunstan
61
- change metaphone so that an empty input string causes an empty
62
output string to be returned, instead of throwing an ERROR
63
- fixed examples in README.soundex
66
- folded soundex contrib into this one
72
Place these files in a directory called 'fuzzystrmatch' under 'contrib' in the PostgreSQL source tree. Then run:
77
You can use fuzzystrmatch.sql to create the functions in your database of choice, e.g.
79
psql -U postgres template1 < fuzzystrmatch.sql
81
installs following functions into database template1:
83
levenshtein() - calculates the levenshtein distance between two strings
84
metaphone() - calculates the metaphone code of an input string
87
==================================================================
90
levenshtein -- calculates the levenshtein distance between two strings
94
levenshtein(text source, text target)
99
any text string, 255 characters max, NOT NULL
102
any text string, 255 characters max, NOT NULL
110
select levenshtein('GUMBO','GAMBOL');
112
==================================================================
115
metaphone -- calculates the metaphone code of an input string
119
metaphone(text source, int max_output_length)
124
any text string, 255 characters max, NOT NULL
127
maximum length of the output metaphone code; if longer, the output
128
is truncated to this length
136
select metaphone('GUMBO',4);
138
==================================================================