Commit 85b3a2d7 authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

fixGUIbugs

parent 1316a67f
Loading
Loading
Loading
Loading
+28 −9
Original line number Diff line number Diff line
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import DBinterface.DBinterface;
@@ -35,27 +36,45 @@ public class Corrector implements GUIListener {

    @Override
    public String updateFlagsAndLabel(List<Boolean> flags) {
        this.curSentenceGUI = dbInterfaceGUI.correctTokenInDatabaseGUI(this.curSentenceGUI.toLowerCase(), graphGUI, flags);
        String tempString = dbInterfaceGUI.correctTokenInDatabaseGUI(this.curSentenceGUI.toLowerCase(), graphGUI, flags);
        if(tempString.contains("|")){
            String[] parts = tempString.split("|");
            //this.curSentenceGUI = parts[0];
            int ind = Integer.parseInt(parts[1]);
            List<Boolean> flagsTemp = new ArrayList<>();
            for(int k=0; k<flags.size(); k++){
                if(k<=ind)
                    flagsTemp.add(flags.get(k));
                else
                    flagsTemp.add(true);
            }
            String tempSentenceGUI = dbInterfaceGUI.correctTokenInDatabase(this.curSentenceGUI.toLowerCase(), graphGUI, 1, false, flags);
        }else{
            this.curSentenceGUI = tempString;
            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());
        }

        this.senteceIndGUI++;
        if(this.senteceIndGUI>=extractedSentencesGUI.size()){
            System.out.println("end!");
            return "";
        }
        
        this.curSentenceGUI = extractedSentencesGUI.get(senteceIndGUI);
        String tempSentenceGUI = dbInterfaceGUI.correctTokenInDatabase(this.curSentenceGUI.toLowerCase(), graphGUI, 1, false);
        return this.curSentenceGUI;

    }
+128 −83
Original line number Diff line number Diff line
@@ -93,6 +93,15 @@ public class DBinterface {
        return sentence;
    }

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

    public String correctTokenInDatabaseGUI(String sentence, DirectedGraph<State> graph, List<Boolean> flagsCorrection){
        return  correctTokenInDatabaseInnerloop(sentence, graph, flagsCorrection, false);
    }
@@ -107,6 +116,9 @@ public class DBinterface {
        String dicFileName = "./SQLite/smallDic.txt";
        TypoCorrector typoChecker =  TypoCorrector.of(dicFileName);
        int initialConf = 0;
        int flagsCorrectioncnt = 0;
        boolean flagTypoCorrectionAccepted = true;
        StringFileWriter sfw = StringFileWriter.of("correction_details.txt", "\n", isNotGUI);
        try (Connection connection = DriverManager.getConnection(url)) {

            // Lookup each token in the database and categorize it
@@ -127,8 +139,20 @@ public class DBinterface {
                        String tokenCorrected = new String();
                        if(role.isEmpty()){
                            tokenCorrected = typoChecker.closestWord(token);
                            if(!tokenCorrected.equals(token))
                            if(!tokenCorrected.equals(token)){
                                
                                initialConf += 5;
                                if(flagsCorrection.isEmpty()){
                                    sfw.appendString(token + " -> "+ tokenCorrected + "*");
                                }else if(!flagsCorrection.get(flagsCorrectioncnt) && isNotGUI){
                                    flagTypoCorrectionAccepted = false;
                                    //tokenList.set(i, "nan");
                                    break;
                                }else if(!flagsCorrection.get(flagsCorrectioncnt)){
                                    tokenCorrected = token;
                                }
                                flagsCorrectioncnt++;
                            }
                           // ////System.out.print("Corrected token: " + token + " -> " + tokenCorrected);

                            query = "SELECT role FROM word_roles WHERE word = '" + tokenCorrected + "';";
@@ -146,7 +170,8 @@ public class DBinterface {
                    }
                    //////System.out.println();
            }

            int     indDotseen = tokenList.size()+1;
            if(flagTypoCorrectionAccepted){
                List<State> actions = new ArrayList<>();

                for(String token: tokens){
@@ -163,9 +188,13 @@ public class DBinterface {
                List<Integer> flags   = output.getChangesList();
                int delCnt = 0;
                boolean seenDot = false;
            int     indDotseen = Math.max(suggested.size()+1, flags.size()+1);
            StringFileWriter sfw = StringFileWriter.of("correction_details.txt", "\n", isNotGUI);
            int flagsCorrectioncnt = 0;
                indDotseen = Math.max(suggested.size()+1, flags.size()+1);
                
                
                int biasToken = 0;
                
                //if(!isNotGUI)
                // System.out.println(flagsCorrection);
                for(int i=0; i<suggested.size(); i++){
                    if(seenDot){
                        //indDotseen = i;
@@ -183,14 +212,14 @@ public class DBinterface {
                            if (resultSet.next()) {
                                word = resultSet.getString("word");
                                ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size()){
                                if(i+biasToken<tokenList.size()){
                                    
                                    if(flagsCorrection.isEmpty()){
                                    tokenList.set(i,word);
                                        tokenList.set(i+biasToken,word);
                                        sfw.appendString(tokenList.get(i) + " -> "+ word);
                                    }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                    tokenList.set(i,word);
                                    flagsCorrectioncnt++;
                                        
                                        tokenList.set(i+biasToken,word);
                                    }
                                    
                                    
@@ -200,20 +229,24 @@ public class DBinterface {
                                        tokenList.add(word);
                                    }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                        tokenList.add(word);
                                    flagsCorrectioncnt++;
                                    }
                                }
                                flagsCorrectioncnt++;
                                //System.out.println(biasToken);
                            }
                        }
                    }else if(flags.get(i+delCnt)==2){
                        delCnt++;
                        if(flagsCorrection.isEmpty()){
                            sfw.appendString(tokenList.get(i) + " -> X");
                        tokenList.remove(i);
                            tokenList.remove(i+biasToken);
                        }else if(flagsCorrection.get(flagsCorrectioncnt)){
                        tokenList.remove(i);
                        flagsCorrectioncnt++;
                            tokenList.remove(i+biasToken);
                        }else{
                            biasToken++;
                        }
                        flagsCorrectioncnt++;
                    // System.out.println(biasToken);
                    }else if(flags.get(i+delCnt)==3){
                        try (Statement statement = connection.createStatement()) {
                            ////System.out.println(suggested.get(i));
@@ -223,25 +256,32 @@ public class DBinterface {
                            if (resultSet.next()) {
                                word = resultSet.getString("word");
                                ////System.out.println("Here I am: "+ word);
                                
                                if(flagsCorrection.isEmpty()){
                                    sfw.appendString("IND: "+ i + " -> "+ word);
                                    if(i<tokenList.size()){
                                    tokenList.add(i,word);
                                        tokenList.add(i+biasToken,word);
                                    }else{
                                        tokenList.add(word);
                                    }
                                }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                    if(i<tokenList.size()){
                                    tokenList.add(i,word);
                                    // System.out.println("here!");
                                        tokenList.add(i+biasToken,word);
                                    }else{
                                        tokenList.add(word);
                                    }
                                }else{
                                    biasToken--;
                                }
                                flagsCorrectioncnt++;
                            // System.out.println(biasToken);
                            }
                        }
                    }
                }   
            }

            try {
                if(isNotGUI)
                    sfw.appendString("-----------------------------------------");
@@ -251,6 +291,7 @@ public class DBinterface {
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
           // System.out.println(tokenList);
            StringBuilder result = new StringBuilder();
            boolean flagStart = false;
            int i = 0;
@@ -263,6 +304,10 @@ public class DBinterface {
                flagStart = true;  
                i++;
            }
            if(!flagTypoCorrectionAccepted){
                result.append("|");
                result.append(flagsCorrectioncnt);
            }
            return result.toString();
        } catch (SQLException e) {
            e.printStackTrace();
+36 −8
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@ public class SelectCorrectionGUI extends JFrame {
    private JButton nextButton;
    private List<JButton> buttons;
    private JLabel counterLabel;
    private JLabel noteLabel;
    private JLabel emptyLabel;
    private GUIListener listener;
    private static final int POPUP_WIDTH = 400;
    private static final int POPUP_HEIGHT = 200;
    private static final int POPUP_WIDTH = 1000;
    private static final int POPUP_HEIGHT = 300;
    

    public SelectCorrectionGUI(GUIListener listener, String label) {
@@ -28,7 +30,7 @@ public class SelectCorrectionGUI extends JFrame {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        labelPanel = new JPanel(new GridLayout(1, 1));
        labelPanel = new JPanel(new GridLayout(3, 1));
        applyPanel = new JPanel(new GridLayout(1, 2));
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setPreferredSize(new Dimension(POPUP_WIDTH, POPUP_HEIGHT));
@@ -46,12 +48,19 @@ public class SelectCorrectionGUI extends JFrame {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        if(btn.getBackground().equals(Color.GREEN))
                            btn.setBackground(Color.LIGHT_GRAY);
                        else
                            btn.setBackground(Color.GREEN);
                    }
                });
                buttons.add(button);
                panel.add(button);
            }
            if(buttons.isEmpty()){
                noteLabel = new JLabel("All set! No suggestion!");
                noteLabel.setHorizontalAlignment(SwingConstants.CENTER);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
@@ -80,13 +89,14 @@ public class SelectCorrectionGUI extends JFrame {
            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);
                    return;
                }

                updateCounter(listener.loadNextSentece());
                // Update buttons based on file changes
                updateCounter(sentence);
                updatePanel();
                
            }
@@ -95,6 +105,8 @@ public class SelectCorrectionGUI extends JFrame {

        updatePanel();
        labelPanel.add(counterLabel);  
        labelPanel.add(emptyLabel);
        labelPanel.add(noteLabel);  
        applyPanel.add(applyButton);
        applyPanel.add(nextButton);
        mainPanel.add(labelPanel, BorderLayout.NORTH);
@@ -110,8 +122,13 @@ public class SelectCorrectionGUI extends JFrame {
        setVisible(true);
    }
    private void updateCounter(String label){
        emptyLabel = new JLabel(" ");
        emptyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        noteLabel = new JLabel("Rejection of suggestion with * will result in a new set of suggestions regardless of other choices.");
        noteLabel.setHorizontalAlignment(SwingConstants.CENTER);
        counterLabel = new JLabel(label);
        counterLabel.setHorizontalAlignment(SwingConstants.CENTER);
        counterLabel.setFont(new Font(counterLabel.getFont().getName(), Font.BOLD, 20)); 
    }

    private void updatePanel() {
@@ -127,17 +144,25 @@ public class SelectCorrectionGUI extends JFrame {
            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();
                        if(btn.getBackground().equals(Color.GREEN))
                            btn.setBackground(Color.LIGHT_GRAY);
                        else
                            btn.setBackground(Color.GREEN);
                    }
                });
                buttons.add(button);
                panel.add(button);
                if(buttons.isEmpty()){
                    noteLabel = new JLabel("All set! No suggestion!");
                    noteLabel.setHorizontalAlignment(SwingConstants.CENTER);
                }
            }
            br.close();
        } catch (IOException e) {
@@ -150,9 +175,12 @@ public class SelectCorrectionGUI extends JFrame {
        panel.repaint();

        labelPanel.add(counterLabel);
        labelPanel.add(emptyLabel);
        labelPanel.add(noteLabel);
        labelPanel.revalidate();
        labelPanel.repaint();
        
        
        applyPanel.add(applyButton);
        applyPanel.add(nextButton);
        applyPanel.revalidate();