java 最短的 Levenshtein 距离?我需要吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17274183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shortest Levenshtein Distance? Do I need it?
提问by Writwick
I want to look up a String
in a String[]
for the best match of the query. I have heard of Levenshtein Distance. But I cannot determine if I need it or not.
我想String
在 a 中String[]
查找查询的最佳匹配。我听说过 Levenshtein 距离。但我无法确定我是否需要它。
Suppose, I have a String query = "Examples"
and
假设,我有一个String query = "Examples"
和
String[] arrayStr = new String[] {"The Examples String", "The Example String", "Example", "Examples String", "Example String", "Examplestring"};
Now, I want to get the Example
from the String[]
as the best match.
现在,我想Example
从String[]
作为最佳匹配中获取。
So, Do I need Levenshtein Distance to do it?
那么,我需要 Levenshtein Distance 吗?
Alternatively, If someone can point me a fast implementation of Levenshtein Distance for Java, it would be great. I would like to check if it works with all the kind of strings that I have. (Basically I have around 10k strings to match from 10k arrays.)
或者,如果有人可以为我指出 Levenshtein Distance for Java 的快速实现,那就太好了。我想检查它是否适用于我拥有的所有类型的字符串。(基本上我有大约 10k 个字符串可以从 10k 个数组中匹配。)
回答by GreyBeardedGeek
Yes, Levenshtein Distance is the goto algorithm for this. You can find implementations in many languages, including Java, at http://rosettacode.org/wiki/Levenshtein_distance
是的,Levenshtein Distance 是这个的 goto 算法。您可以在http://rosettacode.org/wiki/Levenshtein_distance找到多种语言的实现,包括 Java