支援的版本:目前 (17) / 16 / 15 / 14 / 13
開發版本:devel
不支援的版本:12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3

F.16. fuzzystrmatch — 判斷字串相似度和距離 #

fuzzystrmatch 模組提供了幾個函數來判斷字串之間的相似性和距離。

注意

目前,soundexmetaphonedmetaphonedmetaphone_alt 函數在多位元組編碼(例如 UTF-8)下無法正常運作。請使用 daitch_mokotofflevenshtein 處理此類資料。

此模組被視為信任,也就是說,在目前的資料庫上擁有 CREATE 權限的非超級使用者也可以安裝。

F.16.1. Soundex #

Soundex 系統是一種藉由將發音相似的名稱轉換為相同代碼來進行比對的方法。它最初由美國人口普查局在 1880 年、1900 年和 1910 年使用。請注意,Soundex 對於非英語名稱來說不是很有用。

fuzzystrmatch 模組提供了兩個函數來處理 Soundex 代碼

soundex(text) returns text
difference(text, text) returns int

soundex 函數會將字串轉換為其 Soundex 代碼。difference 函數會將兩個字串轉換為其 Soundex 代碼,然後報告比對的代碼位置數。由於 Soundex 代碼有四個字元,因此結果範圍從零到四,零表示不比對,四表示完全比對。(因此,此函數名稱不佳 — similarity 可能會是更好的名稱。)

以下是一些使用範例

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.16.2. Daitch-Mokotoff Soundex #

與原始 Soundex 系統一樣,Daitch-Mokotoff Soundex 藉由將發音相似的名稱轉換為相同代碼來進行比對。但是,Daitch-Mokotoff Soundex 比原始系統對於非英語名稱更有用。與原始系統相比,主要改進包括

  • 該代碼基於前六個有意義的字母,而不是四個字母。

  • 一個字母或字母組合對應到十個可能的代碼,而不是七個。

  • 如果兩個連續字母具有單一發音,它們會被編碼為單一數字。

  • 當一個字母或字母組合可能具有不同的發音時,會發出多個代碼以涵蓋所有可能性。

此函數會為其輸入產生 Daitch-Mokotoff soundex 代碼

daitch_mokotoff(source text) returns text[]

結果可能包含一個或多個代碼,具體取決於有多少種合理的發音,因此它表示為一個陣列。

由於 Daitch-Mokotoff soundex 代碼僅由 6 個數字組成,因此 source 最好是單一單字或名稱。

以下是一些範例

SELECT daitch_mokotoff('George');
 daitch_mokotoff
-----------------
 {595000}

SELECT daitch_mokotoff('John');
 daitch_mokotoff
-----------------
 {160000,460000}

SELECT daitch_mokotoff('Bierschbach');
                      daitch_mokotoff
-----------------------------------------------------------
 {794575,794574,794750,794740,745750,745740,747500,747400}

SELECT daitch_mokotoff('Schwartzenegger');
 daitch_mokotoff
-----------------
 {479465}

對於單一名稱的比對,可以使用 && 運算符直接比對傳回的文字陣列:任何重疊都可以視為比對。GIN 索引可以用於提高效率,請參閱 第 64.4 節 和此範例

CREATE TABLE s (nm text);
CREATE INDEX ix_s_dm ON s USING gin (daitch_mokotoff(nm)) WITH (fastupdate = off);

INSERT INTO s (nm) VALUES
  ('Schwartzenegger'),
  ('John'),
  ('James'),
  ('Steinman'),
  ('Steinmetz');

SELECT * FROM s WHERE daitch_mokotoff(nm) && daitch_mokotoff('Swartzenegger');
SELECT * FROM s WHERE daitch_mokotoff(nm) && daitch_mokotoff('Jane');
SELECT * FROM s WHERE daitch_mokotoff(nm) && daitch_mokotoff('Jens');

對於以任何順序索引和比對任意數量的名稱,可以使用全文檢索功能。請參閱 第 12 章 和此範例

CREATE FUNCTION soundex_tsvector(v_name text) RETURNS tsvector
BEGIN ATOMIC
  SELECT to_tsvector('simple',
                     string_agg(array_to_string(daitch_mokotoff(n), ' '), ' '))
  FROM regexp_split_to_table(v_name, '\s+') AS n;
END;

CREATE FUNCTION soundex_tsquery(v_name text) RETURNS tsquery
BEGIN ATOMIC
  SELECT string_agg('(' || array_to_string(daitch_mokotoff(n), '|') || ')', '&')::tsquery
  FROM regexp_split_to_table(v_name, '\s+') AS n;
END;

CREATE TABLE s (nm text);
CREATE INDEX ix_s_txt ON s USING gin (soundex_tsvector(nm)) WITH (fastupdate = off);

INSERT INTO s (nm) VALUES
  ('John Doe'),
  ('Jane Roe'),
  ('Public John Q.'),
  ('George Best'),
  ('John Yamson');

SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('john');
SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('jane doe');
SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('john public');
SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('besst, giorgio');
SELECT * FROM s WHERE soundex_tsvector(nm) @@ soundex_tsquery('Jameson John');

如果希望避免在索引重新檢查期間重新計算 soundex 代碼,則可以使用單獨欄位上的索引,而不是在運算式上的索引。可以使用儲存的產生欄位來實現;請參閱 第 5.4 節

F.16.3. Levenshtein #

此函數計算兩個字串之間的 Levenshtein 距離

levenshtein(source text, target text, ins_cost int, del_cost int, sub_cost int) returns int
levenshtein(source text, target text) returns int
levenshtein_less_equal(source text, target text, ins_cost int, del_cost int, sub_cost int, max_d int) returns int
levenshtein_less_equal(source text, target text, max_d int) returns int

sourcetarget 都可以是任何非空字串,最大長度為 255 個字元。成本參數分別指定插入、刪除或替換字元的成本。您可以省略成本參數,如該函數的第二個版本所示;在這種情況下,它們都預設為 1。

levenshtein_less_equal 是 Levenshtein 函數的加速版本,用於僅對小距離感興趣的情況。如果實際距離小於或等於 max_d,則 levenshtein_less_equal 會傳回正確的距離;否則,它會傳回大於 max_d 的某個值。如果 max_d 為負數,則行為與 levenshtein 相同。

範例

test=# SELECT levenshtein('GUMBO', 'GAMBOL');
 levenshtein
-------------
           2
(1 row)

test=# SELECT levenshtein('GUMBO', 'GAMBOL', 2, 1, 1);
 levenshtein
-------------
           3
(1 row)

test=# SELECT levenshtein_less_equal('extensive', 'exhaustive', 2);
 levenshtein_less_equal
------------------------
                      3
(1 row)

test=# SELECT levenshtein_less_equal('extensive', 'exhaustive', 4);
 levenshtein_less_equal
------------------------
                      4
(1 row)

F.16.4. Metaphone #

Metaphone,就像 Soundex 一樣,基於為輸入字串建構一個代表性代碼的概念。如果兩個字串具有相同的代碼,則它們被認為是相似的。

此函數計算輸入字串的 Metaphone 代碼

metaphone(source text, max_output_length int) returns text

source 必須是一個非空字串,最大長度為 255 個字元。 max_output_length 設定輸出 Metaphone 代碼的最大長度;如果更長,則輸出將被截斷到此長度。

範例

test=# SELECT metaphone('GUMBO', 4);
 metaphone
-----------
 KM
(1 row)

F.16.5. Double Metaphone #

Double Metaphone 系統為給定的輸入字串計算兩個聽起來像字串——一個主要和一個替代。在大多數情況下,它們是相同的,但對於非英語名稱,尤其是在發音方面,它們可能會有所不同。這些函數計算主要和替代代碼

dmetaphone(source text) returns text
dmetaphone_alt(source text) returns text

輸入字串沒有長度限制。

範例

test=# SELECT dmetaphone('gumbo');
 dmetaphone
------------
 KMP
(1 row)

提交更正

如果您在文件中發現任何不正確、與您使用特定功能的經驗不符或需要進一步澄清的地方,請使用此表單報告文件問題。