diff --git a/.gitignore b/.gitignore index 996f9277f3c1bd01c1f34ae360a1d5428f9d916f..489d0c4c6a64821cfff2cb5676048117e7f7b796 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ CheckerCorrector/corrector CheckerCorrector/checker Crawler/target/classes/ScratchCrawler.class Crawler/target/classes/crawledData.txt +*.db *.java !CheckerCorrector/manifest* !CheckerCorrector/**/*.java diff --git a/CheckerCorrector/Checker.java b/CheckerCorrector/Checker.java index cca39136a55a1997056344cef99359079e746c85..a83a423540d921d6aefa62eabe7f569059bd6dc2 100644 --- a/CheckerCorrector/Checker.java +++ b/CheckerCorrector/Checker.java @@ -25,10 +25,15 @@ public class Checker { JsonMaker jsonMaker = JsonMaker.create(); if(argPars.isValidateUpdates()){ + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - new WordRoleUpdater(); + String dbName = "SQLite/newdatabase.db"; + if(argPars.isCheckFile()){ + dbName = argPars.getFileName(); + } + new WordRoleUpdater(dbName); } }); }else if(argPars.isUpdateToken()){ @@ -134,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(); + } + }); } } diff --git a/CheckerCorrector/Corrector.java b/CheckerCorrector/Corrector.java index 7ef4d887a5874ca5d4e942b011526268974e2e33..39983662a9c4201d9b6783c2755557df0950cbda 100644 --- a/CheckerCorrector/Corrector.java +++ b/CheckerCorrector/Corrector.java @@ -1,9 +1,12 @@ import java.io.IOException; +import java.util.ArrayList; 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 +15,108 @@ 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 extractedSentencesGUI; + private DirectedGraph 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 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 flagsTemp = new ArrayList<>(); + for(int k=0; k=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; + + } + Corrector(SentenceExtractor extractor){ + this.extractedSentencesGUI = extractor.getSentences(); + } public static void main(String[] args) { //DirectedGraph graph = new DirectedGraph<>(); ArgumentParser argPars = ArgumentParser.of(args); BasicGraph basicGraphClass = new BasicGraph(); DBinterface dbInterface = new DBinterface(); - DirectedGraph 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 extractedSentences = extractor.getSentences(); - - - for (String sentence : extractedSentences) { - System.out.println("Sentence: " + sentence); - stringWriter.appendString(dbInterface.correctTokenInDatabase(sentence.toLowerCase(), graph)); - - 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()); + List extractedSentences = extractor.getSentences(); + if(argPars.isCorrectionGUI()){ + corrector.start(); + }else{ + for (String sentence : extractedSentences) { + System.out.println("Sentence: " + sentence); + String tempString = dbInterface.correctTokenInDatabase(sentence.toLowerCase(), graph, 2, true); + stringWriter.appendString(tempString); + + + System.out.println("##########################################################"); } - 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()); } }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."); diff --git a/CheckerCorrector/DBinterface/DBinterface.java b/CheckerCorrector/DBinterface/DBinterface.java index a614d91420f54ab8140004c838f8a664c1297f7f..09348a5e8408753724c7d9822fa7a25ce7610e92 100644 --- a/CheckerCorrector/DBinterface/DBinterface.java +++ b/CheckerCorrector/DBinterface/DBinterface.java @@ -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,8 +12,10 @@ import java.sql.*; import StateMachine.*; import TypoCorrector.TypoCorrector; import util.TwoListStruct; +import util.StringFileWriter; import util.StringProcessor; +import GUI.SelectCorrectionHandler; public class DBinterface { public int checkTokenInDatabase(String sentence, DirectedGraph graph){ @@ -80,16 +83,30 @@ public class DBinterface { return 0; } - public String correctTokenInDatabase(String sentence, DirectedGraph graph){ - for(int i=0; i<2; i++){ - sentence = new String(correctTokenInDatabaseInnerloop(sentence, graph)); + public String correctTokenInDatabase(String sentence, DirectedGraph graph, int cnt, boolean isNotGUI){ + List flagsCorrection = new ArrayList<>(); + for(int i=0; i graph){ + public String correctTokenInDatabase(String sentence, DirectedGraph graph, int cnt, boolean isNotGUI, List flagsCorrection){ + for(int i=0; i graph, List flagsCorrection){ + return correctTokenInDatabaseInnerloop(sentence, graph, flagsCorrection, false); + } + + private String correctTokenInDatabaseInnerloop(String sentence, DirectedGraph graph, List flagsCorrection, boolean isNotGUI){ StateMachine SM = new StateMachine(); sentence = sentence.replaceAll("\\p{Punct}", " $0"); String[] tokens = sentence.split("\\s+"); @@ -99,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 @@ -119,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 + "';"; @@ -138,67 +170,128 @@ public class DBinterface { } //////System.out.println(); } + int indDotseen = tokenList.size()+1; + if(flagTypoCorrectionAccepted){ + List actions = new ArrayList<>(); - List actions = new ArrayList<>(); - - for(String token: tokens){ - actions.add(State.fromString(token)); - } - // Define the initial state - State initialState = State.START; + for(String token: tokens){ + actions.add(State.fromString(token)); + } + // Define the initial state + State initialState = State.START; - // Check if the sequence of actions follows the state machine + // Check if the sequence of actions follows the state machine - TwoListStruct output = SM.suggestedStateMachine(graph, actions, initialState); - // output.displayArrays(); - List suggested = output.getOutputList(); - List flags = output.getChangesList(); - int delCnt = 0; - boolean seenDot = false; - int indDotseen = Math.max(suggested.size()+1, flags.size()+1); - for(int i=0; i output = SM.suggestedStateMachine(graph, actions, initialState); + // output.displayArrays(); + List suggested = output.getOutputList(); + List flags = output.getChangesList(); + int delCnt = 0; + boolean seenDot = false; + indDotseen = Math.max(suggested.size()+1, flags.size()+1); + + + int biasToken = 0; + + //if(!isNotGUI) + // System.out.println(flagsCorrection); + for(int i=0; i "+ word); + }else if(flagsCorrection.get(flagsCorrectioncnt)){ + + tokenList.set(i+biasToken,word); + } + + + }else{ + if(flagsCorrection.isEmpty()){ + sfw.appendString("IND: "+ i + " -> "+ word); + tokenList.add(word); + }else if(flagsCorrection.get(flagsCorrectioncnt)){ + tokenList.add(word); + } + } + 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+biasToken); + }else if(flagsCorrection.get(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)); + String query = "SELECT word FROM word_roles WHERE role = '" + suggested.get(i) + "';"; + String word = new String(); + ResultSet resultSet = statement.executeQuery(query); + if (resultSet.next()) { + word = resultSet.getString("word"); + ////System.out.println("Here I am: "+ word); + + if(flagsCorrection.isEmpty()){ + sfw.appendString("IND: "+ i + " -> "+ word); + if(i1) + return 0; //System.out.print("After token: "+tokens[i]+"| "); - //////System.out.println(); + //System.out.println(); } //System.out.println("\nMISS: "+cntMiss); - if(cntMiss<3 && cntMiss>0 && !missFlag.get(missFlag.size()-1)){ + if(cntMiss>0 && !missFlag.get(missFlag.size()-1)){ List actions = new ArrayList<>(); for(String token: tokens){ diff --git a/CheckerCorrector/DirectedGraph/BasicGraph.java b/CheckerCorrector/DirectedGraph/BasicGraph.java index 630867ce4e688f936d3da1f6e56d34f759e69c4a..36ac27511faf7e3a0082976695a14ff843098391 100644 --- a/CheckerCorrector/DirectedGraph/BasicGraph.java +++ b/CheckerCorrector/DirectedGraph/BasicGraph.java @@ -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 diff --git a/CheckerCorrector/GUI/GUIListener.java b/CheckerCorrector/GUI/GUIListener.java new file mode 100644 index 0000000000000000000000000000000000000000..ff36f0e205d563beb63b35164bae8d9c04018379 --- /dev/null +++ b/CheckerCorrector/GUI/GUIListener.java @@ -0,0 +1,6 @@ +package GUI; +import java.util.List; +public interface GUIListener { + String updateFlagsAndLabel(List flags); + String loadNextSentece(); +} \ No newline at end of file diff --git a/CheckerCorrector/GUI/HighlighterGUI.java b/CheckerCorrector/GUI/HighlighterGUI.java new file mode 100644 index 0000000000000000000000000000000000000000..2fa04bba07030529f95a0b8754f5bc8672a82566 --- /dev/null +++ b/CheckerCorrector/GUI/HighlighterGUI.java @@ -0,0 +1,102 @@ +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 phrases = extractPhrases(text, 3); + highlightPhrases(phrases); + } + + private List extractPhrases(String text, int phraseLength) { + List 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 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(); + } + }); + } +} diff --git a/CheckerCorrector/GUI/SelectCorrectionGUI.java b/CheckerCorrector/GUI/SelectCorrectionGUI.java new file mode 100644 index 0000000000000000000000000000000000000000..98a941cf907e293dfcb1d65e6bef046eaffe7a4f --- /dev/null +++ b/CheckerCorrector/GUI/SelectCorrectionGUI.java @@ -0,0 +1,195 @@ + +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 buttons; + private JLabel counterLabel; + private JLabel noteLabel; + private JLabel emptyLabel; + private GUIListener listener; + private static final int POPUP_WIDTH = 1000; + private static final int POPUP_HEIGHT = 300; + + + 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(3, 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(); + 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(); + } + + applyButton = new JButton("Apply Corrections"); + applyButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + List 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); + return; + } + // Update buttons based on file changes + updateCounter(sentence); + updatePanel(); + + } + }); + + + updatePanel(); + labelPanel.add(counterLabel); + labelPanel.add(emptyLabel); + labelPanel.add(noteLabel); + 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){ + 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() { + // 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(); + 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(); + } + + //panel.add(applyButton); + //panel.add(counterLabel); + panel.revalidate(); + panel.repaint(); + + labelPanel.add(counterLabel); + labelPanel.add(emptyLabel); + labelPanel.add(noteLabel); + 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(); + } +} diff --git a/CheckerCorrector/GUI/SelectCorrectionHandler.java b/CheckerCorrector/GUI/SelectCorrectionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..28df87be08385d8fcd658a837b774e20385a1a4d --- /dev/null +++ b/CheckerCorrector/GUI/SelectCorrectionHandler.java @@ -0,0 +1,29 @@ +package GUI; +import java.util.List; + +public class SelectCorrectionHandler {/*implements GUIListener { + private String sentence; + private List flags; + + public SelectCorrectionHandler(int sentence){ + this.sentence = sentence; + } + + + public void start() { + SelectCorrectionGUI gui = new SelectCorrectionGUI(this, this.sentence); + } + + @Override + public String updateFlagsAndLabel(List flags) { + // Print the flags + this.flags = flags; + this.updateApplied = true; + return this.sentence; + + } + @Override + public String loadNeString(){ + return String(); + }*/ +} diff --git a/CheckerCorrector/GUI/WordRoleUpdater.java b/CheckerCorrector/GUI/WordRoleUpdater.java index ebb30e344e0bca7f66769c4cdb596dea9d77399b..c437405340cfba1a6ceb0e5c9899ec6dc608d459 100644 --- a/CheckerCorrector/GUI/WordRoleUpdater.java +++ b/CheckerCorrector/GUI/WordRoleUpdater.java @@ -9,7 +9,7 @@ import java.util.HashMap; import java.util.Map; public class WordRoleUpdater extends JFrame { - private static final String DB_URL = "jdbc:sqlite:./SQLite/newdatabase.db"; + private String DB_URL = "jdbc:sqlite:./"; private static final String TABLE_NAME = "word_roles"; private static final int POPUP_WIDTH = 300; private static final int POPUP_HEIGHT = 200; @@ -21,18 +21,21 @@ public class WordRoleUpdater extends JFrame { private JLabel progressLabel; private JLabel wordLabel; private JLabel roleLabel; + private JLabel currentWordStatusLabel; private JButton acceptButton; private JButton rejectButton; private JButton acceptAllButton; private JButton rejectAllButton; + private JButton backButton; + private JButton nextButton; - public WordRoleUpdater() { + public WordRoleUpdater(String DB_URL_suffix) { + DB_URL = DB_URL.concat(DB_URL_suffix); try { connection = DriverManager.getConnection(DB_URL); loadWordRoles(); initializeUI(); - updateWordLabel(); // Show the first word and its role - updateRoleLabel(); + updateLabels(); } catch (SQLException e) { e.printStackTrace(); } @@ -49,7 +52,7 @@ public class WordRoleUpdater extends JFrame { String word = resultSet.getString("word"); String role = resultSet.getString("role"); wordRoles.put(word, role); - changes.put(word, false); + changes.put(word, true); } statement.close(); @@ -58,35 +61,42 @@ public class WordRoleUpdater extends JFrame { private void initializeUI() { setTitle("Word Role Updater"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLayout(new BorderLayout()); progressLabel = new JLabel(); progressLabel.setHorizontalAlignment(SwingConstants.CENTER); - updateProgressLabel(); wordLabel = new JLabel(); wordLabel.setHorizontalAlignment(SwingConstants.CENTER); - updateWordLabel(); roleLabel = new JLabel(); roleLabel.setHorizontalAlignment(SwingConstants.CENTER); - updateRoleLabel(); - JPanel labelPanel = new JPanel(new GridLayout(3, 1)); + currentWordStatusLabel = new JLabel(); + currentWordStatusLabel.setHorizontalAlignment(SwingConstants.CENTER); + // updateCurrentStatus(); + + updateLabels(); + JPanel labelPanel = new JPanel(new GridLayout(4, 1)); labelPanel.add(progressLabel); labelPanel.add(wordLabel); labelPanel.add(roleLabel); + labelPanel.add(currentWordStatusLabel); acceptButton = new JButton("Accept"); rejectButton = new JButton("Reject"); acceptAllButton = new JButton("Accept All"); rejectAllButton = new JButton("Reject All"); + backButton = new JButton("Back"); + nextButton = new JButton("Next"); - JPanel buttonPanel = new JPanel(new GridLayout(2, 2)); + JPanel buttonPanel = new JPanel(new GridLayout(3, 2)); buttonPanel.add(acceptButton); buttonPanel.add(rejectButton); buttonPanel.add(acceptAllButton); buttonPanel.add(rejectAllButton); + buttonPanel.add(backButton); + buttonPanel.add(nextButton); + acceptButton.addActionListener(new ActionListener() { @Override @@ -96,9 +106,24 @@ public class WordRoleUpdater extends JFrame { } }); + nextButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + goNext(); + } + }); + + backButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + goBack(); + } + }); + rejectButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + changes.put(getCurrentWord(), false); //wordRoles.remove(getCurrentWord()); // Remove the current word //changes.remove(getCurrentWord()); // Remove the change for the current word updateNextWord(); // Move to the next word @@ -108,10 +133,6 @@ public class WordRoleUpdater extends JFrame { acceptAllButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - for (String word : wordRoles.keySet()) { - changes.put(word, true); - } - updateDatabase(); // Update database before ending JOptionPane.showMessageDialog(WordRoleUpdater.this, "All changes accepted. Database updated."); System.exit(0); } @@ -144,6 +165,23 @@ public class WordRoleUpdater extends JFrame { return (String) wordRoles.keySet().toArray()[currentIndex]; } + private void goBack(){ + if(currentIndex>0){ + currentIndex--; + updateLabels(); + }else + JOptionPane.showMessageDialog(this, "Can not go back further"); + } + + private void goNext(){ + if (currentIndex < wordRoles.size()-1) { + currentIndex++; + updateLabels(); + } else { + JOptionPane.showMessageDialog(this, "Can not go next further"); + } + } + private void updateNextWord() { currentIndex++; if (currentIndex >= wordRoles.size()) { @@ -151,12 +189,23 @@ public class WordRoleUpdater extends JFrame { JOptionPane.showMessageDialog(this, "No more changes to review."); System.exit(0); } else { - updateWordLabel(); - updateRoleLabel(); - updateProgressLabel(); + updateLabels(); } } + private void updateLabels(){ + progressLabel.setText((currentIndex + 1) + "/" + wordRoles.size()); + + String word = getCurrentWord(); + wordLabel.setText("Word: " + word); + + String role = wordRoles.get(word); + roleLabel.setText("Role: " + role); + + currentWordStatusLabel.setText("To be cleared? " + ((changes.get(word)? "No!": "Yes!"))); + } + + /* private void updateProgressLabel() { progressLabel.setText((currentIndex + 1) + "/" + wordRoles.size()); } @@ -172,6 +221,11 @@ public class WordRoleUpdater extends JFrame { roleLabel.setText("Role: " + role); } + // private void updateCurrentStatus() { + // String word = getCurrentWord(); + // currentWordStatusLabel.setText("To be cleared? " + ((changes.get(word)? "No!": "Yes!"))); + // } + */ private void updateDatabase() { try { Statement statement = connection.createStatement(); diff --git a/CheckerCorrector/SQLite/newdatabase.db b/CheckerCorrector/SQLite/newdatabase.db index aab4acbd71206566ddc3b038f8cd8435601f35c5..196d526d18501292ae92ef27254ad8aa18db46e2 100644 Binary files a/CheckerCorrector/SQLite/newdatabase.db and b/CheckerCorrector/SQLite/newdatabase.db differ diff --git a/CheckerCorrector/util/ArgumentParser.java b/CheckerCorrector/util/ArgumentParser.java index d50370880ada85ff2344e1935470f9976aa83c40..7f0b46505e676c55864449652494c734933dd4dd 100644 --- a/CheckerCorrector/util/ArgumentParser.java +++ b/CheckerCorrector/util/ArgumentParser.java @@ -10,6 +10,8 @@ public class ArgumentParser { private boolean updateToken; private boolean checkSentence; private boolean checkFile; + private boolean checkGUI; + private boolean correctionGUI; private boolean updateHashTable; private boolean validateUpdates; @@ -19,6 +21,8 @@ public class ArgumentParser { checkFile = false; updateHashTable = false; validateUpdates = false; + checkGUI = false; + correctionGUI = false; parseArguments(Arrays.asList(args)); } @@ -61,6 +65,12 @@ public class ArgumentParser { case "--validateUpdates": validateUpdates = true; break; + case "--checkGUI": + checkGUI = true; + break; + case "--correctionGUI": + correctionGUI = true; + break; // Add cases for other arguments here default: // Handle unknown arguments or simply ignore them @@ -104,4 +114,10 @@ public class ArgumentParser { public boolean isValidateUpdates(){ return validateUpdates; } + public boolean isCheckGUI(){ + return checkGUI; + } + public boolean isCorrectionGUI(){ + return correctionGUI; + } } diff --git a/CheckerCorrector/util/StringFileWriter.java b/CheckerCorrector/util/StringFileWriter.java index 30a8c1f1747fb869bfa444969175bd05139fd981..3c9db527204169bb0073a0543601344111f7cb16 100644 --- a/CheckerCorrector/util/StringFileWriter.java +++ b/CheckerCorrector/util/StringFileWriter.java @@ -3,21 +3,36 @@ package util; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; +import java.io.File; public class StringFileWriter { private StringBuilder stringBuilder; private String filePath; private String space; + private boolean appendFlag; private StringFileWriter(String filePath) { this.stringBuilder = new StringBuilder(); this.filePath = filePath; this.space = " "; + this.appendFlag = false; } private StringFileWriter(String filePath, String S) { this.stringBuilder = new StringBuilder(); this.filePath = filePath; this.space = S; + this.appendFlag = false; + } + + private StringFileWriter(String filePath, String S, boolean appendFlag) { + this.stringBuilder = new StringBuilder(); + this.filePath = filePath; + this.space = S; + this.appendFlag = appendFlag; + } + + public static StringFileWriter of(String filePath, String S, boolean appendFlag) { + return new StringFileWriter(filePath, S, appendFlag); } public static StringFileWriter of(String filePath, String S) { @@ -34,8 +49,21 @@ public class StringFileWriter { } public void writeToFile() throws IOException { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, appendFlag))) { writer.write(stringBuilder.toString()); } } + public static void deleteFile(String fileName) { + File file = new File(fileName); + if (file.exists()) { + boolean deleted = file.delete(); + if (deleted) { + System.out.println("File '" + fileName + "' has been deleted successfully."); + } else { + System.out.println("Failed to delete the file '" + fileName + "'."); + } + } else { + System.out.println("File '" + fileName + "' does not exist."); + } + } } diff --git a/Crawler/bin/Debug.class b/Crawler/bin/Debug.class deleted file mode 100644 index 6c67077d7215ee6388787be1554cfc9673591f06..0000000000000000000000000000000000000000 Binary files a/Crawler/bin/Debug.class and /dev/null differ diff --git a/Crawler/bin/RegexParser.class b/Crawler/bin/RegexParser.class deleted file mode 100644 index 46de2defb90b09aa71e018cd21bd294cd4afe1be..0000000000000000000000000000000000000000 Binary files a/Crawler/bin/RegexParser.class and /dev/null differ diff --git a/Crawler/bin/RobotsTXT.class b/Crawler/bin/RobotsTXT.class deleted file mode 100644 index f89d86019f47db38b3b05b8c30cd6c0e847b4abd..0000000000000000000000000000000000000000 Binary files a/Crawler/bin/RobotsTXT.class and /dev/null differ diff --git a/Crawler/bin/ScratchCrawler.class b/Crawler/bin/ScratchCrawler.class deleted file mode 100644 index 255b2164e45d9c3f54f13ecb902aa80e2883e6d2..0000000000000000000000000000000000000000 Binary files a/Crawler/bin/ScratchCrawler.class and /dev/null differ diff --git a/Crawler/target/classes/ScratchCrawler.class b/Crawler/target/classes/ScratchCrawler.class deleted file mode 100644 index baf945b9d85db1d9dad16c13186944ec79433bd5..0000000000000000000000000000000000000000 Binary files a/Crawler/target/classes/ScratchCrawler.class and /dev/null differ diff --git a/Crawler/target/classes/crawledData.txt b/Crawler/target/classes/crawledData.txt deleted file mode 100644 index 04d59b443cb614295366b23decc1a08e29d12398..0000000000000000000000000000000000000000 --- a/Crawler/target/classes/crawledData.txt +++ /dev/null @@ -1,43 +0,0 @@ - Crawler Test Site

Link alternate media handheld


yLb2Nwr0mmT+FJwNkwkk
- Link alternate media print

Link alternate media print


4aibl+gquo9XzDJWu0Va

- RSS Title This is an example of an RSS feed http://www.example.com/main.html Mon, 06 Sep 2010 00:01:00 +0000 Sun, 06 Sep 2009 16:20:00 +0000 1800 Example entry Here is some text containing an interesting description. http://www.example.com/blog/post/1 7bd204c6-1655-4c27-aeee-53f933c5395f Sun, 06 Sep 2009 16:20:00 +0000 - Example Feed A subtitle. urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 2003-12-13T18:30:02Z Atom-Powered Robots Run Amok urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a 2003-12-13T18:30:02Z Some text.

This is the entry content.

John Doe johndoe@example.com
- Example Domain

Example Domain

This domain is for use in illustrative examples in documents. You may use - Crawler Test Site

Link alternate media handheld


yLb2Nwr0mmT+FJwNkwkk
- Crawler Test Site

Link alternate media handheld


yLb2Nwr0mmT+FJwNkwkk
- Crawler Test Site

Link alternate media handheld


yLb2Nwr0mmT+FJwNkwkk
- Link alternate media print

Link alternate media print


4aibl+gquo9XzDJWu0Va

- RSS Title This is an example of an RSS feed http://www.example.com/main.html Mon, 06 Sep 2010 00:01:00 +0000 Sun, 06 Sep 2009 16:20:00 +0000 1800 Example entry Here is some text containing an interesting description. http://www.example.com/blog/post/1 7bd204c6-1655-4c27-aeee-53f933c5395f Sun, 06 Sep 2009 16:20:00 +0000 - Example Feed A subtitle. urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 2003-12-13T18:30:02Z Atom-Powered Robots Run Amok urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a 2003-12-13T18:30:02Z Some text.

This is the entry content.

John Doe johndoe@example.com
- Example Domain

Example Domain

This domain is for use in illustrative examples in documents. You may use - Link alternate media print

Link alternate media print


4aibl+gquo9XzDJWu0Va

- RSS Title This is an example of an RSS feed http://www.example.com/main.html Mon, 06 Sep 2010 00:01:00 +0000 Sun, 06 Sep 2009 16:20:00 +0000 1800 Example entry Here is some text containing an interesting description. http://www.example.com/blog/post/1 7bd204c6-1655-4c27-aeee-53f933c5395f Sun, 06 Sep 2009 16:20:00 +0000 - Example Feed A subtitle. urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 2003-12-13T18:30:02Z Atom-Powered Robots Run Amok urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a 2003-12-13T18:30:02Z Some text.

This is the entry content.

John Doe johndoe@example.com
-Internet Archive: Digital Library of Free & Borrowable Books, Movies, Music & Wayback Machine