|
| 1 | +This directory contains a module that implements the "Metaphone" code as |
| 2 | +a PostgreSQL user-defined function. The Metaphone system is a method of |
| 3 | +matching similar sounding names (or any words) to the same code. |
| 4 | + |
| 5 | +Metaphone was invented by Lawrence Philips as an improvement to the popular |
| 6 | +name-hashing routine, Soundex. |
| 7 | + |
| 8 | +This metaphone code is from Michael Kuhn, and is detailed at |
| 9 | + http://aspell.sourceforge.net/metaphone/metaphone-kuhn.txt |
| 10 | + |
| 11 | +Code for this (including this help file!) was liberally borrowed from |
| 12 | +the soundex() module for PostgreSQL. |
| 13 | + |
| 14 | +There are two functions: |
| 15 | + metaphone(text) : returns hash of a name |
| 16 | + metaphone(text,int) : returns hash (maximum length of int) of name |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +To install it, first configure the main source tree, then run make; |
| 21 | +make install in this directory. Finally, load the function definition |
| 22 | +with psql: |
| 23 | + |
| 24 | + psql -f PREFIX/share/contrib/metaphone.sql |
| 25 | + |
| 26 | +The following are some usage examples: |
| 27 | + |
| 28 | +SELECT text_metaphone('hello world!'); |
| 29 | +SELECT text_metaphone('hello world!', 4); |
| 30 | + |
| 31 | +CREATE TABLE s (nm text)\g |
| 32 | + |
| 33 | +insert into s values ('john')\g |
| 34 | +insert into s values ('joan')\g |
| 35 | +insert into s values ('wobbly')\g |
| 36 | + |
| 37 | +select * from s |
| 38 | +where text_metaphone(nm) = text_metaphone('john')\g |
| 39 | + |
| 40 | +select nm from s a, s b |
| 41 | +where text_metaphone(a.nm) = text_metaphone(b.nm) |
| 42 | +and a.oid <> b.oid\g |
| 43 | + |
| 44 | +CREATE FUNCTION text_mp_eq(text, text) RETURNS bool AS |
| 45 | +'select text_metaphone($1) = text_metaphone($2)' |
| 46 | +LANGUAGE 'sql'\g |
| 47 | + |
| 48 | +CREATE FUNCTION text_mp_lt(text,text) RETURNS bool AS |
| 49 | +'select text_metaphone($1) < text_metaphone($2)' |
| 50 | +LANGUAGE 'sql'\g |
| 51 | + |
| 52 | +CREATE FUNCTION text_mp_gt(text,text) RETURNS bool AS |
| 53 | +'select text_metaphone($1) > text_metaphone($2)' |
| 54 | +LANGUAGE 'sql'; |
| 55 | + |
| 56 | +CREATE FUNCTION text_mp_le(text,text) RETURNS bool AS |
| 57 | +'select text_metaphone($1) <= text_metaphone($2)' |
| 58 | +LANGUAGE 'sql'; |
| 59 | + |
| 60 | +CREATE FUNCTION text_mp_ge(text,text) RETURNS bool AS |
| 61 | +'select text_metaphone($1) >= text_metaphone($2)' |
| 62 | +LANGUAGE 'sql'; |
| 63 | + |
| 64 | +CREATE FUNCTION text_mp_ne(text,text) RETURNS bool AS |
| 65 | +'select text_metaphone($1) <> text_metaphone($2)' |
| 66 | +LANGUAGE 'sql'; |
| 67 | + |
| 68 | +DROP OPERATOR #= (text,text)\g |
| 69 | + |
| 70 | +CREATE OPERATOR #= (leftarg=text, rightarg=text, procedure=text_mp_eq, |
| 71 | +commutator=text_mp_eq)\g |
| 72 | + |
| 73 | +SELECT * |
| 74 | +FROM s |
| 75 | +WHERE text_mp_eq(nm,'pillsbury')\g |
| 76 | + |
| 77 | +SELECT * |
| 78 | +from s |
| 79 | +where s.nm #= 'pillsbury'; |
0 commit comments