Commit 3328321e authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Merge branch '26-corrector-and-checker-are-suggesting-changes-when-not-required' into 'master'

Resolve "Corrector and Checker are suggesting changes when not required"

Closes #26

See merge request ec504/ec504_projects/group7!16
parents 37d7ef02 00dcb616
Loading
Loading
Loading
Loading
+22 −2
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> {
@@ -178,7 +194,7 @@ public class Corrector {
        sortedList.sort(Map.Entry.comparingByValue());

        for (Map.Entry<String, Integer> entry : sortedList) {
            System.out.println("    " + entry.getKey() + " | Changes: " + entry.getValue());
            System.out.println(">> " + entry.getKey() + " | Changes: " + entry.getValue());
        }
    }

@@ -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);
                }
+22 −7
Original line number Diff line number Diff line
@@ -186,11 +186,15 @@ public class MainApp extends JFrame {
                    }
                }

                if (worstPhrase != null) {
                if (worstPhrase != null && lowestScore > 10.0) { // Check if the worst phrase has a low enough score
                    // Append to the result with annotations
                    result.append("\nSentence: ").append(sentence)
                            .append("\n>> Worst Phrase: ").append(worstPhrase)
                            .append(" (Score: ").append(lowestScore).append(")\n");
                } else {
                    result.append("\nSentence: ").append(sentence)
                            .append("\n>> No worst phrase found (with low enough score), the sentence is grammatically correct.\n");
                    worstPhrases[sentences.indexOf(sentence)] = "";
                }
            }

@@ -202,7 +206,7 @@ public class MainApp extends JFrame {
            for (String phrase : worstPhrases) {
                phrase = phrase.replaceAll("\"", "");
                //System.out.println(phrase);
                if (phrase != null) {
                if (phrase != null && !phrase.isEmpty()) {
                    //System.out.println(resultArea.getText());
                    int start = resultArea.getText().indexOf(phrase);
                    if (start != -1) {
@@ -254,7 +258,7 @@ public class MainApp extends JFrame {

        int i = 1;
        for (Map.Entry<String, Integer> entry : sortedList) {
            result.append("    " + i + ") " + entry.getKey() + " | Changes: " + entry.getValue() + "\n");
            result.append(">> " + entry.getKey() + " | Changes: " + entry.getValue() + "\n");
            i++;
        }
    }
@@ -273,18 +277,27 @@ 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());

            // Add a pop up to input the best correction for each sentence
            TrieNode node = corrector.getDetector();
            boolean changeMade = false;
            for (String sentence : sentences) {
                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--;
@@ -312,16 +325,18 @@ public class MainApp extends JFrame {
                        String[] words = phrase.split(" ");
                        node.insert(words);
                    }
                    changeMade = true;
                } else {
                    System.out.println("No correction selected");
                }
            }

            // Serialize the trie
            if (changeMade) {
                byte[] serialized = node.serialize();
                byte[] compressed = compress(serialized);
                writeToFile(compressed, "metadata.ser");

            }
        } catch (Exception e) {
            resultArea.setText("Error: " + e.getMessage());
        }
+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() {