字符串编辑距离之Damerau–Levenshtein Distance

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/asty9000/article/details/81570627

概述

Damerau–Levenshtein Distance用来测量两个字符序列之间的编辑距离的字符串度量标准。两个词的Damerau–Levenshtein Distance是从一个词转换为另一个词的最少操作数,与Levenshtein Distance不同的是,除了单个字符的插入、删除和变更之外,还包括两个相邻字符的转换。

定义

对于两个字符串ab,函数d_{a,b}(i,j)表示a的前i个字符与b的前j个字符的编辑距离:

d_{a,b}(i,j)=\begin{cases} \max(i,j) & \text {if } \min(i,j)=0 \\ \min\begin{cases} d_{a,b}(i-1,j)+1\\ d_{a,b}(i,j-1)+1\\ d_{a,b}(i-1,j-1)+1_{(a_{i}\neq b_{j})}\\ d_{a,b}(i-2,j-2)+1 \end{cases}& \text {if }i,j> 1 \text{ and }a_{i}=b_{j-1}\text{ and }a_{i-1}=b_{j}\\ \min\begin{cases} d_{a,b}(i-1,j)+1\\ d_{a,b}(i,j-1)+1\\ d_{a,b}(i-1,j-1)+1_{(a_{i}\neq b_{j})} \end{cases}& \text{otherwise} \end{cases}

i,j> 1 \text{ and }a_{i}=b_{j-1}\text{ and }a_{i-1}=b_{j} 时,除了要计算Levenshtein Distance中所定义的插入、删除和变更操作的操作数以外,还要计算相邻字符转换的操作数,然后将四个操作数做对比取最小的值。

示例

以字符串a:aborad和b:aboard为例,求值过程如下图:

图片制作中。。。

如图字符串ab的Damerau–Levenshtein Distance \operatorname{d}_{a,b}(6,6)为1,相似度Sim_{a,b}为:

Sim_{a,b}=1-(\operatorname{d}_{a,b}(6,6)/max(6,6))=0.8333\cdots

猜你喜欢

转载自blog.csdn.net/asty9000/article/details/81570627