Commit c05629c5 authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Refactor Corrector class to handle original sentence in top 5

parent 2bf2c37d
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -105,8 +105,24 @@ public class Corrector {
            }
        }

        // Check if the original sentence is in the top 5
        boolean originalInTop5 = false;
        for (String sentence : topSentences) {
            sentence = sentence.split(" \\| Score: ")[0]; // Remove score
            System.out.println(sentence);
            if (sentence != null && sentence.equals(inputSentence)) {
                originalInTop5 = true;
                break;
            }
        }

        if (originalInTop5) {
            String[] result = new String[0];
            return result;
        } else {
            return topSentences;
        }
    }

    // Helper class to manage sentences and their scores
    class SentenceScorePair implements Comparable<SentenceScorePair> {
@@ -192,6 +208,10 @@ public class Corrector {
                for (String sentence : sentences) {
                    sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
                    String[] corrected = corrector.correct(sentence);
                    if (corrected.length == 0) {
                        System.out.println(sentence + " | No corrections needed.");
                        continue;
                    }
                    System.out.println(sentence + " | Corrected Sentence Suggestions:");
                    printSentencesInOrderOfChanges(corrected, sentence);
                }
+8 −0
Original line number Diff line number Diff line
@@ -273,8 +273,13 @@ public class MainApp extends JFrame {
            for (String sentence : sentences) {
                sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
                String[] corrected = corrector.correct(sentence);
                if (corrected.length == 0) {
                    result.append(sentence + " | No corrections found for this sentence.\n\n");
                    continue;
                }
                result.append(sentence + " | Corrected Sentence Suggestions:\n");
                printSentencesInOrderOfChanges(corrected, sentence, result);
                result.append("\n");
            }

            resultArea.setText(result.toString());
@@ -286,6 +291,9 @@ public class MainApp extends JFrame {
                String sentenceNew = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
                String[] corrected = corrector.correct(sentenceNew);
                int correctedLength = corrected.length;
                if (correctedLength == 0) {
                    continue;
                }
                for (String s : corrected) {
                    if (s == null || s.isEmpty()) {
                        correctedLength--;
+9 −2
Original line number Diff line number Diff line
@@ -50,6 +50,13 @@ public class TrieNode implements Serializable, Cloneable {
        return (float) node.childCounts / node.children.size();
    }

    private int factorial(int n) {
        int res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }

    public float perplexity(String phrase) {
        float logProb = 0;
        String[] words = phrase.split(" ");
@@ -63,8 +70,8 @@ public class TrieNode implements Serializable, Cloneable {
            logProb += Math.log((probability(currentPhrase)));
        }
        float perplexity = (float) Math.pow(2, -logProb);
        System.out.println("Perplexity of phrase (" + phrase + ") : " + perplexity);
        return perplexity;
        //System.out.println("Perplexity of phrase (" + phrase + ") : " + ((float) perplexity / factorial(words.length)));
        return (float) perplexity / factorial(words.length);
    }
    
    public byte[] serialize() {