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

Merge branch '30-feedback-system-for-corrector' into 'master'

Resolve "Feedback system for corrector"

Closes #30

See merge request ec504/ec504_projects/group7!15
parents cd776704 96e3f457
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@ public class Corrector {
        detector = loadFile("metadata.ser");
    }

    public TrieNode getDetector() {
        return detector;
    }

    private TrieNode loadFile(String filePath) {
        TrieNode trie = new TrieNode();
        try (FileInputStream fis = new FileInputStream(filePath)) {
+77 −1
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ import javax.swing.text.*;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.zip.Deflater;
import java.io.FileOutputStream;
import java.io.IOException;



@@ -249,8 +252,10 @@ public class MainApp extends JFrame {
        List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(changesMap.entrySet());
        sortedList.sort(Map.Entry.comparingByValue());

        int i = 1;
        for (Map.Entry<String, Integer> entry : sortedList) {
            result.append("    " + entry.getKey() + " | Changes: " + entry.getValue() + "\n");
            result.append("    " + i + ") " + entry.getKey() + " | Changes: " + entry.getValue() + "\n");
            i++;
        }
    }

@@ -273,11 +278,82 @@ public class MainApp extends JFrame {
            }

            resultArea.setText(result.toString());

            // Add a pop up to input the best correction for each sentence
            TrieNode node = corrector.getDetector();
            for (String sentence : sentences) {
                String sentenceNew = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
                String[] corrected = corrector.correct(sentenceNew);
                int correctedLength = corrected.length;
                for (String s : corrected) {
                    if (s == null || s.isEmpty()) {
                        correctedLength--;
                    }
                }
                //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];
                    //System.out.println(options[i]);
                    options[i + 1] = options[i + 1].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]);
                if (selected != null) {
                    // Insert the selected correction into the trie for future reference
                    List<String> phrases = TextProcessor.extractPhrases(selected, 2, 3);
                    for (String phrase : phrases) {
                        String[] words = phrase.split(" ");
                        node.insert(words);
                    }
                } else {
                    System.out.println("No correction selected");
                }
            }

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

        } catch (Exception e) {
            resultArea.setText("Error: " + e.getMessage());
        }
    }

    private static byte[] compress(byte[] input) {
        // Create compressor
        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
        compressor.setInput(input);
        compressor.finish();

        // Store compressed data in dynamic byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        compressor.end();

        return bos.toByteArray();
    }

    private static void writeToFile(byte[] compressedData, String filePath) {
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            fos.write(compressedData);
        } catch (IOException e) {
            System.err.println("Error writing metadata to file: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainApp mainApp = new MainApp();
+1 −1

File changed.

Contains only whitespace changes.