mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-26 22:47:19 +02:00
compares two strings' soundex values for similarity, from Kris Jurka. Also mark the text_soundex() function as STRICT, to avoid crashing on NULL input.
78 lines
2.1 KiB
Plaintext
78 lines
2.1 KiB
Plaintext
NOTE: Modified August 07, 2001 by Joe Conway. Updated for accuracy
|
|
after combining soundex code into the fuzzystrmatch contrib
|
|
---------------------------------------------------------------------
|
|
The Soundex system is a method of matching similar sounding names
|
|
(or any words) to the same code. It was initially used by the
|
|
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
|
|
|
|
select a.nm, b.nm from s a, s b
|
|
where soundex(a.nm) = soundex(b.nm)
|
|
and a.oid <> b.oid\g
|
|
|
|
CREATE FUNCTION text_sx_eq(text, text) RETURNS bool AS
|
|
'select soundex($1) = soundex($2)'
|
|
LANGUAGE 'sql'\g
|
|
|
|
CREATE FUNCTION text_sx_lt(text,text) RETURNS bool AS
|
|
'select soundex($1) < soundex($2)'
|
|
LANGUAGE 'sql'\g
|
|
|
|
CREATE FUNCTION text_sx_gt(text,text) RETURNS bool AS
|
|
'select soundex($1) > soundex($2)'
|
|
LANGUAGE 'sql';
|
|
|
|
CREATE FUNCTION text_sx_le(text,text) RETURNS bool AS
|
|
'select soundex($1) <= soundex($2)'
|
|
LANGUAGE 'sql';
|
|
|
|
CREATE FUNCTION text_sx_ge(text,text) RETURNS bool AS
|
|
'select soundex($1) >= soundex($2)'
|
|
LANGUAGE 'sql';
|
|
|
|
CREATE FUNCTION text_sx_ne(text,text) RETURNS bool AS
|
|
'select soundex($1) <> soundex($2)'
|
|
LANGUAGE 'sql';
|
|
|
|
DROP OPERATOR #= (text,text)\g
|
|
|
|
CREATE OPERATOR #= (leftarg=text, rightarg=text, procedure=text_sx_eq,
|
|
commutator = #=)\g
|
|
|
|
SELECT *
|
|
FROM s
|
|
WHERE text_sx_eq(nm,'john')\g
|
|
|
|
SELECT *
|
|
FROM s
|
|
WHERE s.nm #= 'john';
|
|
|
|
SELECT *
|
|
FROM s
|
|
WHERE difference(s.nm, 'john') > 2;
|
|
|