Commit 9c169b68 authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Add selected correction to trie

parent c4fd30e0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ import java.util.HashMap;


public class Corrector {
    private TrieNode detector;
    public TrieNode detector;

    public Corrector() {
        detector = loadFile("metadata.ser");
+43 −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;



@@ -302,16 +305,55 @@ public class MainApp extends JFrame {
                }
                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) {
                    resultArea.setText(resultArea.getText().replace(sentence, selected));
                    // Insert the selected correction into the trie for future reference
                    TrieNode node = corrector.detector;
                    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 = corrector.detector.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.