# LevenshteinDist 指令碼與圖表函數

LevenshteinDist() 傳回兩個字串之間的 Levenshtein 距離。這定義為將一個字串變更為另一個字串所需的最小單一字元編輯數量 (插入、刪除或替代)。 該函數對於模糊字串比較很實用。

語法：

LevenshteinDist(text1, text2)

傳回的資料類型： 整數

引數

| 引數 | 描述 |
| --- | --- |
| text1 | 第一個字串。 |
| text2 | 第二個字串將與第一個字串進行比較，以計算單字元編輯的最小數量。 |

範例：圖表運算式

| 範例 | 結果 |
| --- | --- |
| `LevenshteinDist( 'Kitten','Sitting' )` | 傳回 `3` |

## 範例 - LevenshteinDist 基礎事項

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/LevenshteinDist.htm)

#### 圖表運算式

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為 `Example` 之資料表格的資料集。

* 資料表格中稱為 `InputText` 的一個欄位。

載入指令碼

```
Example:
Load * inline [
InputText
Sliver
SSiver
SSiveer
];
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* `InputText`

建立下列量值：

* `=LevenshteinDist('Silver', InputText)`，用來計算將 `InputText` 的字串值變更為字詞 `Silver` 所需的最小單一字元編輯次數。

結果表格

| InputText | LevenshteinDist('Silver', InputText) |
| --- | --- |
| Sliver | 2 |
| SSiveer | 3 |
| SSiver | 2 |

`LevenshteinDist` 函數的輸出會傳回將 `InputText` 變更為預期文字 `Silver` 所需的變更次數。例如，第一列需要兩次變更，才能將字詞 `Sliver` 修改為 `Silver`。第二列需要 3 次變更： 1) 刪除多餘的字元 `S`。2) 刪除多餘的字元 `e`。3) 插入新字元 `l`。

## 範例 - LevenshteinDist 使用情境

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/LevenshteinDist.htm)

#### 圖表運算式

概述

此範例合併了來自不同系統的產品名稱。由於拼字錯誤、縮寫、空格或其他變化，產品名稱不一定使用相同的拼寫。使用 `LevenshteinDist` 函數，您可以衡量兩個產品名稱之間的相似性，並識別哪些名稱即使不相同，仍可能指的是相同產品。

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為 `Example` 之資料表格的資料集。

* 資料表格中的欄位如下：

  * `ProductA`

  * `ProductB`

載入指令碼

```
Example:
Load * inline [
ProductA, ProductB
Coca Cola 330ml, CocaCola 330 ml
Pepsi 500 ml, Pepsi 500ml
Sprite Zero 600 ml, SpriteZero600ml
Red Bull 250ml, Redbull 250ml
];
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `ProductA`

* `ProductB`

建立下列量值：

* `=LevenshteinDist(ProductA, ProductB)`，用來計算變更 `ProductB` 的字串值以符合 `ProductA` 所需的最小單一字元編輯次數。

結果表格

| ProductA | ProductB | LevenshteinDist(ProductA, ProductB) |
| --- | --- | --- |
| Coca Cola 330ml | CocaCola 330 ml | 2 |
| Pepsi 500 ml | Pepsi 500ml | 1 |
| Red Bull 250ml | Redbull 250ml | 2 |
| Sprite Zero 600 ml | SpriteZero600ml | 3 |

Levenshtein 距離是一種模糊比對，廣泛用作拼字檢查器、光學字元辨識和校正系統的一部分，適用於客戶資料管理、庫存系統和文件處理等領域，這些領域的文字經常出現些微的變化。

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/LevenshteinDist.htm)

#### 載入指令碼

載入指令碼

```
Example:
Load *, recno() as ID;
Load 'Silver' as String_1,* inline [
String_2
Sliver
SSiver
SSiveer ];

Example:
Load *, recno()+3 as ID;
Load 'Gold' as String_1,* inline [
String_2
Bold
Bool
Bond ];

Example:
Load *, recno()+6 as ID;
Load 'Ove' as String_1,* inline [
String_2
Ove
Uve
Üve ];

Example:
Load *, recno()+9 as ID;
Load 'ABC' as String_1,* inline [
String_2
DEFG
abc
ビビビ ];

set nullinterpret = '<NULL>';
Example:
Load *, recno()+12 as ID;
Load 'X' as String_1,* inline [
String_2
''
<NULL>
1 ];

R1:
Load
ID,
String_1,
String_2,
LevenshteinDist(String_1, String_2) as LevenshteinDistance
resident Example;

Drop table Example;
```

結果表格

| ID | String\_1 | String\_2 | LevenshteinDistance |
| --- | --- | --- | --- |
| 1 | Silver | Sliver | 2 |
| 2 | Silver | SSiver | 2 |
| 3 | Silver | SSiveer | 3 |
| 4 | Gold | Bold | 1 |
| 5 | Gold | Bool | 3 |
| 6 | Gold | Bond | 2 |
| 7 | Ove | Ove | 0 |
| 8 | Ove | Uve | 1 |
| 9 | Ove | Üve | 1 |
| 10 | ABC | DEFG | 4 |
| 11 | ABC | abc | 3 |
| 12 | ABC | ビビビ | 3 |
| 13 | X |  | 1 |
| 14 | X | - | 1 |
| 15 | X | 1 | 1 |