# LevenshteinDist - 指令碼與圖表函數

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

語法：

LevenshteinDist(text1, text2)

傳回的資料類型： 整數

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

#### 範例：圖表運算式

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

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

#### 圖表運算式

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的索引標籤。

載入指令碼包含：

* 

* 資料表格中稱為 `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'。

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

#### 圖表運算式

概述

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

開啟資料載入編輯器並將下面的載入指令碼新增至新的索引標籤。

載入指令碼包含：

* 

* 

  * `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)

#### 範例：載入指令碼

載入指令碼

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

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

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

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

set nullinterpret = '<NULL>';
T1:
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 T1;

Drop table T1;
```

結果

| 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 |