Levenshtein distance 不会完全起作用,因为您希望允许重新排列。我认为您最好的选择是找到以列文斯坦距离作为每个单词成本的最佳重新排列。
要找到重新排列的成本,有点像pancake sorting problem。因此,您可以置换每个单词组合(过滤掉完全匹配),以及其他字符串的每个组合,尽量减少每个单词对上置换距离和 Levenshtein 距离的组合。
编辑:
现在我有时间可以发布一个简单的示例(所有“最佳”猜测都在检查中,而不是实际运行算法):
original strings | best rearrangement w/ lev distance per word
Into the clear blue sky | Into the c_lear blue sky
The color is sky blue | is__ the colo_r blue sky
R_dist = dist( 3 1 2 5 4 ) --> 3 1 2 *4 5* --> *2 1 3* 4 5 --> *1 2* 3 4 5 = 3
L_dist = (2D+S) + (I+D+S) (Total Subsitutions: 2, deletions: 3, insertion: 1)
(注意所有翻转都包括范围内的所有元素,我使用 Xi - Xj = +/- 1 的范围)
其他例子
original strings | best rearrangement w/ lev distance per word
Into the clear blue sky | Into the clear blue sky
In the blue clear sky | In__ the clear blue sky
R_dist = dist( 1 2 4 3 5 ) --> 1 2 *3 4* 5 = 1
L_dist = (2D) (Total Subsitutions: 0, deletions: 2, insertion: 0)
并显示这三者的所有可能组合...
The color is sky blue | The colo_r is sky blue
In the blue clear sky | the c_lear in sky blue
R_dist = dist( 2 4 1 3 5 ) --> *2 3 1 4* 5 --> *1 3 2* 4 5 --> 1 *2 3* 4 5 = 3
L_dist = (D+I+S) + (S) (Total Subsitutions: 2, deletions: 1, insertion: 1)
无论如何,您使成本函数成为第二选择将是最低成本,这是您所期望的!