Commit 0b9824c9 authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

workingTunedTokenUpdate

parent 82a7c7cc
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -34,9 +34,9 @@ public class Checker {
                    }                        
                }
            }else if(argPars.isCheckSentence()){
                dbInterface.updateTokenInDatabase(argPars.getSentence().toLowerCase(), graph);
                //dbInterface.updateTokenInDatabase(argPars.getSentence().toLowerCase(), graph);
                for (String phrase : PhraseExtractor.fromSentence(argPars.getSentence(),3, 5).getPhrases()) {
                    //dbInterface.updateTokenInDatabase(phrase.toLowerCase(), graph);                    
                    dbInterface.updateTokenInDatabase(phrase.toLowerCase(), graph);                    
                }
            }
        }else if(argPars.isUpdateHashTable()){
+82 −13
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import java.sql.*;
import StateMachine.*;
import TypoCorrector.TypoCorrector;
import util.TwoListStruct;
import util.StringProcessor;


public class DBinterface {
@@ -217,15 +218,22 @@ public class DBinterface {
    }

    public void updateTokenInDatabase(String sentence, DirectedGraph<State> graph){
        //System.out.print(sentence + " |");
        sentence = StringProcessor.processString(sentence);
        
        if(sentence.equals(""))
            return;
        StateMachine SM = new StateMachine();
        sentence = sentence.replaceAll("\\p{Punct}", " $0");
        //System.out.println(sentence);
        String[] tokens = sentence.split("\\s+");
        String[] tokensCopy = tokens.clone();
        List<Boolean> missFlag = new ArrayList();
        List<String> tokenList = new ArrayList<>(Arrays.asList(tokensCopy));
        String url       = "jdbc:sqlite:./SQLite/mydatabase.db";
        //String url       = "jdbc:sqlite:./SQLite/mydatabase.db";
        String urlinsert = "jdbc:sqlite:./SQLite/newdatabase.db";
        String dicFileName = "./SQLite/smallDic.txt";
        String url = "jdbc:sqlite:./SQLite/newdatabase.db";
        TypoCorrector typoChecker =  TypoCorrector.of(dicFileName);
        int initialConf = 0;
        int cntMiss = 0;
@@ -234,7 +242,7 @@ public class DBinterface {
            // Lookup each token in the database and categorize it
            for (int i = 0; i < tokens.length; i++) {
                String token = tokens[i];
                System.out.print("\nBefor token: "+tokens[i]+"| ");
            //    System.out.print("\nBefor token: "+tokens[i]+"| ");
                try (Statement statement = connection.createStatement()) {
                    
                    String query = "SELECT role FROM word_roles WHERE word = '" + token + "';";
@@ -244,7 +252,7 @@ public class DBinterface {
                    if (resultSet.next()) {
                        role = resultSet.getString("role");
                        //////System.out.print("first try: " + token + " -> " + role);
                        System.out.println("role: " + role);
                    //    System.out.println("role: " + role);
                        if(!role.equals("NAN")){
                            tokens[i] = role;
                            missFlag.add(false);
@@ -267,7 +275,7 @@ public class DBinterface {
                                tokenList.set(i,tokenCorrected);
                                role = resultSet.getString("role");
                               //  ////System.out.print("| Second try: "+ token + " -> " + role);
                                System.out.println("role: " + role);
                              //  System.out.println("role: " + role);
                                if(!role.equals("NAN")){
                                    tokens[i] = role;
                                    missFlag.add(false);
@@ -283,11 +291,12 @@ public class DBinterface {

                    } 
                    }
                    System.out.print("After token: "+tokens[i]+"| ");
                    //System.out.print("After token: "+tokens[i]+"| ");
                    //////System.out.println();
            }
            System.out.println("\nMISS: "+cntMiss);
            if(cntMiss<3 && cntMiss>0){
            
            //System.out.println("\nMISS: "+cntMiss);
            if(cntMiss<3 && cntMiss>0 && !missFlag.getLast()){
                List<State> actions = new ArrayList<>();

                for(String token: tokens){
@@ -298,20 +307,25 @@ public class DBinterface {
                
                // Check if the sequence of actions follows the state machine
                List<State> suggested = SM.updateDB(graph, actions, initialState);
                System.out.println(actions);
                System.out.println(suggested);
                System.out.println("---------------------------");
               // System.out.println(actions);
              //  System.out.println(suggested);
              //  System.out.println("---------------------------");
                int cntUp=0;
                for(int i=0; i<suggested.size(); i++){
                    if(!suggested.get(i).toString().equals(tokens[i]))
                        cntUp++;
                }
                System.out.println(missFlag);
                
                if(cntUp<3){
                    for(int i=0; i<tokens.length; i++){
                        if(missFlag.get(i)){
                            if(!suggested.get(i).toString().equals(tokens[i])){
                                System.out.println(tokens[i] + "| " + suggested.get(i));
                            if(!suggested.get(i).toString().equals(tokens[i]) && !suggested.get(i).toString().equals("nan") && State.validSuggestedState(suggested.get(i))){
                                System.out.println(sentence);
                                System.out.println(actions);
                                System.out.println(suggested);
                                System.out.println(missFlag);
                                System.out.println(tokens[i] + "-> " + suggested.get(i));
                                System.out.println("--------------------------------");
                                String sql = "INSERT INTO word_roles VALUES(?, ?);";
                                try (Connection conn = DriverManager.getConnection(urlinsert);
                                    PreparedStatement pstmt = conn.prepareStatement(sql)) {
@@ -332,4 +346,59 @@ public class DBinterface {
            e.printStackTrace();
        }
    }


    private HashMap<String, String> wordRolesMap;

    // Method to read data from SQLite database into HashMap
    public void readDataFromDatabase(String dbUrl) {
        wordRolesMap = new HashMap<>();

        try (Connection connection = DriverManager.getConnection(dbUrl)) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM word_roles");

            while (resultSet.next()) {
                String word = resultSet.getString("word");
                String role = resultSet.getString("role");
                wordRolesMap.put(word, role);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Method to update SQLite database with updated HashMap
    public void updateDatabase(String dbUrl) {
        try (Connection connection = DriverManager.getConnection(dbUrl)) {
            // Clear existing data in the table
            Statement clearStatement = connection.createStatement();
            clearStatement.executeUpdate("DELETE FROM word_roles");

            // Insert updated data from HashMap into the table
            PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO word_roles (word, role) VALUES (?, ?)");
            for (String word : wordRolesMap.keySet()) {
                String role = wordRolesMap.get(word);
                insertStatement.setString(1, word);
                insertStatement.setString(2, role);
                insertStatement.addBatch();
            }
            insertStatement.executeBatch();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Getter method for the HashMap
    public HashMap<String, String> getWordRolesMap() {
        return wordRolesMap;
    }

    // Setter method for the HashMap
    public void setWordRolesMap(HashMap<String, String> wordRolesMap) {
        this.wordRolesMap = wordRolesMap;
    }

}
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ public class BasicGraph {
        graph.addEdge(State.NOT,       State.ARTICLE);
        graph.addEdge(State.NOT,       State.DOT);
        graph.addEdge(State.OF,        State.NOUN);
        graph.addEdge(State.OF,        State.PRONOUN);
        graph.addEdge(State.NOUN,      State.OF);
        graph.addEdge(State.NOUN,      State.IS);
        graph.addEdge(State.PRONOUN,   State.IS);
+8 −6
Original line number Diff line number Diff line
@@ -19,14 +19,14 @@ public class DFS {
        return new DFS();
    }

    public Set<String> dfs(DirectedGraph<State> graph, State currentState, int maxDepth, int minDepth){
    public Set<String> dfs(DirectedGraph<State> graph, State currentState, State end, int maxDepth, int minDepth){
        List<State> path = new ArrayList<>();
        path.add(currentState);
        dfsRecurssion(graph, currentState, 0, maxDepth, path, minDepth);
        dfsRecurssion(graph, currentState, end, 0, maxDepth, path, minDepth);
        return allPaths;
    }
    private void dfsRecurssion(DirectedGraph<State> graph, State currentState, int depth, int maxDepth, List<State> path, int minDepth) {
        if ((currentState == State.DOT)) {
    private void dfsRecurssion(DirectedGraph<State> graph, State currentState, State end, int depth, int maxDepth, List<State> path, int minDepth) {
        if ((currentState == end)) {
            ListToString lTS =  ListToString.of();
            StringBuilder sb = new StringBuilder();
            int cnt=0;
@@ -34,8 +34,10 @@ public class DFS {
                lTS.addString(p);
                cnt++;
            }
            if(cnt>(maxDepth-minDepth))
            
            if(cnt>(maxDepth-minDepth)){
                allPaths.add(lTS.getString());
            }
            return;
        }else if(depth >= maxDepth){
            return;
@@ -43,7 +45,7 @@ public class DFS {
        List<State> transitions = graph.getAdjacentNodes(currentState);
        for (State nextState : transitions) {
            path.add(nextState);
            dfsRecurssion(graph, nextState, depth + 1, maxDepth, path, minDepth);
            dfsRecurssion(graph, nextState, end, depth + 1, maxDepth, path, minDepth);
            path.remove(path.size() - 1);
        }
    }
+20 KiB (32 KiB)

File changed.

No diff preview for this file type.

Loading