Commit 1316a67f authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

correctionGUIFunci

parent 5f3d2ddc
Loading
Loading
Loading
Loading
+71 −17
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ import java.util.List;
import DBinterface.DBinterface;
import DirectedGraph.BasicGraph;
import DirectedGraph.DirectedGraph;
import GUI.SelectCorrectionGUI;
import GUI.GUIListener;
import util.ArgumentParser;
import util.JsonMaker;
import util.PhraseExtractor;
@@ -12,38 +14,90 @@ import util.StringFileWriter;
import StateMachine.*;


public class Corrector {
public class Corrector implements GUIListener {
    SentenceExtractor extractorGUI;
    private String curSentenceGUI;
    private DBinterface dbInterfaceGUI;
    private StringFileWriter stringWriterGUI;
    private List<String> extractedSentencesGUI;
    private DirectedGraph<State> graphGUI;
    private int senteceIndGUI;
    public void start() {
        
        this.dbInterfaceGUI = new DBinterface();
        this.graphGUI = new BasicGraph().getGraph();
        this.stringWriterGUI = StringFileWriter.of("corrected.txt");
        this.senteceIndGUI = 0;
        this.curSentenceGUI = extractedSentencesGUI.get(0);
        String tempString = dbInterfaceGUI.correctTokenInDatabase(this.curSentenceGUI.toLowerCase(), graphGUI, 1, false);
        SelectCorrectionGUI gui = new SelectCorrectionGUI(this, this.curSentenceGUI);
    }

    @Override
    public String updateFlagsAndLabel(List<Boolean> flags) {
        this.curSentenceGUI = dbInterfaceGUI.correctTokenInDatabaseGUI(this.curSentenceGUI.toLowerCase(), graphGUI, flags);
        String tempSentenceGUI = dbInterfaceGUI.correctTokenInDatabase(this.curSentenceGUI.toLowerCase(), graphGUI, 1, false);
        return this.curSentenceGUI;
    }

    @Override
    public String loadNextSentece() {
        if(this.senteceIndGUI>=extractedSentencesGUI.size()){
            System.out.println("end!");
            return "";
        }
        stringWriterGUI.appendString(this.curSentenceGUI);
        this.senteceIndGUI++;
        this.curSentenceGUI = extractedSentencesGUI.get(senteceIndGUI);
        String tempSentenceGUI = dbInterfaceGUI.correctTokenInDatabase(this.curSentenceGUI.toLowerCase(), graphGUI, 1, false);
        try {
            stringWriterGUI.writeToFile();
           // System.out.println("Corrected version has been written to the file.");
        } catch (IOException e) {
            System.err.println("An error occurred while writing to the file: " + e.getMessage());
        }
        return this.curSentenceGUI;

    }
    Corrector(SentenceExtractor extractor){
        this.extractedSentencesGUI = extractor.getSentences();
    }
    public static void main(String[] args) {
        //DirectedGraph<State> graph = new DirectedGraph<>();
        
        ArgumentParser argPars = ArgumentParser.of(args);
        BasicGraph basicGraphClass = new BasicGraph();
        DBinterface dbInterface = new DBinterface();
        DirectedGraph<State> graph = basicGraphClass.getGraph();
        DirectedGraph graph = basicGraphClass.getGraph();
        StringFileWriter stringWriter = StringFileWriter.of("corrected.txt");
        Corrector corrector = new Corrector(SentenceExtractor.of(argPars.getFileName()));
            
        StringFileWriter.deleteFile("correction_details.txt");
        if(argPars.isCheckFile()){
            SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
            List<String> extractedSentences = extractor.getSentences();
            
            
            if(argPars.isCorrectionGUI()){    
                    corrector.start();
            }else{
                for (String sentence : extractedSentences) {
                    System.out.println("Sentence: " + sentence);
                stringWriter.appendString(dbInterface.correctTokenInDatabase(sentence.toLowerCase(), graph));
                    String tempString = dbInterface.correctTokenInDatabase(sentence.toLowerCase(), graph, 2, true);
                    stringWriter.appendString(tempString);

                    
                    System.out.println("##########################################################");
                }
            }
            try {
                stringWriter.writeToFile();
                System.out.println("Corrected version has been written to the file.");
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
                System.out.println("##########################################################");
                
            }
        }else if(argPars.isCheckSentence()){

            System.out.println("Sentence: " + argPars.getSentence());
            stringWriter.appendString(dbInterface.correctTokenInDatabase(argPars.getSentence().toLowerCase(), graph));
            stringWriter.appendString(dbInterface.correctTokenInDatabase(argPars.getSentence().toLowerCase(), graph, 2, true));
            try {
                stringWriter.writeToFile();
                System.out.println("Corrected version has been written to the file.");
+53 −18
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import util.TwoListStruct;
import util.StringFileWriter;
import util.StringProcessor;

import GUI.SelectCorrectionHandler;

public class DBinterface {
    public int checkTokenInDatabase(String sentence, DirectedGraph<State> graph){
@@ -82,16 +83,21 @@ public class DBinterface {
        return 0;
    }

    public String correctTokenInDatabase(String sentence, DirectedGraph<State> graph){
        for(int i=0; i<2; i++){
            sentence = new String(correctTokenInDatabaseInnerloop(sentence, graph));
    public String correctTokenInDatabase(String sentence, DirectedGraph<State> graph, int cnt, boolean isNotGUI){
        List<Boolean> flagsCorrection = new ArrayList<>();
        for(int i=0; i<cnt; i++){ 
            sentence = new String(correctTokenInDatabaseInnerloop(sentence, graph, flagsCorrection, isNotGUI));
            if(checkTokenInDatabase(sentence, graph)<10)
                break;
        }
        return sentence;
    }

    private String correctTokenInDatabaseInnerloop(String sentence, DirectedGraph<State> graph){
    public String correctTokenInDatabaseGUI(String sentence, DirectedGraph<State> graph, List<Boolean> flagsCorrection){
        return  correctTokenInDatabaseInnerloop(sentence, graph, flagsCorrection, false);
    }

    private String correctTokenInDatabaseInnerloop(String sentence, DirectedGraph<State> graph, List<Boolean> flagsCorrection, boolean isNotGUI){
        StateMachine SM = new StateMachine();
        sentence = sentence.replaceAll("\\p{Punct}", " $0");
        String[] tokens = sentence.split("\\s+");
@@ -158,7 +164,8 @@ 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);
            StringFileWriter sfw = StringFileWriter.of("correction_details.txt", "\n", isNotGUI);
            int flagsCorrectioncnt = 0;
            for(int i=0; i<suggested.size(); i++){
                if(seenDot){
                    //indDotseen = i;
@@ -177,19 +184,36 @@ public class DBinterface {
                            word = resultSet.getString("word");
                            ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size()){
                                
                                if(flagsCorrection.isEmpty()){
                                    tokenList.set(i,word);
                                    sfw.appendString(tokenList.get(i) + " -> "+ word);
                                }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                    tokenList.set(i,word);
                                    flagsCorrectioncnt++;
                                }
                                
                                
                            }else{
                                if(flagsCorrection.isEmpty()){
                                    sfw.appendString("IND: "+ i + " -> "+ word);
                                    tokenList.add(word);
                                }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                    tokenList.add(word);
                                    flagsCorrectioncnt++;
                                }
                            }
                        }
                    }
                }else if(flags.get(i+delCnt)==2){
                    delCnt++;
                    if(flagsCorrection.isEmpty()){
                        sfw.appendString(tokenList.get(i) + " -> X");
                        tokenList.remove(i);
                    }else if(flagsCorrection.get(flagsCorrectioncnt)){
                        tokenList.remove(i);
                        flagsCorrectioncnt++;
                    }
                }else if(flags.get(i+delCnt)==3){
                    try (Statement statement = connection.createStatement()) {
                        ////System.out.println(suggested.get(i));
@@ -199,20 +223,31 @@ public class DBinterface {
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size()){
                            if(flagsCorrection.isEmpty()){
                                sfw.appendString("IND: "+ i + " -> "+ word);
                                if(i<tokenList.size()){
                                    tokenList.add(i,word);
                                }else{
                                    tokenList.add(word);
                                }
                            }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                if(i<tokenList.size()){
                                    tokenList.add(i,word);
                                }else{
                                sfw.appendString("IND: "+ i + " -> "+ word);
                                    tokenList.add(word);
                                }
                                flagsCorrectioncnt++;
                            }
                        }
                    }
                }
            }   
            try {
                if(isNotGUI)
                    sfw.appendString("-----------------------------------------");
                if(flagsCorrection.isEmpty()){
                    sfw.writeToFile();
                }
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
+2 −2
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ public class BasicGraph {
    private void createTable() {
        edgeList = new HashMap<>();
        // SQL statement to create the table
        String sqlCreateTable = "CREATE TABLE nodeOne_nodeTwo ("
        /*String sqlCreateTable = "CREATE TABLE nodeOne_nodeTwo ("
                + "nodeOne VARCHAR(50) PRIMARY KEY,"
                + "nodeTwo VARCHAR(50)"
                + ")";
@@ -198,7 +198,7 @@ public class BasicGraph {
            stmt.execute(sqlCreateTable);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        }*/
    }

    // Method to read data from SQLite database into HashMap
+6 −0
Original line number Diff line number Diff line
package GUI;
import java.util.List;
public interface GUIListener {
    String updateFlagsAndLabel(List<Boolean> flags);
    String loadNextSentece();
}
 No newline at end of file
+167 −0
Original line number Diff line number Diff line

package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SelectCorrectionGUI extends JFrame {
    private JPanel panel;
    private JPanel labelPanel;
    private JPanel applyPanel;
    private JPanel  mainPanel;
    private JButton applyButton;
    private JButton nextButton;
    private List<JButton> buttons;
    private JLabel counterLabel;
    private GUIListener listener;
    private static final int POPUP_WIDTH = 400;
    private static final int POPUP_HEIGHT = 200;
    

    public SelectCorrectionGUI(GUIListener listener, String label) {
        this.listener = listener;
        setTitle("Correction Suggestions");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        labelPanel = new JPanel(new GridLayout(1, 1));
        applyPanel = new JPanel(new GridLayout(1, 2));
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setPreferredSize(new Dimension(POPUP_WIDTH, POPUP_HEIGHT));
        panel.setLayout(new FlowLayout());

        buttons = new ArrayList<>();
        updateCounter(label);
        try {
            File file = new File("correction_details.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                JButton button = new JButton(line);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        btn.setBackground(Color.GREEN);
                    }
                });
                buttons.add(button);
                panel.add(button);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        applyButton = new JButton("Apply Corrections");
        applyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Boolean> flags = new ArrayList<>();
                for (JButton button : buttons) {
                    flags.add(button.getBackground().equals(Color.GREEN));
                }
                // Pass the flags and counter to Main using the interface method
                updateCounter(listener.updateFlagsAndLabel(flags));
                // Update buttons based on file changes
                updatePanel();
                
            }
        });


        nextButton = new JButton("Next Sentence");
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                String sentence = listener.loadNextSentece();
                if(sentence.equals("")){
                    JOptionPane.showMessageDialog(SelectCorrectionGUI.this, "All senteces have been reviewed and the corrected version has been written to corrected.txt!");
                    System.exit(0);
                }

                updateCounter(listener.loadNextSentece());
                // Update buttons based on file changes
                updatePanel();
                
            }
        });


        updatePanel();
        labelPanel.add(counterLabel);    
        applyPanel.add(applyButton);
        applyPanel.add(nextButton);
        mainPanel.add(labelPanel, BorderLayout.NORTH);
        mainPanel.add(panel, BorderLayout.CENTER);
        mainPanel.add(applyPanel, BorderLayout.SOUTH);

        add(mainPanel);

        pack();
        setLocationRelativeTo(null); // Center the window

        //add(mainPanel);
        setVisible(true);
    }
    private void updateCounter(String label){
        counterLabel = new JLabel(label);
        counterLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }

    private void updatePanel() {
        // Here you should update the buttons based on changes in the text file
        // For simplicity, I'm just removing all buttons and adding new ones
        panel.removeAll();
        labelPanel.removeAll();
        applyPanel.removeAll();
        mainPanel.removeAll();

        buttons.clear();
        try {
            File file = new File("correction_details.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                JButton button = new JButton(line);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        btn.setBackground(Color.GREEN);
                    }
                });
                buttons.add(button);
                panel.add(button);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //panel.add(applyButton);
        //panel.add(counterLabel);
        panel.revalidate();
        panel.repaint();

        labelPanel.add(counterLabel);
        labelPanel.revalidate();
        labelPanel.repaint();
        
        applyPanel.add(applyButton);
        applyPanel.add(nextButton);
        applyPanel.revalidate();
        applyPanel.repaint();
        
        mainPanel.add(labelPanel, BorderLayout.NORTH);
        mainPanel.add(panel, BorderLayout.CENTER);
        mainPanel.add(applyPanel, BorderLayout.SOUTH);
        mainPanel.revalidate();
        mainPanel.repaint();
    }
}
Loading