Commit 7cc39efa authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

faultyCorrectorNewAlgorithm

parent 6d0e7a21
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -140,16 +140,18 @@ public class DBinterface {
            TwoListStruct<State, Integer> output = SM.suggestedStateMachine(graph, actions, initialState);
            List<State> suggested = output.getOutputList();
            List<Integer> flags   = output.getChangesList();
            int insertCnt = 0;
            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: ");
                        
                        System.out.print("!!! 1: " + resultSet + "| ");
                        if (resultSet.next()) {
                            word = resultSet.getString("word");
                            System.out.println(word);
                            System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                                tokenList.set(i,word);
                            else
@@ -158,15 +160,16 @@ public class DBinterface {
                    }
                }else if(flags.get(i)==2){
                    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);
                        System.out.print("!!! 2: ");
                        if (resultSet.next()) {
                            insertCnt++;
                            word = resultSet.getString("word");
                            System.out.println(word);
                            System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                                tokenList.add(i,word);
                                tokenList.add(i-insertCnt,word);
                            else
                                tokenList.add(word);
                        }
+22 −0
Original line number Diff line number Diff line
@@ -3,6 +3,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.w3c.dom.Node;

import util.ListToString;

public class DirectedGraph<NodeClass> {
    private Map<NodeClass, List<NodeClass>> graph;
@@ -22,4 +27,21 @@ public class DirectedGraph<NodeClass> {
    public List<NodeClass> getAdjacentNodes(NodeClass node) {
        return graph.getOrDefault(node, new ArrayList<>());
    }

    public static <NC> void dfs(DirectedGraph<NC> graph, NC currentState, int depth, int maxDepth, List<NC> path, Set<String> allPaths) {
        if (currentState.equals("dot") || depth >= maxDepth) {
            ListToString<NC> lTS =  ListToString.of();
            for(NC p: path){
                lTS.addString(p);
            }
            allPaths.add(lTS.toString());
            return;
        }
        
        for (NC nextState : graph.getAdjacentNodes(currentState)) {
            path.add(nextState);
            dfs(graph, nextState, depth + 1, maxDepth, path, allPaths);
            path.remove(path.size() - 1);
        }
    }
}
 No newline at end of file
(12 KiB)

File changed.

No diff preview for this file type.

+51 −0
Original line number Diff line number Diff line
package StateMachine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import DirectedGraph.DirectedGraph;

import util.TwoListStruct;
import util.ListToString;
import util.StringFileWriter;
import util.StringToList;
import TypoCorrector.TypoCorrector;

public class StateMachine{

    public int isStateMachineFollowed(DirectedGraph<State> graph, List<State> actions, State initialState, int initialConf) {
        int confidence = initialConf;
        //System.out.println("----------------------------------------------------");
@@ -26,6 +34,47 @@ 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;

        StateMachine SM = new StateMachine();
        int confidence = SM.isStateMachineFollowed(graph, actions, initialState, 0);
        if(confidence<10)
            return TwoListStruct.of(actions, flags);
        else{
            Set<String> allPaths = new HashSet<>();
            DirectedGraph.dfs(graph, initialState, 0, actions.size()+4, new ArrayList<>(), allPaths);
            StringFileWriter sfw = StringFileWriter.of("all_path.txt", "\n");
            for(String path: allPaths)
                sfw.appendString(path);
            try {
                sfw.writeToFile();
                TypoCorrector tc = TypoCorrector.of("all_path.txt", true);
                ListToString lts = ListToString.of();
                for(State action: actions)
                    lts.addString(action);
                String suggestedActionsString = tc.closestWord(lts.getString());
                List<State> parts = StringToList.split(suggestedActionsString);
                suggestedAction.addAll(parts);
                flags.addAll(tc.traceBack());
                return TwoListStruct.of(suggestedAction, flags);
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }

        }
        return TwoListStruct.of(suggestedAction, flags);
    }


    /*
    public TwoListStruct suggestedStateMachine(DirectedGraph<State> graph, List<State> actions, State initialState) {
        
        //System.out.println("----------------------------------------------------");
@@ -82,6 +131,7 @@ public class StateMachine{
                            System.out.print("| updated to with missing: "+ checkState + " =)");
                            flags.add(2);
                            currentState = checkState;
                            tempState = currentState;
                            i--;
                        }
                    }
@@ -98,4 +148,5 @@ public class StateMachine{
        
        return TwoListStruct.of(suggestedAction, flags);
    }
     */
}
 No newline at end of file
+72 −6
Original line number Diff line number Diff line
@@ -4,16 +4,33 @@ import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.DeepCopyTwoD;

public class TypoCorrector {
    TypoCorrector(String filePath){
    private TypoCorrector(String filePath){
        dic = new ArrayList<>();
        readWordsFromFile(filePath);
        traceBackFlag = false;
        copyOfDirMar = DeepCopyTwoD.createEmpty();
    }

    public static TypoCorrector of (String filename){
        return new TypoCorrector(filename);
    }

    private TypoCorrector(String filePath, Boolean tBFalg){
        dic = new ArrayList<>();
        readWordsFromFile(filePath);
        traceBackFlag = tBFalg;
        copyOfDirMar = DeepCopyTwoD.createEmpty();
    }

    public static TypoCorrector of (String filename, Boolean tBFalg){
        return new TypoCorrector(filename, tBFalg);
    }

    public void readWordsFromFile(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
@@ -37,6 +54,8 @@ public class TypoCorrector {
        int curMax = -(1<<30);
        String wordMax = new String();
        int indCur = 0;

        DeepCopyTwoD copyOfDirMar = DeepCopyTwoD.createEmpty();
        for(String dicS: dic){
            int m = dicS.length();
            int n = word.length();
@@ -54,11 +73,12 @@ public class TypoCorrector {
                    int curScore = 0;
                    int up   = scoreMat[i][j-1] + ((dirMat[i][j-1]==2 || dirMat[i][j-1]==0)?gapExtend:gapOpen);
                    int left = scoreMat[i-1][j] + ((dirMat[i-1][j]==3 || dirMat[i-1][j]==0)?insertGapExtend:insertGapOpen);
                    int diag = scoreMat[i-1][j-1] + (word.charAt(j-1)==dicS.charAt(i-1)?matchScore:mismatchScore);
                    int s = (word.charAt(j-1)==dicS.charAt(i-1)?matchScore:mismatchScore);
                    int diag = scoreMat[i-1][j-1] + s;


                    curScore = diag;
                    dirMat[i][j]= 1;
                    dirMat[i][j]= s-1;

                    if(up>curScore){
                    curScore = up;
@@ -76,15 +96,60 @@ public class TypoCorrector {
            if(disCur>curMax){
                wordMax = dicS;
                curMax = disCur;
                if(traceBackFlag)
                    copyOfDirMar.setArray(dirMat);
            }
            indCur ++;
        }  
        
        
        return (curMax<4 && wordMax.length()>0)?wordMax:word;
        
    }
    
    public List<Integer> traceBack(){
        int [][] dirMat     = copyOfDirMar.getArray();
        copyOfDirMar.displayArray();
        List<Integer> trace = new ArrayList<>();
        if(traceBackFlag){
            int m = dirMat.length;
            if(m>0){
                int n = dirMat[0].length;
                return traceBackRecursion(dirMat, trace, m-1, n-1);
            }else{
                return trace;
            }
        }else{
            return trace;
        }
    }
    private List<Integer> traceBackRecursion(int[][] dirMat, List<Integer> trace, int i, int j){
        if(i==0 && j==0){
            return trace;
        }
        System.out.println(i + " " + j);
        int curDir = dirMat[i][j];
        if(curDir==2){
            trace.add(2);
            return traceBackRecursion(dirMat, trace, i, j-1);
        }else if(curDir==3){
            trace.add(3);
            return traceBackRecursion(dirMat, trace, i-1, j);
        }else{
            if(i>0 && j>0){
                trace.add(curDir==(matchScore-1)?0:1);
                return traceBackRecursion(dirMat, trace, i-1, j-1);
            }else if(i==0){
                trace.add(2);
                return traceBackRecursion(dirMat, trace, i, j-1);
            }else{
                trace.add(3);
                return traceBackRecursion(dirMat, trace, i-1, j);
            }
        }   
    }

    ArrayList<String> dic;
    boolean traceBackFlag;
    DeepCopyTwoD copyOfDirMar;
    final int mismatchScore  = -2;
    final int matchScore = 0;
    final int gapOpen   = -2;
@@ -93,4 +158,5 @@ public class TypoCorrector {
    final int insertGapOpen  = -2;
    final int insertGapExtend  = -1;
    final int minusInf = -(1<<4);
    
}
Loading