From 1ac9f0e9f79c02a48c374dee206d3952e5520f84 Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Wed, 26 Jan 2005 08:04:04 +0000 Subject: [PATCH] The attached patch implements the soundex difference function which compares two strings' soundex values for similarity, from Kris Jurka. Also mark the text_soundex() function as STRICT, to avoid crashing on NULL input. --- contrib/fuzzystrmatch/README.fuzzystrmatch | 4 ++++ contrib/fuzzystrmatch/README.soundex | 19 ++++++++++++++-- contrib/fuzzystrmatch/fuzzystrmatch.c | 20 +++++++++++++++++ contrib/fuzzystrmatch/fuzzystrmatch.h | 1 + contrib/fuzzystrmatch/fuzzystrmatch.sql.in | 26 ++++++++++++---------- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/contrib/fuzzystrmatch/README.fuzzystrmatch b/contrib/fuzzystrmatch/README.fuzzystrmatch index 876bdef284..ad2d12c388 100644 --- a/contrib/fuzzystrmatch/README.fuzzystrmatch +++ b/contrib/fuzzystrmatch/README.fuzzystrmatch @@ -33,6 +33,10 @@ * Folded existing soundex contrib into this one. Renamed text_soundex() (C function) * to soundex() for consistency. * + * difference() + * ------------ + * Return the difference between two strings' soundex values. Kris Jurka + * * 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 diff --git a/contrib/fuzzystrmatch/README.soundex b/contrib/fuzzystrmatch/README.soundex index ec4f50fd1d..5a58655cdc 100644 --- a/contrib/fuzzystrmatch/README.soundex +++ b/contrib/fuzzystrmatch/README.soundex @@ -7,15 +7,25 @@ United States Census in 1880, 1900, and 1910, but it has little use beyond English names (or the English pronunciation of names), and it is not a linguistic tool. +When comparing two soundex values to determine similarity, the +difference function reports how close the match is on a scale +from zero to four, with zero being no match and four being an +exact match. + The following are some usage examples: SELECT soundex('hello world!'); +SELECT soundex('Anne'), soundex('Ann'), difference('Anne', 'Ann'); +SELECT soundex('Anne'), soundex('Andrew'), difference('Anne', 'Andrew'); +SELECT soundex('Anne'), soundex('Margaret'), difference('Anne', 'Margaret'); + CREATE TABLE s (nm text)\g insert into s values ('john')\g insert into s values ('joan')\g insert into s values ('wobbly')\g +insert into s values ('jack')\g select * from s where soundex(nm) = soundex('john')\g @@ -58,5 +68,10 @@ FROM s WHERE text_sx_eq(nm,'john')\g SELECT * -from s -where s.nm #= 'john'; +FROM s +WHERE s.nm #= 'john'; + +SELECT * +FROM s +WHERE difference(s.nm, 'john') > 2; + diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.c b/contrib/fuzzystrmatch/fuzzystrmatch.c index d3627097cf..90505a8b2c 100644 --- a/contrib/fuzzystrmatch/fuzzystrmatch.c +++ b/contrib/fuzzystrmatch/fuzzystrmatch.c @@ -755,3 +755,23 @@ _soundex(const char *instr, char *outstr) ++count; } } + +PG_FUNCTION_INFO_V1(difference); + +Datum +difference(PG_FUNCTION_ARGS) +{ + char sndx1[SOUNDEX_LEN+1], sndx2[SOUNDEX_LEN+1]; + int i, result; + + _soundex(_textout(PG_GETARG_TEXT_P(0)), sndx1); + _soundex(_textout(PG_GETARG_TEXT_P(1)), sndx2); + + result = 0; + for (i=0; i