Commit 7ff656fe authored by Moises Bensadon's avatar Moises Bensadon
Browse files

Merge branch '37-add-perplexity-of-original-sentence-to-corrrector-output' into 'master'

Resolve "Add Perplexity of Original sentence to Corrrector output"

Closes #37

See merge request ec504/ec504_projects/group7!22
parents 4c5e4f1c 7708c990
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -112,19 +112,25 @@ public class Corrector {
        // }

        // Convert to array of just sentences
        String[] topSentences = new String[topPairs.length];
        for (int i = 0; i < topPairs.length; i++) {
        String[] topSentences = new String[topPairs.length+1];
        topSentences[0] = inputSentence+" | Score: "+detector.perplexity(inputSentence);
        for (int i = 1; i < topPairs.length+1; i++) {
            // Remove sentence that are not in the 0.5 percentile of the best sentence
            if (topPairs[i].score > topPairs[0].score * 1.5) {
            if (topPairs[i-1].score > topPairs[0].score * 1.5) {
                topSentences[i] = "";
            } else {
                topSentences[i] = topPairs[i].sentence + " | Score: " + topPairs[i].score;
                topSentences[i] = topPairs[i-1].sentence + " | Score: " + topPairs[i-1].score;
            }
        }

        // Check if the original sentence is in the top 5
        boolean originalInTop5 = false;
        boolean first = true;
        for (String sentence : topSentences) {
            if (first) {
                first = false;
                continue;
            }
            sentence = sentence.split(" \\| Score: ")[0]; // Remove score
            //System.out.println(sentence);
            if (sentence != null && sentence.equals(inputSentence)) {
@@ -134,7 +140,8 @@ public class Corrector {
        }

        if (originalInTop5) {
            String[] result = new String[0];
            String[] result = new String[1];
            result[0] = topSentences[0];
            return result;
        } else {
            return topSentences;
@@ -203,9 +210,11 @@ public class Corrector {
        Map<String, Integer> changesMap = new HashMap<>();

        String[] originalWords = originalSentence.split(" ");
        boolean first = true;

        for (String sentence : sentences) {
            if (sentence == null || sentence.isEmpty()) {
            if (sentence == null || sentence.isEmpty() || first) {
                first = false;
                continue;
            }

@@ -225,6 +234,7 @@ public class Corrector {
        List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(changesMap.entrySet());
        sortedList.sort(Map.Entry.comparingByValue());


        for (Map.Entry<String, Integer> entry : sortedList) {
            outputCallback.accept(">> " + entry.getKey() + " | Changes: " + entry.getValue()+"\n");
        }
+10 −16
Original line number Diff line number Diff line
@@ -285,20 +285,18 @@ public class MainApp extends JFrame {
            progressBar.setStringPainted(true);
            progressBar.setString(null);
            progressBar.setMaximum(sentences.length*2);

            for (String sentence : sentences) {
                progressBar.setValue(progressBar.getValue() + 1);
                sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
                String[] corrected = corrector.correct(sentence);
                progressBar.setValue(progressBar.getValue() + 2);
                if (corrected.length == 0) {
                if (corrected.length == 1) {
                    outputCallback.accept(sentence + " | No corrections found for this sentence.\n\n");
                    continue;
                }
                outputCallback.accept(sentence + " | Corrected Sentence Suggestions:\n");
                outputCallback.accept(corrected[0] + " | Corrected Sentence Suggestions:\n");
                corrector.printSentencesInOrderOfChanges(corrected, sentence);
            }

            // resultArea.setText(result.toString());
            // ISSUE #30 - Feedback for corrector
            // Add a pop up to input the best correction for each sentence
@@ -308,7 +306,7 @@ 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) {
                if (correctedLength <= 1) {
                    continue;
                }
                for (String s : corrected) {
@@ -318,16 +316,11 @@ public class MainApp extends JFrame {
                }
                //System.out.println(correctedLength);
                String[] options = new String[correctedLength+1];
                options[0] = sentence;
                for (int i = 0; i < corrected.length; i++) {
                    // System.out.println(corrected[i]);
                    if (corrected[i] == null || corrected[i].isEmpty()) {
                        continue;
                    }

                    options[i + 1] = corrected[i];
                    options[i] = corrected[i];
                    // System.out.println(options[i]);
                    options[i + 1] = options[i + 1].split(" \\| Score: ")[0];
                    options[i] = options[i].split(" \\| Score: ")[0];
                    // System.out.println(options[i]);
                }
                String selected = (String) JOptionPane.showInputDialog(this, "Select the best correction for the following sentence:\n  " + sentence, "Correction Selection", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
@@ -344,6 +337,7 @@ public class MainApp extends JFrame {
                        progressBar.setValue(progressBar.getValue() + 1);
                    }
                    changeMade = true;
                    resultArea.setText("");
                } else {
                    System.out.println("No correction selected");
                }