文字列の類似度を比較する

SQL Server データベースでのデータクレンジング

Miriam Antona

Software Engineer

問題の説明

  • 乱れた文字列
| airport_state |
|---------------|
| Caalifornia   |
| California    |
| Californiaa   |
| Illinois      |
| Ilynois       |
| Tejas         |
| Texas         |
SOUNDEX
DIFFERENCE
SQL Server データベースでのデータクレンジング

SOUNDEX

SOUNDEX ( character_expression ) 
  • 発音類似アルゴリズム
  • 4文字のコードを返す
  • 英語ベースだが他言語の多くの語にも有効
SELECT SOUNDEX('Illinois') AS soundex_code1;
SELECT SOUNDEX('Ilynois') AS soundex_code2;
SELECT SOUNDEX('California') AS soundex_code3;
| soundex_code1 |   | soundex_code2 |   | soundex_code3 |
|---------------|   |---------------|   |---------------|
| I452          |   | I452          |   | I416          |
SQL Server データベースでのデータクレンジング

SOUNDEX の仕組み

例: "Illinois"
  • 単語の最初の文字を書く
  • 最初の文字以降の母音と "h"、"w"、"y" を 0 に置換

 

"Illinois" -> I

"Illinois" -> Ill0n00s

SQL Server データベースでのデータクレンジング

SOUNDEX の仕組み

例: "Illinois"
  • 最初の文字以降の子音を置換
SQL Server データベースでのデータクレンジング

SOUNDEX の仕組み

例: "Illinois"
  • 最初の文字以降の子音を置換する
文字 置換後
b, f, p, v 1
c, g, j, k, q, s, x, z 2
d, t 3
l 4
m, n 5
r 6

 

"Ill0n00s" -> I4405002

SQL Server データベースでのデータクレンジング

SOUNDEX の仕組み

  • 連続する同一の数字は1つにする
  • 0 をすべて削除する
  • 文字の数字が先頭の数字と同じ場合、先頭の数字を削除
  • 桁が3未満なら 0 を追加
  • 桁が3より多ければ末尾を削除

"I4405002" -> I40502

"I40502" -> I452

 

"I452"(これ以上は適用しない)

SQL Server データベースでのデータクレンジング

SOUNDEX の例外

SELECT SOUNDEX('Arizona') AS soundex_code1;
SELECT SOUNDEX('Arkansas') AS soundex_code2;
| soundex_code1 |   | soundex_code2 |
|---------------|   |---------------|
| A625          |   | A625          |
SQL Server データベースでのデータクレンジング

SOUNDEX での類似度チェック

SELECT DISTINCT A1.airport_state
FROM airports A1 
INNER JOIN airports A2 
    ON SOUNDEX(A1.airport_state) = SOUNDEX(A2.airport_state)
    AND A1.airport_state <> A2.airport_state
| airport_state |
|---------------|
| Caalifornia   |
| California    |
| Californiaa   |
| Illinois      |
| Ilynois       |
| New Jersey    |
| New York      |
| Tejas         |
| Texas         |
SQL Server データベースでのデータクレンジング

SOUNDEX での類似度チェック

SELECT DISTINCT A1.airport_state
FROM airports A1 
INNER JOIN airports A2 
    ON SOUNDEX(REPLACE(A1.airport_state, ' ', '')) = SOUNDEX(REPLACE(A2.airport_state, ' ', ''))
    AND A1.airport_state <> A2.airport_state

"New York" -> "NewYork"

| airport_state |
|---------------|
| Caalifornia   |
| California    |
| Californiaa   |
| Illinois      |
| Ilynois       |
| Tejas         |
| Texas         |
SQL Server データベースでのデータクレンジング

DIFFERENCE

DIFFERENCE ( character_expression , character_expression )  
  • 2つの SOUNDEX 値を比較
  • 0〜4 を返す
    • 0 -> 類似ほぼなし
    • 4 -> 非常に類似/同一
SQL Server データベースでのデータクレンジング

DIFFERENCE

SELECT DIFFERENCE('Illinois', 'Ilynois') AS dif_1;
| dif1 |
|------|
|  4   |
SELECT DIFFERENCE('Illinois', 'California') AS dif_2;
| dif2 |
|------|
| 1    |
SQL Server データベースでのデータクレンジング

DIFFERENCE での類似度チェック

SELECT DISTINCT A1.airport_state, A2.airport_state
FROM airports A1 
INNER JOIN airports A2 
    ON DIFFERENCE(REPLACE(A1.airport_state, ' ', ''), REPLACE(A2.airport_state, ' ', '')) = 4
    AND A1.airport_state <> A2.airport_state
| airport_state | airport_state |
|---------------|---------------|
| Caalifornia   | California    |
| Caalifornia   | Californiaa   |
| California    | Caalifornia   |
| California    | Californiaa   |
| Californiaa   | Caalifornia   |
| Californiaa   | California    |
| Illinois      | Ilynois       |
| Ilynois       | Illinois      |
| Massachusetts | Michigan      |
| Tejas         | Texas         |
| Texas         | Tejas         |
SQL Server データベースでのデータクレンジング

Ayo berlatih!

SQL Server データベースでのデータクレンジング

Preparing Video For Download...