如何修改 Levenshteins 编辑距离以计算“相邻字母交换”作为 1 编辑
我正在使用 Levenshteins 编辑距离算法,我想扩展它来计算换位 - 即交换相邻字母的数量——作为 1 次编辑。未修改的算法计算从另一个字符串到达某个字符串所需的插入、删除或替换次数。例如,从“KITTEN”到“SITTING”的编辑距离是3。维基百科的解释如下:
- kitten→sitten(将'k'替换为's')
- sitten→sittin(将'e'替换为'i') )
- 坐 → 坐(在末尾插入“g”)。
按照相同的方法,从“CHIAR”到“CHAIR”的编辑距离为2:
- CHIAR → CHAR (delete 'I')
- CHAR → CHAIR (insert 'I')
我想将其计为“1次编辑”,因为我只交换两个相邻的字母。我该怎么做呢?
I'm playing around with Levenshteins Edit Distance algorithm, and I want to extend this to count transpositions -- that is, exchanges of adjacent letters -- as 1 edit. The unmodified algorithm counts insertions, deletes or substitutions needed to reach a certain string from another. For instance, the edit distance from "KITTEN" to "SITTING" is 3. Here's the explanation from Wikipedia:
- kitten → sitten (substitution of 'k' with 's')
- sitten → sittin (substitution of 'e' with 'i')
- sittin → sitting (insert 'g' at the end).
Following the same method, the edit distance from "CHIAR" to "CHAIR" is 2:
- CHIAR → CHAR (delete 'I')
- CHAR → CHAIR (insert 'I')
I would like to count this as "1 edit", since I only exchange two adjacent letters. How would I go about to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还需要维基百科的算法中的另一种情况:
You need one more case in the algorithm from Wikipedia:
您必须修改更新动态规划表的方式。在原始算法中,我们考虑长度最多相差一的两个单词的尾部(或头部)。更新是所有此类可能性中的最低限度。
如果您想修改算法,使两个相邻位置的变化计为 1,则必须根据最多相差 2 的尾部(或头部)计算上述最小值。您可以将其扩展到更大的社区,但复杂性将随着该社区的大小呈指数级增加。
您可以进一步概括并分配取决于删除、插入或替换的字符的成本,但您必须确保分配给配对编辑的成本低于两次单独编辑,否则两次单独编辑将总是赢。
令单词为 w1 和 w2
我所说的
&&
的意思是,只有满足条件时才应考虑这些行。You have to modify how you update the dynamic programming table. In the original algorithm one considers the tails(or heads) of the two words that differ at the most by length one. The update is the minimum of all such possibilities.
If you want to modify the algorithm such that changes in two adjacent locations count as one, the minimum above has to be computed over tails(or heads) that differ by at most two. You can extend this to larger neighborhoods but the complexity will increase exponentially in the size of that neighborhood.
You can generalize further and assign costs that depend on the character(s) deleted, inserted or substituted, but you have to make sure that the cost you assign to a pair-edit is lower than two single edits, otherwise the two single edits will always win.
Let the words be w1 and w2
What I mean by the
&&
is that those lines should be considered only if the conditions are satisfied.其他答案是实现最佳字符串对齐算法,而不是 Damerau Levenshtein我想这就是你所描述的。
我有一个 OSA 的 Java 实现,并进行了一些优化:
https://gist.github.com/steveash/5426191
The other answers are implementing the Optimal String Alignment algorithm, not Damerau Levenshtein which I think is what you are describing.
I have a java implementation of OSA with some optimizations here:
https://gist.github.com/steveash/5426191