Commit 3ed91af1 authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

correctorWithCurrentGrammar

parent 7cc39efa
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ public class DBinterface {
            // Check if the sequence of actions follows the state machine

            TwoListStruct<State, Integer> output = SM.suggestedStateMachine(graph, actions, initialState);
            output.displayArrays();
            List<State> suggested = output.getOutputList();
            List<Integer> flags   = output.getChangesList();
            int insertCnt = 0;
@@ -158,7 +159,7 @@ public class DBinterface {
                                tokenList.add(word);
                        }
                    }
                }else if(flags.get(i)==2){
                }else if(flags.get(i)==3){
                    try (Statement statement = connection.createStatement()) {
                        System.out.println(suggested.get(i));
                        String query = "SELECT word FROM word_roles WHERE role = '" + suggested.get(i) + "';";
@@ -169,7 +170,7 @@ public class DBinterface {
                            word = resultSet.getString("word");
                            System.out.println("Here I am: "+ word);
                            if(i<tokenList.size())
                                tokenList.add(i-insertCnt,word);
                                tokenList.add(i,word);
                            else
                                tokenList.add(word);
                        }

DirectedGraph/DFS.java

0 → 100644
+45 −0
Original line number Diff line number Diff line
package DirectedGraph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import StateMachine.State;
import util.ListToString;

public class DFS {

    private DFS() {
        allPaths = new HashSet<>();
    }

    public static  DFS of(){
        return new DFS();
    }

    public Set<String> dfs(DirectedGraph<State> graph, State currentState, int maxDepth){
        List<State> path = new ArrayList<>();
        dfsRecurssion(graph, currentState, 0, maxDepth, path);
        return allPaths;
    }
    private void dfsRecurssion(DirectedGraph<State> graph, State currentState, int depth, int maxDepth, List<State> path) {
        if (currentState.equals("dot") || depth >= maxDepth) {
            ListToString lTS =  ListToString.of();
            StringBuilder sb = new StringBuilder();
            for(State p: path){
                lTS.addString(p);
            }
            allPaths.add(lTS.getString());
            return;
        }
        List<State> transitions = graph.getAdjacentNodes(currentState);
        for (State nextState : transitions) {
            path.add(nextState);
            dfsRecurssion(graph, nextState, depth + 1, maxDepth, path);
            path.remove(path.size() - 1);
        }
    }
    Set<String> allPaths;
}
+1 −17
Original line number Diff line number Diff line
package DirectedGraph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -27,21 +28,4 @@ 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
+25 −0
Original line number Diff line number Diff line
@@ -37,4 +37,29 @@ public enum State {
        static public State first() {
            return State.values()[0];
        }

        public static char mapToChar(State element) {
            return (char) ('a' + element.ordinal());
        }
    
        // Function to perform reverse mapping from characters 'a' to 'z' to enum elements
        public static State mapToEnum(char character) {
            int index = character - 'a';
            if (index >= 0 && index < values().length) {
                return values()[index];
            } else {
                throw new IllegalArgumentException("Character is not in range 'a' to 'z'");
            }
        }

        // Function to perform reverse mapping from characters 'a' to 'z' to enum elements
        public static State mapToEnum(String character) {
            int index = character.charAt(0) - 'a';
            if (index >= 0 && index < values().length) {
                return values()[index];
            } else {
                throw new IllegalArgumentException("Character is not in range 'a' to 'z'");
            }
        }
        
}
+9 −4
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Set;

import DirectedGraph.DirectedGraph;
import DirectedGraph.DFS;

import util.TwoListStruct;
import util.ListToString;
@@ -46,21 +47,25 @@ public class StateMachine{

        StateMachine SM = new StateMachine();
        int confidence = SM.isStateMachineFollowed(graph, actions, initialState, 0);
        if(confidence<10)
        if(confidence<10){
            for(int i =0; i<actions.size(); i++)
                flags.add(0);
            return TwoListStruct.of(actions, flags);
        else{
        }else{
            Set<String> allPaths = new HashSet<>();
            DirectedGraph.dfs(graph, initialState, 0, actions.size()+4, new ArrayList<>(), allPaths);
            DFS dfs = DFS.of();
            allPaths.addAll(dfs.dfs(graph, actions.get(0), actions.size()+2));
            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);
                TypoCorrector tc = TypoCorrector.of("all_path.txt", true, -3, 0, -2, -1);
                ListToString lts = ListToString.of();
                for(State action: actions)
                    lts.addString(action);
                String suggestedActionsString = tc.closestWord(lts.getString());
                System.out.println(lts.getString() + " -> " + suggestedActionsString);
                List<State> parts = StringToList.split(suggestedActionsString);
                suggestedAction.addAll(parts);
                flags.addAll(tc.traceBack());
Loading