Commit 9640d115 authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Refactor Corrector class to use findClosestWord method for word correction

parent 8b5afb3c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -66,7 +66,10 @@ public class Corrector {
        // Check if all words are in the trie
        for (int i = 0; i < words.length; i++) {
            if (!detector.containsWord(words[i])) {
                continue;
                String closestWord = detector.findClosestWord(words[i]);
                if (closestWord != null) {
                    words[i] = closestWord;
                }
            }
        }

+29 −3
Original line number Diff line number Diff line
@@ -28,10 +28,36 @@ public class TrieNode implements Serializable, Cloneable {
        return this.children.containsKey(word);
    }

    public String findClosestWord(String word) {
        String[] children = this.children.keySet().toArray(new String[0]);
    public String findClosestWord(String inputWord) {
        int minDistance = Integer.MAX_VALUE;
        String closestWord = null;

        return "";
        for (String word : this.children.keySet()) {
            int distance = levenshteinDistance(inputWord, word);
            if (distance < minDistance) {
                minDistance = distance;
                closestWord = word;
            }
        }
        return closestWord;
    }

    private int levenshteinDistance(String s1, String s2) {
        int[][] dp = new int[s1.length() + 1][s2.length() + 1];

        for (int i = 0; i <= s1.length(); i++) {
            for (int j = 0; j <= s2.length(); j++) {
                if (i == 0) {
                    dp[i][j] = j;
                } else if (j == 0) {
                    dp[i][j] = i;
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1),
                            dp[i - 1][j - 1] + (s1.charAt(i - 1) == s2.charAt(j - 1) ? 0 : 1));
                }
            }
        }
        return dp[s1.length()][s2.length()];
    }

    public float probability(String phrase) {