# F.15.模糊格式
F.15.1.SoundexF.15.2.莱文斯坦F.15.3.变音F.15.4.双变音
这个模糊格式
模块提供了几个函数来确定字符串之间的相似性和距离。
# 小心
目前soundex
,变音
,数字电话
和dmetaphone_alt
函数不能很好地与多字节编码(如UTF-8)配合使用。
该模块被认为是“受信任的”,也就是说,它可以由拥有创造
当前数据库的权限。
# F.15.1.Soundex
Soundex系统是一种通过将相似的发音名称转换为相同代码来匹配它们的方法。它最初在1880年、1900年和1910年的美国人口普查中使用。请注意,Soundex对于非英语名称不是很有用。
这个模糊格式
模块提供两个使用Soundex代码的功能:
soundex(text) returns text
difference(text, text) returns int
这个soundex
函数将字符串转换为其Soundex代码。这个差别
函数将两个字符串转换为它们的Soundex代码,然后报告匹配代码位置的数量。由于Soundex代码有四个字符,因此结果的范围从零到四,零表示不匹配,四表示完全匹配。(因此,该函数被错误命名为-相似性
这是一个更好的名字。)
以下是一些用法示例:
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);
INSERT INTO s VALUES ('john');
INSERT INTO s VALUES ('joan');
INSERT INTO s VALUES ('wobbly');
INSERT INTO s VALUES ('jack');
SELECT * FROM s WHERE soundex(nm) = soundex('john');
SELECT * FROM s WHERE difference(s.nm, 'john') > 2;
# F.15.2.莱文施坦
此函数用于计算两个字符串之间的Levenshtein距离:
levenshtein(text source, text target, int ins_cost, int del_cost, int sub_cost) returns int
levenshtein(text source, text target) returns int
levenshtein_less_equal(text source, text target, int ins_cost, int del_cost, int sub_cost, int max_d) returns int
levenshtein_less_equal(text source, text target, int max_d) returns int
二者都来源
和目标
可以是任何非空字符串,最多255个字符。成本参数分别指定字符插入、删除或替换的费用。您可以省略成本参数,就像在函数的第二个版本中一样;在这种情况下,它们都默认为1.
levenshtein_less_equal
是Levenshtein函数的一个加速版本,仅适用于感兴趣的小距离。如果实际距离小于或等于麦克斯
然后levenshtein_less_equal
返回正确的距离;否则,它将返回大于麦克斯
如果麦克斯
如果是负面的,那么行为与莱文施坦
.
例如:
test=# SELECT levenshtein('GUMBO', 'GAMBOL');
levenshtein
### F.15.3. Metaphone
Metaphone, like Soundex, is based on the idea of constructing a representative code for an input string. Two strings are then deemed similar if they have the same codes.
This function calculates the metaphone code of an input string:
[]()
变音(文本源,int max_output_length)返回文本
`source` has to be a non-null string with a maximum of 255 characters. `max_output_length` sets the maximum length of the output metaphone code; if longer, the output is truncated to this length.
Example:
测试=#选择变音('GUMBO',4);变音
# F.15.4.双变音
双变音系统为给定的输入字符串计算两个“听起来像”的字符串——“主”和“备用”。在大多数情况下,它们是相同的,但对于非英语名称,它们可能会有点不同,这取决于发音。这些函数计算主代码和备用代码:
dmetaphone(text source) returns text
dmetaphone_alt(text source) returns text
输入字符串没有长度限制。
例子:
test=# SELECT dmetaphone('gumbo');
dmetaphone