Commit e13a10e2 authored by Moises Bensadon's avatar Moises Bensadon
Browse files

work in progress to highlight text

parent 0580b3df
Loading
Loading
Loading
Loading
+49 −3
Original line number Diff line number Diff line
package edu.bu.LanguageCorrection;

import javax.swing.*;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.Highlight;

import java.awt.*;
// import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
@@ -12,6 +15,7 @@ import java.util.List;
public class MainApp extends JFrame {
    private final JTextField urlField;
    private JTextArea resultArea;
    private Highlighter.HighlightPainter myHighlightPainter;
    private final JButton runButton;
    private final JComboBox<String> moduleSelector;

@@ -115,16 +119,58 @@ public class MainApp extends JFrame {
            } else 
                content = text;
            Checker checker = new Checker();
            checker.analyze(content);
            checker.analyze(content); // This outputs void so we need to take the output from the console
            StringBuilder result = new StringBuilder();

            // Redirect output to the GUI
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            PrintStream printStream = new PrintStream(outputStream);
            PrintStream originalOut = System.out;
            System.setOut(printStream);
        
            // Reset System.out
            System.out.flush();
            System.setOut(originalOut);
    
            // Capture the output into a string
            String output = outputStream.toString();
    
            // Parsing the output to get sentences and phrases
            String[] lines = output.split("\n");
            String[] sentences = null;
            for (String line : lines) {
                if (line.startsWith("{")) {
                    continue;
                } else if (line.startsWith("sentences:")) {
                    line = line.replace("sentences:", "").replaceAll("\"{", "").replaceAll("}", "");
                    sentences = line.split(","); // List of sentences
                } else if (line.startsWith("phrases:")) {
                    line = line.replace("phrases:", "").replaceAll("\"{", "").replaceAll("}", "");
                    phrases = line.split(","); // List of phrases
                    for (String phrase : phrases) {
                        double phraseScore = phrases;
                        // Assuming highlight() method is defined elsewhere
                        highlight(resultArea, phrase, phraseScore); // Pass the phrase and its score to highlight
                    }
                }
            }
        } catch (Exception e) {
            resultArea.setText("Error: " + e.getMessage());
        }
    }

    public void highlight(JTextArea textArea, String phrase, String pattern) {
        try {
            Highlighter hilite = textArea.getHighlighter();
            int pos = 0;
            while ((pos = phrase.indexOf(pattern, pos)) >= 0) {
                // Create highlighter using private painter and apply around pattern
                hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
                pos += pattern.length();
            }
        } catch (Exception e) {
        }
    }

    private void runCorrector(String input, boolean isFile) {
        try {
            String content;