Commit b7da512a authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

addGUIforHighlighterANDlistOfCorrections

parent 87d6a0f1
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -139,6 +139,13 @@ public class Checker {
            }
            jsonMaker.toJson("confidence_ourChecker.json");
            System.out.println("##########################################################");
        }else if(argPars.isCheckGUI()){
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new HighlighterGUI();
                }
            });
        }
          
    }
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ public class Corrector {
        DBinterface dbInterface = new DBinterface();
        DirectedGraph<State> graph = basicGraphClass.getGraph();
        StringFileWriter stringWriter = StringFileWriter.of("corrected.txt");

        StringFileWriter.deleteFile("correction_details.txt");
        if(argPars.isCheckFile()){
            SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
            List<String> extractedSentences = extractor.getSentences();  
+22 −5
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;

import DirectedGraph.DirectedGraph;

@@ -11,6 +12,7 @@ import java.sql.*;
import StateMachine.*;
import TypoCorrector.TypoCorrector;
import util.TwoListStruct;
import util.StringFileWriter;
import util.StringProcessor;


@@ -156,6 +158,7 @@ public class DBinterface {
            int delCnt = 0;
            boolean seenDot = false;
            int     indDotseen = Math.max(suggested.size()+1, flags.size()+1);
            StringFileWriter sfw = StringFileWriter.of("correction_details.txt", "\n", true);
            for(int i=0; i<suggested.size(); i++){
                if(seenDot){
                    //indDotseen = i;
@@ -173,14 +176,19 @@ public class DBinterface {
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                            if(i<tokenList.size()){
                                sfw.appendString(tokenList.get(i) + " -> "+ word);
                                tokenList.set(i,word);
                            else
                                
                            }else{
                                sfw.appendString("IND: "+ i + " -> "+ word);
                                tokenList.add(word);
                            }
                        }
                    }
                }else if(flags.get(i+delCnt)==2){
                    delCnt++;
                    sfw.appendString(tokenList.get(i) + " -> X");
                    tokenList.remove(i);
                }else if(flags.get(i+delCnt)==3){
                    try (Statement statement = connection.createStatement()) {
@@ -191,14 +199,23 @@ public class DBinterface {
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                            if(i<tokenList.size()){
                                sfw.appendString("IND: "+ i + " -> "+ word);
                                tokenList.add(i,word);
                            else
                            }else{
                                sfw.appendString("IND: "+ i + " -> "+ word);
                                tokenList.add(word);
                            }
                        }
                    }
                }
            }   
            try {
                sfw.appendString("-----------------------------------------");
                sfw.writeToFile();
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
            StringBuilder result = new StringBuilder();
            boolean flagStart = false;
            int i = 0;
@@ -268,7 +285,7 @@ public class DBinterface {
                            cntMiss ++;
                        }
                }
                if(cntMiss>2)
                if(cntMiss>1)
                    return 0;
                    //System.out.print("After token: "+tokens[i]+"| ");
                    //System.out.println();
+102 −0
Original line number Diff line number Diff line
package GUI;

import javax.swing.*;
import javax.swing.text.DefaultHighlighter;

import DBinterface.DBinterface;
import DirectedGraph.BasicGraph;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HighlighterGUI extends JFrame {
    private JTextArea textArea;
    private JButton highlightButton;

    public HighlighterGUI() {
        setTitle("Text Highlighter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        textArea = new JTextArea(10, 30);
        highlightButton = new JButton("Highlight");
        highlightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                highlightPhrases();
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(highlightButton);

        add(new JScrollPane(textArea), BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void highlightPhrases() {
        String text = textArea.getText();
        List<String> phrases = extractPhrases(text, 3);
        highlightPhrases(phrases);
    }

    private List<String> extractPhrases(String text, int phraseLength) {
        List<String> phrases = new ArrayList<>();
        String[] words = text.split("\\s+");
        for (int i = 0; i <= words.length - phraseLength; i++) {
            StringBuilder phraseBuilder = new StringBuilder();
            for (int j = 0; j < phraseLength; j++) {
                phraseBuilder.append(words[i + j]);
                if (j < phraseLength - 1) {
                    phraseBuilder.append(" ");
                }
            }
            phrases.add(phraseBuilder.toString());
        }
        return phrases;
    }

    private void highlightPhrases(List<String> phrases) {
        DBinterface dbInterface = new DBinterface();
        BasicGraph basicGraphClass = new BasicGraph();
        for (String phrase : phrases) {
            highlightPhrase(phrase, dbInterface.checkTokenInDatabase(phrase, basicGraphClass.getGraph()));
        }
    }

    private Color getColorForNumber(int number) {
        float hue = (float) number / 100; // Hue ranges from 0 to 1
        return Color.getHSBColor(hue, 1, 1);
    }

    private void highlightPhrase(String phrase, int colorInd) {
        Color color = getColorForNumber(colorInd);
        String text = textArea.getText();
        int index = text.indexOf(phrase);
        while (index >= 0) {
            try {
                textArea.getHighlighter().addHighlight(index, index + phrase.length(), new DefaultHighlighter.DefaultHighlightPainter(color));
            } catch (Exception e) {
                // Handle exception
            }
            index = text.indexOf(phrase, index + 1);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HighlighterGUI();
            }
        });
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ public class ArgumentParser {
    private boolean updateToken;
    private boolean checkSentence;
    private boolean checkFile;
    private boolean checkGUI;
    private boolean updateHashTable;
    private boolean validateUpdates;

@@ -19,6 +20,7 @@ public class ArgumentParser {
        checkFile  = false;
        updateHashTable = false;
        validateUpdates = false;
        checkGUI = false;
        parseArguments(Arrays.asList(args));
    }

@@ -61,6 +63,9 @@ public class ArgumentParser {
                    case "--validateUpdates":
                        validateUpdates = true;
                        break;
                    case "--checkGUI":
                        checkGUI = true;
                        break;
                    // Add cases for other arguments here
                    default:
                        // Handle unknown arguments or simply ignore them
@@ -104,4 +109,7 @@ public class ArgumentParser {
    public boolean isValidateUpdates(){
        return validateUpdates;
    }
    public boolean isCheckGUI(){
        return checkGUI;
    }
}
Loading