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

initial

parents
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
package DBinterface;

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

import DirectedGraph.DirectedGraph;

import java.sql.*;
import StateMachine.*;

public class DBinterface {
    public void checkTokenInDatabase(String sentence, DirectedGraph<State> graph){
        StateMachine<State> SM = new StateMachine<>();
        sentence = sentence.replaceAll("\\p{Punct}", " $0");
        String[] tokens = sentence.split("\\s+");
        String url = "jdbc:sqlite:./SQLite/mydatabase.db";
        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 + "';";
                    ResultSet resultSet = statement.executeQuery(query);
                    if (resultSet.next()) {
                        String role = resultSet.getString("role");
                        // Replace the token with its role
                        tokens[i] = role;
                    }
                }
            }

            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

            boolean isFollowed = SM.isStateMachineFollowed(graph, actions, initialState);
            if (isFollowed) {
                System.out.println("Sequence of actions follows the state machine.");
            } else {
                System.out.println("Sequence of actions does not follow the state machine.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
+26 −0
Original line number Diff line number Diff line
package DirectedGraph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DirectedGraph<NodeClass> {
    private Map<NodeClass, List<NodeClass>> graph;

    public DirectedGraph() {
        graph = new HashMap<>();
    }

    public void addNode(NodeClass node) {
        graph.putIfAbsent(node, new ArrayList<>());
    }

    public void addEdge(NodeClass source, NodeClass destination) {
        graph.get(source).add(destination);
    }

    public List<NodeClass> getAdjacentNodes(NodeClass node) {
        return graph.getOrDefault(node, new ArrayList<>());
    }
}
 No newline at end of file

Main.java

0 → 100644
+51 −0
Original line number Diff line number Diff line
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;




import DirectedGraph.DirectedGraph;
import StateMachine.*;
import DBinterface.DBinterface;

public class Main {
     public static void main(String[] args) {
        DirectedGraph<State> graph = new DirectedGraph<>();
        

        // Build the state machine graph
        graph.addNode(State.START);
        graph.addNode(State.PRONOUN);
        graph.addNode(State.VERB);
        graph.addNode(State.ADJECTIVE);
        graph.addNode(State.ARTICLE);
        graph.addNode(State.NOUN);
        graph.addNode(State.DOT);
        

        graph.addEdge(State.START,     State.PRONOUN);
        graph.addEdge(State.PRONOUN,   State.VERB);
        graph.addEdge(State.VERB,      State.ADJECTIVE);
        graph.addEdge(State.VERB,      State.ARTICLE);
        graph.addEdge(State.ARTICLE,   State.ADJECTIVE);
        graph.addEdge(State.ADJECTIVE, State.DOT);
        graph.addEdge(State.ADJECTIVE, State.NOUN);
        graph.addEdge(State.NOUN,      State.DOT);
        graph.addEdge(State.DOT,       State.END);

        
        // Sentence to tokenize
        String sentence = "they are a good.";
        // Tokenize the sentence
        DBinterface dbInterface = new DBinterface();

        dbInterface.checkTokenInDatabase(sentence, graph);
        
    }
}

SQLite/mydatabase.db

0 → 100644
+12 KiB

File added.

No diff preview for this file type.

+40.2 KiB

File added.

No diff preview for this file type.