详细字间距
我将如何显示单词之间的详细距离。 例如,程序的输出可能是:
Words are "car" and "cure":
Replace "a" with "u".
Add "e".
Levenshtein 距离不能满足我的需求(我认为)。
How would I go about displaying detailed distance between words.
For example, the output of the program could be:
Words are "car" and "cure":
Replace "a" with "u".
Add "e".
The Levenshtein distance does not fulfill my needs (I think).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作。该算法大致遵循维基百科(Levenshtein 距离)。下面使用的语言以ruby
为例,将
s
改为t
的情况如下:首先,
s和
t
转为数组,并在开头插入一个空字符串。m
最终将是算法中使用的矩阵。然而,这里的
m
与维基百科中的算法给出的矩阵不同,因为每个单元不仅包含 Levenshtein 距离,还包含(非)操作(开始、不执行任何操作、删除、插入或替换)用于从相邻(左、上或左上)单元格到达该单元格。它还可能包括描述操作参数的字符串。即每个单元格的格式为:[Levenshtein distance, operation(, string)]
这里是主例程。它按照算法填充
m
的单元格:现在,我们将
i
、j
设置为m 的右下角
并按照步骤向后执行,将单元格的内容取消移动到名为steps
的数组中,直到到达开头。然后我们打印操作和每个步骤的字符串,除非那是非操作。
对于这个特定的例子,它将输出:
Try the following. The algorithm is roughly following Wikipedia (Levenshtein distance). The language used below is ruby
Use as an example, the case of changing
s
intot
as follows:First,
s
andt
are turned into arrays, and an empty string is inserted at the beginning.m
will eventually be the matrix used in the argorithm.m
here, however, is different from the matrix given if the algorithm in wikipedia for the fact that each cell includes not only the Levenshtein distance, but also the (non-)operation (starting, doing nothing, deletion, insertion, or substitution) that was used to get to that cell from an adjacent (left, up, or upper-left) cell. It may also include a string describing the parameters of the operation. That is, the format of each cell is:[Levenshtein distance, operation(, string)]
Here is the main routine. It fills in the cells of
m
following the algorithm:Now, we set
i
,j
to the bottom-right corner ofm
and follow the steps backwards as we unshift the contents of the cell into an array calledsteps
, until we reach the start.Then we print the operation and the string of each step unless that is a non-operation.
With this particular example, it will output: