假设,我有一个 ArrayList aList1,其中包含以下条目: 莫罕达斯·甘地, 甘地先生, 马丁·路德·金, 金正恩, 亚伯拉罕·林肯, 金正恩
假设,我有另一个 ArrayList aList2 所有正确的名称。如何将 aList1 中的每个项目与 aList2 匹配?
我希望最终输出是 Mohandas Gandhi、Mohandas Gandhi、Martin Luther King、Kim Jong、Abraham Lincoln、Kim Jong。
输出应该没有拼写错误。我如何匹配这两个词?如果我可以匹配两个单词,那么我可以使用 Edit distance将一个单词转换为另一个单词。
我需要用 Java 编写这个代码。
最佳答案
类似这样的事情应该可以帮助您开始:
String[] incorrectNames = "Mohandas Gandhi, M Gandhi, Martin Luther King, Kim Jong, Abrahm Lincln, Kim Jng".split(", ");
String[] dictionary = "Mohandas Gandhi, Martin Luther King, Kim Jong, Abraham Lincoln".split(", ");
List<String> correctedNames = new ArrayList<>();
for (String incorrectName : incorrectNames) {
int distance = Integer.MAX_VALUE;
String closestMatch = null;
for (String correctName : dictionary) {
int currentDistance = levenshteinDistance(incorrectName, correctName);
if (distance > currentDistance) {
distance = currentDistance;
closestMatch = correctName;
}
}
correctedNames.add(closestMatch);
}
return correctedNames;
您当然需要一个 levenshteinDistance
的实现。其他注意事项:该算法为 O(m*n)
,其中 m
是字典的大小,n
是要查找的名称数量。已更正,编辑距离可能太简单,无法很好地做到这一点。
关于java - 如何保持数据的一致性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36247105/