1 <!-- doc/src/sgml/fuzzystrmatch.sgml -->
3 <sect1 id=
"fuzzystrmatch" xreflabel=
"fuzzystrmatch">
4 <title>fuzzystrmatch
— determine string similarities and distance
</title>
6 <indexterm zone=
"fuzzystrmatch">
7 <primary>fuzzystrmatch
</primary>
11 The
<filename>fuzzystrmatch
</filename> module provides several
12 functions to determine similarities and distance between strings.
17 At present, the
<function>soundex
</function>,
<function>metaphone
</function>,
18 <function>dmetaphone
</function>, and
<function>dmetaphone_alt
</function> functions do
19 not work well with multibyte encodings (such as UTF-
8).
20 Use
<function>daitch_mokotoff
</function>
21 or
<function>levenshtein
</function> with such data.
26 This module is considered
<quote>trusted
</quote>, that is, it can be
27 installed by non-superusers who have
<literal>CREATE
</literal> privilege
28 on the current database.
31 <sect2 id=
"fuzzystrmatch-soundex">
32 <title>Soundex
</title>
35 The Soundex system is a method of matching similar-sounding names
36 by converting them to the same code. It was initially used by the
37 United States Census in
1880,
1900, and
1910. Note that Soundex
38 is not very useful for non-English names.
42 The
<filename>fuzzystrmatch
</filename> module provides two functions
43 for working with Soundex codes:
47 <primary>soundex
</primary>
51 <primary>difference
</primary>
55 soundex(text) returns text
56 difference(text, text) returns int
60 The
<function>soundex
</function> function converts a string to its Soundex code.
61 The
<function>difference
</function> function converts two strings to their Soundex
62 codes and then reports the number of matching code positions. Since
63 Soundex codes have four characters, the result ranges from zero to four,
64 with zero being no match and four being an exact match. (Thus, the
65 function is misnamed
— <function>similarity
</function> would have been
70 Here are some usage examples:
74 SELECT soundex('hello world!');
76 SELECT soundex('Anne'), soundex('Ann'), difference('Anne', 'Ann');
77 SELECT soundex('Anne'), soundex('Andrew'), difference('Anne', 'Andrew');
78 SELECT soundex('Anne'), soundex('Margaret'), difference('Anne', 'Margaret');
80 CREATE TABLE s (nm text);
82 INSERT INTO s VALUES ('john');
83 INSERT INTO s VALUES ('joan');
84 INSERT INTO s VALUES ('wobbly');
85 INSERT INTO s VALUES ('jack');
87 SELECT * FROM s WHERE soundex(nm) = soundex('john');
89 SELECT * FROM s WHERE difference(s.nm, 'john')
> 2;
93 <sect2 id=
"fuzzystrmatch-daitch-mokotoff">
94 <title>Daitch-Mokotoff Soundex
</title>
97 Like the original Soundex system, Daitch-Mokotoff Soundex matches
98 similar-sounding names by converting them to the same code.
99 However, Daitch-Mokotoff Soundex is significantly more useful for
100 non-English names than the original system.
101 Major improvements over the original system include:
103 <itemizedlist spacing=
"compact" mark=
"bullet">
106 The code is based on the first six meaningful letters rather than four.
111 A letter or combination of letters maps into ten possible codes rather
117 Where two consecutive letters have a single sound, they are coded as a
123 When a letter or combination of letters may have different sounds,
124 multiple codes are emitted to cover all possibilities.
131 <primary>daitch_mokotoff
</primary>
135 This function generates the Daitch-Mokotoff soundex codes for its input:
139 daitch_mokotoff(
<parameter>source
</parameter> text) returns text[]
143 The result may contain one or more codes depending on how many plausible
144 pronunciations there are, so it is represented as an array.
148 Since a Daitch-Mokotoff soundex code consists of only
6 digits,
149 <parameter>source
</parameter> should be preferably a single word or name.
153 Here are some examples:
157 SELECT daitch_mokotoff('George');
162 SELECT daitch_mokotoff('John');
167 SELECT daitch_mokotoff('Bierschbach');
169 -----------------------------------------------------------
170 {
794575,
794574,
794750,
794740,
745750,
745740,
747500,
747400}
172 SELECT daitch_mokotoff('Schwartzenegger');
179 For matching of single names, returned text arrays can be matched
180 directly using the
<literal>&&</literal> operator: any overlap
181 can be considered a match. A GIN index may
182 be used for efficiency, see
<xref linkend=
"gin"/> and this example:
186 CREATE TABLE s (nm text);
187 CREATE INDEX ix_s_dm ON s USING gin (daitch_mokotoff(nm)) WITH (fastupdate = off);
189 INSERT INTO s (nm) VALUES
196 SELECT * FROM s WHERE daitch_mokotoff(nm)
&& daitch_mokotoff('Swartzenegger');
197 SELECT * FROM s WHERE daitch_mokotoff(nm)
&& daitch_mokotoff('Jane');
198 SELECT * FROM s WHERE daitch_mokotoff(nm)
&& daitch_mokotoff('Jens');
202 For indexing and matching of any number of names in any order, Full Text
203 Search features can be used. See
<xref linkend=
"textsearch"/> and this
208 CREATE FUNCTION soundex_tsvector(v_name text) RETURNS tsvector
210 SELECT to_tsvector('simple',
211 string_agg(array_to_string(daitch_mokotoff(n), ' '), ' '))
212 FROM regexp_split_to_table(v_name, '\s+') AS n;
215 CREATE FUNCTION soundex_tsquery(v_name text) RETURNS tsquery
217 SELECT string_agg('(' || array_to_string(daitch_mokotoff(n), '|') || ')', '
&')::tsquery
218 FROM regexp_split_to_table(v_name, '\s+') AS n;
221 CREATE TABLE s (nm text);
222 CREATE INDEX ix_s_txt ON s USING gin (soundex_tsvector(nm)) WITH (fastupdate = off);
224 INSERT INTO s (nm) VALUES
231 SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('john');
232 SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('jane doe');
233 SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('john public');
234 SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('besst, giorgio');
235 SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('Jameson John');
239 If it is desired to avoid recalculation of soundex codes during index
240 rechecks, an index on a separate column can be used instead of an index on
241 an expression. A stored generated column can be used for this; see
242 <xref linkend=
"ddl-generated-columns"/>.
246 <sect2 id=
"fuzzystrmatch-levenshtein">
247 <title>Levenshtein
</title>
250 This function calculates the Levenshtein distance between two strings:
254 <primary>levenshtein
</primary>
258 <primary>levenshtein_less_equal
</primary>
262 levenshtein(source text, target text, ins_cost int, del_cost int, sub_cost int) returns int
263 levenshtein(source text, target text) returns int
264 levenshtein_less_equal(source text, target text, ins_cost int, del_cost int, sub_cost int, max_d int) returns int
265 levenshtein_less_equal(source text, target text, max_d int) returns int
269 Both
<literal>source
</literal> and
<literal>target
</literal> can be any
270 non-null string, with a maximum of
255 characters. The cost parameters
271 specify how much to charge for a character insertion, deletion, or
272 substitution, respectively. You can omit the cost parameters, as in
273 the second version of the function; in that case they all default to
1.
277 <function>levenshtein_less_equal
</function> is an accelerated version of the
278 Levenshtein function for use when only small distances are of interest.
279 If the actual distance is less than or equal to
<literal>max_d
</literal>,
280 then
<function>levenshtein_less_equal
</function> returns the correct
281 distance; otherwise it returns some value greater than
<literal>max_d
</literal>.
282 If
<literal>max_d
</literal> is negative then the behavior is the same as
283 <function>levenshtein
</function>.
291 test=# SELECT levenshtein('GUMBO', 'GAMBOL');
297 test=# SELECT levenshtein('GUMBO', 'GAMBOL',
2,
1,
1);
303 test=# SELECT levenshtein_less_equal('extensive', 'exhaustive',
2);
304 levenshtein_less_equal
305 ------------------------
309 test=# SELECT levenshtein_less_equal('extensive', 'exhaustive',
4);
310 levenshtein_less_equal
311 ------------------------
317 <sect2 id=
"fuzzystrmatch-metaphone">
318 <title>Metaphone
</title>
321 Metaphone, like Soundex, is based on the idea of constructing a
322 representative code for an input string. Two strings are then
323 deemed similar if they have the same codes.
327 This function calculates the metaphone code of an input string:
331 <primary>metaphone
</primary>
335 metaphone(source text, max_output_length int) returns text
339 <literal>source
</literal> has to be a non-null string with a maximum of
340 255 characters.
<literal>max_output_length
</literal> sets the maximum
341 length of the output metaphone code; if longer, the output is truncated
350 test=# SELECT metaphone('GUMBO',
4);
358 <sect2 id=
"fuzzystrmatch-double-metaphone">
359 <title>Double Metaphone
</title>
362 The Double Metaphone system computes two
<quote>sounds like
</quote> strings
363 for a given input string
— a
<quote>primary
</quote> and an
364 <quote>alternate
</quote>. In most cases they are the same, but for non-English
365 names especially they can be a bit different, depending on pronunciation.
366 These functions compute the primary and alternate codes:
370 <primary>dmetaphone
</primary>
374 <primary>dmetaphone_alt
</primary>
378 dmetaphone(source text) returns text
379 dmetaphone_alt(source text) returns text
383 There is no length limit on the input strings.
391 test=# SELECT dmetaphone('gumbo');