Commit 8ace970f authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Merge branch '32-add-spelling-corrections' into 'master'

Resolve "Add spelling corrections"

Closes #32

See merge request ec504/ec504_projects/group7!17
parents 3328321e 9640d115
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -63,6 +63,16 @@ public class Corrector {
        // Divide sentence into words
        String[] words = inputSentence.split(" ");

        // Check if all words are in the trie
        for (int i = 0; i < words.length; i++) {
            if (!detector.containsWord(words[i])) {
                String closestWord = detector.findClosestWord(words[i]);
                if (closestWord != null) {
                    words[i] = closestWord;
                }
            }
        }

        List<String> sentences = generateSentences(words);

        // Use a priority queue to store sentences along with their scores
+36 −0
Original line number Diff line number Diff line
@@ -24,6 +24,42 @@ public class TrieNode implements Serializable, Cloneable {
        past.childCounts += 1;
    }

    public boolean containsWord(String word) {
        return this.children.containsKey(word);
    }

    public String findClosestWord(String inputWord) {
        int minDistance = Integer.MAX_VALUE;
        String closestWord = null;

        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) {
        String[] words = phrase.split(" ");
        if (words.length <= 1) { // If word does not exist in trie