java - 如何保持数据的一致性?

标签 java nlp

假设,我有一个 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/

相关文章:

java - ubuntu下eclipse fatal error

java数组插入到postgres

python - Pytorch 的 nn.TransformerEncoder "src_key_padding_mask"未按预期运行

nlp - 通过spacy检测条件时态?

machine-learning - 使用gensim的fasttext包装器训练词嵌入后,如何嵌入新句子?

java - Hibernate 查询获取日期记录忽略时间戳

java - 为 JavaFX 禁用 Alt + F4

python - 从 nltk 树中获取单词的深度

python-2.7 - 重新训练 spaCy 的 NER v1.8.2 - 训练量和实体类型的混合

java - 仿射变换的问题