Commit 6d0e7a21 authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

faultyCorrector

parent eb5c5157
Loading
Loading
Loading
Loading

Corrector.java

0 → 100644
+57 −0
Original line number Diff line number Diff line
import java.io.IOException;
import java.util.List;

import DBinterface.DBinterface;
import DirectedGraph.BasicGraph;
import DirectedGraph.DirectedGraph;
import util.ArgumentParser;
import util.JsonMaker;
import util.PhraseExtractor;
import util.SentenceExtractor;
import util.StringFileWriter;
import StateMachine.*;


public class Corrector {
    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();
        StringFileWriter stringWriter = StringFileWriter.of("corrected.txt");

        if(argPars.isCheckFile()){
            SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
            List<String> extractedSentences = extractor.getSentences();  
            
            
            for (String sentence : extractedSentences) {
                System.out.println("Sentence: " + sentence);
                stringWriter.appendString(dbInterface.correctTokenInDatabase(sentence, 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());
                }
                System.out.println("##########################################################");
                
            }
        }else if(argPars.isCheckSentence()){

            System.out.println("Sentence: " + argPars.getSentence());
            stringWriter.appendString(dbInterface.correctTokenInDatabase(argPars.getSentence(), 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());
            }
            System.out.println("##########################################################");
        }
          
    }
}
+113 −0
Original line number Diff line number Diff line
package DBinterface;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import DirectedGraph.DirectedGraph;
@@ -8,6 +9,8 @@ import DirectedGraph.DirectedGraph;
import java.sql.*;
import StateMachine.*;
import TypoCorrector.TypoCorrector;
import util.TwoListStruct;


public class DBinterface {
    public int checkTokenInDatabase(String sentence, DirectedGraph<State> graph){
@@ -74,4 +77,114 @@ public class DBinterface {
        }
        return 0;
    }
    public String correctTokenInDatabase(String sentence, DirectedGraph<State> graph){
        StateMachine SM = new StateMachine();
        sentence = sentence.replaceAll("\\p{Punct}", " $0");
        String[] tokens = sentence.split("\\s+");
        String[] tokensCopy = tokens.clone();
        List<String> tokenList = new ArrayList<>(Arrays.asList(tokensCopy));
        String url = "jdbc:sqlite:./SQLite/mydatabase.db";
        String dicFileName = "./SQLite/smallDic.txt";
        TypoCorrector typoChecker =  TypoCorrector.of(dicFileName);
        int initialConf = 0;
        try (Connection connection = DriverManager.getConnection(url)) {

            // Lookup each token in the database and categorize it
            for (int i = 0; i < tokens.length; i++) {
                String token = tokens[i];
                
                try (Statement statement = connection.createStatement()) {
                    
                    String query = "SELECT role FROM word_roles WHERE word = '" + token + "';";
                    String role = new String();
                    
                    ResultSet resultSet = statement.executeQuery(query);
                    if (resultSet.next()) {
                        role = resultSet.getString("role");
                        //System.out.print("first try: " + token + " -> " + role);
                        tokens[i] = role;
                    }else{
                        String tokenCorrected = new String();
                        if(role.isEmpty()){
                            tokenCorrected = typoChecker.closestWord(token);
                            if(!tokenCorrected.equals(token))
                                initialConf += 5;
                           // System.out.print("Corrected token: " + token + " -> " + tokenCorrected);

                            query = "SELECT role FROM word_roles WHERE word = '" + tokenCorrected + "';";
                            // Replace the token with its role
                            resultSet = statement.executeQuery(query);
                            if (resultSet.next()) {
                                tokenList.set(i,tokenCorrected);
                                role = resultSet.getString("role");
                              //  System.out.print("| Second try: "+ token + " -> " + role);
                                tokens[i] = role;
                            }
                        }

                    } 
                    }
                    //System.out.println();
            }

            List<State> actions = new ArrayList<>();

            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

            TwoListStruct<State, Integer> output = SM.suggestedStateMachine(graph, actions, initialState);
            List<State> suggested = output.getOutputList();
            List<Integer> flags   = output.getChangesList();
            for(int i=0; i<suggested.size(); i++){
                if(flags.get(i)==1){
                    try (Statement statement = connection.createStatement()) {
                        String query = "SELECT word FROM word_roles WHERE role = '" + suggested.get(i) + "';";
                        String word = new String();
                        ResultSet resultSet = statement.executeQuery(query);
                        System.out.print("!!! 1: ");
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            System.out.println(word);
                            if(i<tokenList.size())
                                tokenList.set(i,word);
                            else
                                tokenList.add(word);
                        }
                    }
                }else if(flags.get(i)==2){
                    try (Statement statement = connection.createStatement()) {
                        String query = "SELECT word FROM word_roles WHERE role = '" + suggested.get(i) + "';";
                        String word = new String();
                        ResultSet resultSet = statement.executeQuery(query);
                        System.out.print("!!! 2: ");
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            System.out.println(word);
                            if(i<tokenList.size())
                                tokenList.add(i,word);
                            else
                                tokenList.add(word);
                        }
                    }
                }
            }   
            StringBuilder result = new StringBuilder();
            boolean flagStart = false;
            for (String token : tokenList) {
                if(flagStart && !token.equals(".") && !token.equals(","))
                    result.append(" ");
                result.append(token);
                flagStart = true;  
            }
            return result.toString();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new String();
    }
}
+5 −1
Original line number Diff line number Diff line
dev_checker:
	javac -d bin Checker.java **/*.java
	jar cvfm checker.jar manifest.txt -C bin . -C SQLite .
	jar cvfm checker.jar manifestChecker.txt -C bin . -C SQLite .

dev_corrector:
	javac -d bin Corrector.java **/*.java
	jar cvfm corrector.jar manifestCorrector.txt -C bin . -C SQLite .
 No newline at end of file
+76 −0
Original line number Diff line number Diff line
package StateMachine;

import java.util.ArrayList;
import java.util.List;

import DirectedGraph.DirectedGraph;

import util.TwoListStruct;

public class StateMachine{
    public int isStateMachineFollowed(DirectedGraph<State> graph, List<State> actions, State initialState, int initialConf) {
        int confidence = initialConf;
@@ -22,4 +26,76 @@ public class StateMachine{
        }
        return confidence;
    }
    public TwoListStruct suggestedStateMachine(DirectedGraph<State> graph, List<State> actions, State initialState) {
        
        //System.out.println("----------------------------------------------------");
        State currentState = initialState;
        boolean flag = false;
        List<State> suggestedAction = new ArrayList<>();
        List<Integer> flags         = new ArrayList<>();
        State tempState = currentState;
        //suggestedAction.add(currentState);
        int cnt = 0;
        for(int i=0; i<actions.size(); i++){
        //for (State action : actions) {
            State action = actions.get(i);
            System.out.print(currentState);
            
            List<State> transitions = graph.getAdjacentNodes(currentState);
            if(flag && cnt <2){
                List<State> tempTransitions = graph.getAdjacentNodes(tempState);
                for(State checkState: tempTransitions){
                    if(checkState != State.START){
                        List<State> checkTransitions = graph.getAdjacentNodes(checkState);
                        if(checkTransitions.contains(action)){
                            System.out.print("| updated to: "+ checkState);
                            suggestedAction.add(checkState);
                            flags.add(1);
                        }
                    }
                }
                flag = false;
                cnt = 0;
            }
            if (!transitions.contains(action)) {
                //System.out.print("? " + action);
                tempState = currentState;
                currentState = State.START;
                flag = true;
                cnt ++;
            }
            if(!flag){
                suggestedAction.add(currentState);
                System.out.print("| no update");
                flags.add(0);
                currentState = action; // Transition to the next state
            }else if(cnt <2){
                suggestedAction.add(currentState);
                flags.add(0);
                System.out.print("| no update");
                List<State> tempTransitions = graph.getAdjacentNodes(tempState);
                for(State checkState: tempTransitions){
                    if(checkState != State.START){
                        List<State> checkTransitions = graph.getAdjacentNodes(checkState);
                        if(checkTransitions.contains(action)){
                            suggestedAction.add(checkState);
                            System.out.print("| updated to with missing: "+ checkState + " =)");
                            flags.add(2);
                            currentState = checkState;
                            i--;
                        }
                    }
                }
                flag = false;
                cnt = 0;
            }else{
                currentState = action; // Transition to the next state
            }
            System.out.println();
            //System.out.println();
            
        }
        
        return TwoListStruct.of(suggestedAction, flags);
    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
it a very good book, but it is small book. it is very bad.
 No newline at end of file
it a very good book, but it small small book. it is very bad. I am book.
 No newline at end of file
Loading