Levenshtein Trie错误的距离

时间:2014-06-13 13:28:03

标签: java trie levenshtein-distance

我在网上搜索了levenshtein trie和的实现 我发现了这个:Levenshtein Distance Challenge: Causes。 我试图添加一段代码来规范化单词。如果一个字 例如有5个字母('Apple'),我有这个词('Aple') 距离是1,我接受它是相同的。以我为例 有一个更长的词('情况'),你可以犯更多的错误。 如果你在这个单词中有两个错误,原始代码会 计算最小距离为2并且不接受它。所以我想要 使用对数。用对数表示'环境'之间的距离 和'kirkumstances'将小于2并且因为演员 这将是1.这就是我想做的事。

public class LevenshteinTrie {
    private int distance = -1;
    private Trie trie = null;

    public LevenshteinTrie(int distance, Set<String> words) {
        this.distance = distance;
        this.trie = new Trie();

        for(String word : words) {
            this.trie.insert(word);
        }
    }

    public Set<String> discoverFriends(String word, boolean normalized) {
        Set<String> results = new HashSet<String>();

        int[] currentRow = new int[word.length() + 1];

        List<Character> chars = new ArrayList<Character>(word.length());

        for(int i = 0; i < word.length(); i++) {
            chars.add(word.charAt(i));
            currentRow[i] = i;
        }

        currentRow[word.length()] = word.length();

        for(Character c : this.trie.getRoot().getChildren().keySet()) {
            this.traverseTrie(this.trie.getRoot().getChildren().get(c), c, chars, currentRow, results, normalized);
        }

        return results;
    }

    private void traverseTrie(TrieNode node, char letter, List<Character> word, int[] previousRow, Set<String> results, boolean normalized) {
        int size = previousRow.length;
        int[] currentRow = new int[size];

        currentRow[0] = previousRow[0] + 1;

        int minimumElement = currentRow[0];

        int insertCost = 0;
        int deleteCost = 0; 
        int replaceCost = 0;

        for(int i = 1; i < size; i++) {
            insertCost = currentRow[i - 1] + 1;
            deleteCost = previousRow[i] + 1;

            if(word.get(i - 1) == letter) {
                replaceCost = previousRow[i - 1];
            } else {
                replaceCost = previousRow[i - 1] + 1;
            }

            currentRow[i] = Math.min(Math.min(insertCost, deleteCost), replaceCost);

            if(currentRow[i] < minimumElement) {
                if(normalized) {
                    minimumElement = (int)(currentRow[i] / (Math.log10(word.size())));
                } else {
                    minimumElement = currentRow[i];
                }
            }
        }

        int tempCurrentRow = currentRow[size - 1];

        if(normalized) {
            tempCurrentRow = (int)(currentRow[size - 1] / (Math.log10(word.size())));
        }

        System.out.println(tempCurrentRow);

        if(tempCurrentRow <= this.distance && node.getWord() != null) {
            results.add(node.getWord());
        }

        if(minimumElement <= this.distance) {
            for(Character c : node.getChildren().keySet()) {
                this.traverseTrie(node.getChildren().get(c), c, word, currentRow, results, normalized);
            }
        }
    }
}

public class Trie {
    private TrieNode root = null;;

    public Trie() {
        this.root = new TrieNode();
    }

    public void insert(String word) {
        TrieNode current = this.root;

        if (word.length() == 0) {
            current.setWord(word);
        }

        for (int i = 0; i < word.length(); i++) {
            char letter = word.charAt(i);

            TrieNode child = current.getChild(letter);

            if (child != null) {
                current = child;
            } else {
                current.getChildren().put(letter, new TrieNode());
                current = current.getChild(letter);
            }

            if (i == word.length() - 1) {
                current.setWord(word);
            }
        }
    }
 }

public class TrieNode {
    public static final int ALPHABET = 26;
    private String word = null;
    private Map<Character, TrieNode> children = null;

    public TrieNode() {
        this.word = null;
        this.children = new HashMap<Character, TrieNode>(ALPHABET);
    }

    public TrieNode getChild(char letter) {
        if(this.children != null) {
            if(children.containsKey(letter)) {
                return children.get(letter);
            }
        }

        return null;
    }

    public String getWord() {
        return word;
    }
}

不幸的是,此代码无法正常运行。我将最大距离设置为1。 当我现在运行程序并搜索'vdimir putin'时(我有'vladimir putin' 在我的特里)该计划不会接受它作为朋友。当我打印出临时的 计算出的距离看起来像是:

当最大距离= 1时的tempCurrentRows:

11
11
10
10
10
10
11
11
11
11
10
11
11
11
11
11
11
11
10
10
10
10
10
10
10
10
10
10
9
11
11
10
10
10
10

但是当我将最大距离设置为2时,临时距离正在改变:

当最大距离= 2时的tempCurrentRows:

11
11
11
10
10
10
10
9
9
8
7
6
5
4
3
2
1
11
11
10
10
9
9

因此代码中必定存在重大错误。但我不知道在哪里和为什么 以及如何将代码更改为工作,因为我希望它能够工作。

2 个答案:

答案 0 :(得分:0)

你是如何实现'vdimir putin'的搜索? 你的代码似乎是对的。我测试了它:

public static void main(String[] args) {
    HashSet<String> words = new HashSet<String>();
    words.add("vdimir putin");
    LevenshteinTrie lt = new LevenshteinTrie(2, words);
    Set<String> friends = lt.discoverFriends("vladimir putin", false);
    System.out.println(friends.iterator().next());
}

这打印'vdimir putin',意思是“vladimir putin”有一个Levenshtein距离2的朋友

答案 1 :(得分:0)

哦,我想如果必须规范化最小元素:

if(normalized) {
    tempCurrentRow = (int)(currentRow[size - 1] / (Math.log10(word.size())));
    minimumElement = (int)(minimumElement / (Math.log10(word.size())));
}

并替换它:

 if(normalized) {
     minimumElement = (int)(currentRow[i] / (Math.log10(word.size())));
 } else {
     minimumElement = currentRow[i];
 }

用这个:

minimumElement = currentRow[i];

通过这个小小的改动它可以找到,因为我希望它工作。当我现在 搜索&#39; vdmir putin&#39;他正确的最大距离为1 发现弗拉基米尔·普京&#39;。