Commit 274231a2 authored by Manuel  Segimon's avatar Manuel Segimon
Browse files

Refactor Corrector and Checker classes to accept metadata file path as a parameter

parent dd38b617
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -11,10 +11,10 @@ import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class Checker {
    public void analyze(String text) {
    public void analyze(String text, String metadataPath) {
        List<String> sentences = TextProcessor.extractSentences(text);

        TrieNode detector = loadFile("metadata.ser");
        TrieNode detector = loadFile(metadataPath);

        Map<String, Float> sentenceScores = new HashMap<>();
        Map<String, Float> phraseScores = new HashMap<>();
@@ -97,14 +97,15 @@ public class Checker {
    }

    public static void main(String[] args) {
        if (args.length > 1 && "--file".equals(args[0])) { // check syntax
            String path = args[1];
        if (args.length > 3 && "--LF".equals(args[0]) && "--file".equals(args[2])) { // check syntax
            String metadataPath = args[1];
            String path = args[3];
            try {
                // Read entire file
                String content = new String(Files.readAllBytes(Paths.get(path)));

                Checker checker = new Checker(); // Run checker
                checker.analyze(content);
                checker.analyze(content, metadataPath);
            } catch (IOException e) {
                System.err.println("Error reading file: " + e.getMessage());
            }
+6 −5
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@ import java.util.HashMap;
public class Corrector {
    private TrieNode detector;

    public Corrector() {
        detector = loadFile("metadata.ser");
    public Corrector(String metadataPath) {
        detector = loadFile(metadataPath);
    }

    public TrieNode getDetector() {
@@ -209,11 +209,12 @@ public class Corrector {
    }

    public static void main(String[] args) {
        if (args.length > 1 && "--file".equals(args[0])) {
            String path = args[1];
        if (args.length > 3 && "--LF".equals(args[0]) && "--file".equals(args[2])) {
            String metadataPath = args[1];
            String path = args[3];
            try {
                String content = new String(Files.readAllBytes(Paths.get(path)));
                Corrector corrector = new Corrector(); // Run corrector
                Corrector corrector = new Corrector(metadataPath); // Run corrector
                String[] sentences = TextProcessor.extractSentences(content).toArray(new String[0]);
                for (String sentence : sentences) {
                    sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", "");
+30 −5
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import java.awt.*;
// import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringBufferInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
@@ -33,8 +34,11 @@ public class MainApp extends JFrame {
    private JTextArea resultArea;
    private Highlighter.HighlightPainter myHighlightPainter;
    private final JButton runButton;
    private final JButton changeLanguageButton;
    private final JComboBox<String> moduleSelector;

    private String languageFile = "English.ser";

    public MainApp() {
        super("Language Correction Tool");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -43,11 +47,12 @@ public class MainApp extends JFrame {
        setLayout(new BorderLayout());

        // Check if metadata file exists
        if (!Files.exists(Paths.get("metadata.ser"))) {
        if (!Files.exists(Paths.get(languageFile))) {
            String[] languages = {"English", "German", "Portuguese", "Italian"};
            String selectedLanguage = (String) JOptionPane.showInputDialog(this, "Metadata file not found. Please choose a language to build off of. \n(If you want to build from scratch just click Cancel)", "Language Selection", JOptionPane.PLAIN_MESSAGE, null, languages, languages[0]);
            if (selectedLanguage != null) {
                crawler webCrawler = new crawler();
                languageFile = selectedLanguage + ".ser";
                crawler webCrawler = new crawler(languageFile);
                webCrawler.build(selectedLanguage);
            }
        }
@@ -86,10 +91,30 @@ public class MainApp extends JFrame {
                    JOptionPane.showMessageDialog(this, "Select a valid module");
            }
        });
        changeLanguageButton = new JButton("Change Language");
        changeLanguageButton.addActionListener(e -> {
            String[] languages = {"English", "German", "Portuguese", "Italian"};
            String selectedLanguage = (String) JOptionPane.showInputDialog(this, "Please choose a language to build off of.", "Language Selection", JOptionPane.PLAIN_MESSAGE, null, languages, languages[0]);
            if (selectedLanguage != null) {
                languageFile = selectedLanguage + ".ser";

                // Check if metadata file exists
                String extraText = "";
                if (!Files.exists(Paths.get(languageFile))) {
                    crawler webCrawler = new crawler(languageFile);
                    webCrawler.build(selectedLanguage);
                    extraText = "New metadata file created.\n";
                }

                JOptionPane.showMessageDialog(this, extraText + "Language changed to " + selectedLanguage + ".");
            }
        });

        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(moduleSelector, BorderLayout.WEST);
        northPanel.add(urlField, BorderLayout.CENTER);
        northPanel.add(runButton, BorderLayout.EAST);
        northPanel.add(changeLanguageButton, BorderLayout.NORTH);

        // Result area
        resultArea = new JTextArea();
@@ -102,7 +127,7 @@ public class MainApp extends JFrame {

    private void runCrawler(String input) {
        StringBuilder outputBuilder = new StringBuilder();
        crawler webCrawler = new crawler(output -> {
        crawler webCrawler = new crawler(languageFile, output -> {
            outputBuilder.append(output);
            SwingUtilities.invokeLater(() -> resultArea.setText(outputBuilder.toString()));
        });
@@ -150,7 +175,7 @@ public class MainApp extends JFrame {

            for (String sentence : sentences) {
                System.setOut(printStream);
                checker.analyze(sentence); // Analyze each sentence separately
                checker.analyze(sentence, languageFile); // Analyze each sentence separately

                // Reset System.out
                System.out.flush();
@@ -270,7 +295,7 @@ public class MainApp extends JFrame {
                content= new String(Files.readAllBytes(Paths.get(input)));
            else
                content = input;
            Corrector corrector = new Corrector();
            Corrector corrector = new Corrector(languageFile);
            String[] sentences = TextProcessor.extractSentences(content).toArray(new String[0]);

            StringBuilder result = new StringBuilder();
+17 −5
Original line number Diff line number Diff line
@@ -26,12 +26,22 @@ import java.lang.Math;
public class crawler {

    public static void main(String[] args) throws IOException {
        // Extract metadata file from first argument
        String file;
        if (!args[0].equals("--LF")) {
            System.err.println("Invalid argument. Please provide the metadata file.");
            return;
        } else {
            file = args[1];
        }


        // Initialize web crawler
        crawler web_crawler = new crawler();
        crawler web_crawler = new crawler(file);

        // Open the file and read all lines (URLs) into the queue
        String file_url = "";
        for (int i = 0; i < args.length; i++) {
        for (int i = 2; i < args.length; i++) {
            if (args[i].equals("--file")) {
                file_url = args[i + 1];
            } else if (args[i].equals("--debug")) {
@@ -80,7 +90,7 @@ public class crawler {
    // members
    private final LinkedList<String> url_queue;
    private final HashSet<String> visited_urls;
    private String filePath = "metadata.ser";
    private String filePath;
    private TrieNode wordUsage = new TrieNode();
    private int compression_size = 0;
    private boolean debug = false; // flag that outputs uncompressed json showing the trie
@@ -89,23 +99,25 @@ public class crawler {
    private static final int MAXNGRAM = 3;
    private Consumer<String> outputCallback;

    public crawler() {
    public crawler(String file) {
        url_queue = new LinkedList<>();
        visited_urls = new HashSet<>();

        // Load trie from file
        filePath = file;
        wordUsage = loadFile(filePath);
        this.outputCallback = System.out::println;

        // Estimate page count based on compressed file size
    }

    public crawler(Consumer<String> outputCallback) {
    public crawler(String file, Consumer<String> outputCallback) {
        this.outputCallback = outputCallback;
        url_queue = new LinkedList<>();
        visited_urls = new HashSet<>();

        // Load trie from file
        filePath = file;
        wordUsage = loadFile(filePath);
    }