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

Merge branch 'Reza' into 'master'

Reza

See merge request ec504/ec504_projects/group6!17
parents 603db61f 102fae21
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ CheckerCorrector/corrector
CheckerCorrector/checker
Crawler/target/classes/ScratchCrawler.class
Crawler/target/classes/crawledData.txt
*.db
*.java
!CheckerCorrector/manifest*
!CheckerCorrector/**/*.java
+13 −1
Original line number Diff line number Diff line
@@ -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();
                }
            });
        }
          
    }
+91 −18
Original line number Diff line number Diff line
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<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) {
        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() {
        
        stringWriterGUI.appendString(this.curSentenceGUI);
        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;

    }
    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.");
+160 −61
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,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<State> graph){
@@ -80,16 +83,30 @@ 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 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);
    }

    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+");
@@ -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,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){
@@ -155,7 +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);
                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;
@@ -173,15 +212,41 @@ public class DBinterface {
                            if (resultSet.next()) {
                                word = resultSet.getString("word");
                                ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                                tokenList.set(i,word);
                            else
                                if(i+biasToken<tokenList.size()){
                                    
                                    if(flagsCorrection.isEmpty()){
                                        tokenList.set(i+biasToken,word);
                                        sfw.appendString(tokenList.get(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++;
                    tokenList.remove(i);
                        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));
@@ -191,14 +256,42 @@ public class DBinterface {
                            if (resultSet.next()) {
                                word = resultSet.getString("word");
                                ////System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                                tokenList.add(i,word);
                            else
                                
                                if(flagsCorrection.isEmpty()){
                                    sfw.appendString("IND: "+ i + " -> "+ word);
                                    if(i<tokenList.size()){
                                        tokenList.add(i+biasToken,word);
                                    }else{
                                        tokenList.add(word);
                                    }
                                }else if(flagsCorrection.get(flagsCorrectioncnt)){
                                    if(i<tokenList.size()){
                                    // 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("-----------------------------------------");
                if(flagsCorrection.isEmpty()){
                    sfw.writeToFile();
                }
            } 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;
@@ -211,6 +304,10 @@ public class DBinterface {
                flagStart = true;  
                i++;
            }
            if(!flagTypoCorrectionAccepted){
                result.append("|");
                result.append(flagsCorrectioncnt);
            }
            return result.toString();
        } catch (SQLException e) {
            e.printStackTrace();
@@ -268,12 +365,14 @@ public class DBinterface {
                            cntMiss ++;
                        }
                }
                if(cntMiss>1)
                    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<State> actions = new ArrayList<>();

                for(String token: tokens){
+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
Loading